﻿var Export={};
  Export.Page=function(){
    qid=0;//当前要操作的问题编号
    currentPage=1;
    pagesize=15;
    totalpage=0;
    totalrecord=0;
    state=-1;
    questionIDCollection='';
    url='QaAAjax.ashx';
    selectedcssoptions={color:'#FFFFFF','background-color':'#90d3e8',cursor:'pointer'};
    unselectedcssoptions={color:'#90d3e8','background-color':'#FFFFFF',cursor:'pointer'};
    isAdmin=true;
  };
  Export.Page.prototype={
     Init:function(){
        this.Dispose();
        this.RegisteKeyboard();
        this.PagerSet();
        this.LoadPage();
        this.SingleEventAttacth();
     },
     Dispose:function(){
        $('#faq_container').html('');
     },
     Reflesh:function(){
        this.Dispose();
        this.LoadPage();
        $('#background').click();
     },
     Set_Admin:function(val){
        isAdmin=val;
     },
     RegisteKeyboard:function(){
        if($.browser.msie){
            $('#txtAskContent,#txtNick,#txtContact').bind('keydown',function(){
                if(window.event.ctrlKey && window.event.keyCode==13){
                    $('#btnAskSubmit').click();
                }
            });
            $('#txtContent').bind('keydown',function(){
                if(window.event.ctrlKey && window.event.keyCode==13){
                    $('#btn_replay').click();
                }
            });
        }
     },
     PagerSet:function(){
        $('#first,#pre,#next,#last').hide();
     },
     PageSizeSet:function(){
        $('#ps_10,#ps_15,#ps_20').unbind('mouseover').unbind('mouseout');
     	switch(pagesize){
     	    case 10:
     	        $('#ps_10').css(selectedcssoptions);
     	        $('#ps_15,#ps_20').css(unselectedcssoptions);
     		    $('#ps_15,#ps_20').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#ps_15,#ps_20').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
     	        break;
     	    case 15:
     	        $('#ps_15').css(selectedcssoptions);
     	        $('#ps_10,#ps_20').css(unselectedcssoptions);
     		    $('#ps_10,#ps_20').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#ps_10,#ps_20').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
     	        break;
     	    case 20:
     	        $('#ps_20').css(selectedcssoptions);
     	        $('#ps_15,#ps_10').css(unselectedcssoptions);
     		    $('#ps_15,#ps_10').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#ps_15,#ps_10').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
     	        break;
     	}

     },
     StateSet:function(){
        $('#all,#processed,#unprocessed').unbind('mouseover').unbind('mouseout');
        switch(state){
            case -1:
                $('#all').css(selectedcssoptions);
                $('#processed,#unprocessed').css(unselectedcssoptions);
                $('#processed,#unprocessed').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#processed,#unprocessed').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
                break;
            case 0:
                $('#processed').css(selectedcssoptions);
                $('#all,#unprocessed').css(unselectedcssoptions);
                $('#all,#unprocessed').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#all,#unprocessed').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
                break;
            case 1:
                $('#unprocessed').css(selectedcssoptions);
                $('#all,#processed').css(unselectedcssoptions);
                $('#all,#processed').bind('mouseover',function(){
                    $(this).css(selectedcssoptions);
                });
                $('#all,#processed').bind('mouseout',function(){
                    $(this).css(unselectedcssoptions);
                });
                break;
        }
        
     },
     SingleEventAttacth:function(){//只需一次注册的事件
        $('#ask').click(function(){//问题窗口打开
     		$('#background').css({height:$(document).height(),width:'100%',display:'block'});
     		var askpaneloption={left:parseFloat($(document).width()/2-$('#askpanel').width()/2),top:document.documentElement.scrollTop+200,display:'block'};  		 
     		$('#askpanel').css(askpaneloption);
     		$(window).bind('scroll',function(){
     		    $('#askpanel').stop();
                $('#askpanel').animate({top:document.documentElement.scrollTop+200},{duration:200,easing:'swing'});
     		});
     		
     	});
     	
     	$('#btnAskSubmit').click(//提问事件
     	    function(){
     	        if($('#txtAskContent').val().length<11){
     	            $('#ap_msg').text('所问问题的字符长度应大于10个字！');
     	            $('#ap_msg').show();
     	            $('#txtAskContent').bind('keyup',function(){
     	                if($(this).val().length>=11){
     	                    $('#ap_msg').hide();
     	                    $('#txtAskContent').unbind('keyup');
     	                }
     	            });
     	        }
     	        else{
     	            //////这里写提问逻辑
     	            $('#asp_msg').text('数据提交中，请稍等...');
     	            $('#ap_msg').show();
     	            var parameters='w=aq&q='+encodeURIComponent($('#txtAskContent').val())+'&n='+encodeURIComponent($('#txtNick').val())+'&c='+encodeURIComponent($('#txtContact').val());
     	            $.ajax({
     	                type:'POST',
     	                data:parameters,
     	                url:url,
     	                error:function(e){
     	                    alert(e);
     	                },
     	                success:function(e){
     	                    $('#ap_msg').text('数据提交完成...');
     	                    $('#ap_msg').hide();
     	                    
     	                    $('#txtAskContent').val('');
     	                    //更新数据界面
     	                },
     	                complete:function(){
     	                    $('#ap_msg').text('');
                            ex.Reflesh();
     	                }
     	            });
     	        }
     	    }
     	);
        $('#btn_replay').click(//回复事件
     	    function(){
     	        if($('#txtContent').val().length<11){
     	            $('#d_msg').text('所回答问题的字符长度应大于10个字！');
     	            $('#d_msg').show();
     	            $('#txtContent').bind('keyup',function(){
     	                if($(this).val().length>=11){
     	                    $('#d_msg').hide();
     	                    $('#txtContent').unbind('keyup');
     	                }
     	            });
     	        }
     	        else{
     	            //////这里写回复逻辑
     	            var par='w=rq&a='+encodeURIComponent($('#txtContent').val())+'&i='+qid;
     	            $.ajax(
     	                {
     	                    type:'POST',
     	                    url:url,
     	                    data:par,
     	                    success:function(msg){
     	                    },
     	                    complete:function(){
     	                        $('#d_msg').text('');
     	                        $('#background').click();
     	                        $('#r_'+qid).remove();
     	                        $('#c_a_c_'+qid).html($('#txtContent').val());
     	                        $('#txtContent').val('');
     	                    }
     	                }
     	            );
     	        }
     	    }
     	);
     	$('#background').click(
     		function(){
     			$(this).hide();
     			$('#askpanel').hide();
     			$('#dialog').hide();
     			$(window).unbind('scroll');
     		}
     	);
     	$('#all').click(
     	    function(){
     	        state=-1;
     	        currentPage=1;
     	        ex.Reflesh();
     	    }
     	);
     	$('#processed').click(
     	    function(){
     	        state=0;
     	        currentPage=1;
     	        ex.Reflesh();
     	    }
     	);
     	$('#unprocessed').click(
     	    function(){
     	        state=1;
     	        currentPage=1;
     	        ex.Reflesh();
     	    }
     	);
     	$('#first').click(function(){
     	    currentPage=1;
     	    ex.Reflesh();
     	});
     	$('#pre').click(function(){
     	    currentPage-=1;
     	    ex.Reflesh();
     	});
     	$('#next').click(function(){
     	    currentPage+=1;
     	    ex.Reflesh();
     	});
     	$('#last').click(function(){
     	    currentPage=totalpage;
     	    ex.Reflesh();
     	});
     	$('#first,#pre,#last,#next').hover(
     	    function(){
     	        $(this).css(selectedcssoptions);
     	    },
     	    function(){
     	        $(this).css(unselectedcssoptions);
     	    }
     	);
     	$('#ps_10,#ps_15,#ps_20').click(
     	    function(){
     	        pagesize=parseInt($(this).html());
     	        ex.Reflesh();
     	    }
     	);
     },
     EventAttacth:function(){
     	$('.c_del').each(function(){//删除事件
     	    $(this).bind('click',{id:$(this).attr('id').split('_')[1]},function(e){
     	        qid=e.data.id;
     	        if(confirm('点击“确定”删除此问题，“取消”返回'))
     	        {
     	            
     	            var par='w=de&i='+qid;
     	            $.ajax({
     	                type:'POST',
     	                data:par,
     	                url:url,
     	                success:function(e){
     	                    $('#faq_'+qid).remove();
     	                },
     	                complete:function(){
     	                    qid=0;
     	                }
     	            });
     	            //数据库删除逻辑
     	            
     	        }
     	    });
     	});
     	$('.c_reply').each(function(){
     	    $(this).bind('click',{id:$(this).attr('id').split('_')[1]},function(e){
     	        $('#background').css({height:$(document).height(),width:'100%',display:'block'});
     	        var dialogoption={left:parseFloat($(document).width()/2-$('#dialog').width()/2),top:document.documentElement.scrollTop+200,display:'block'};
     	        $('#dialog').css(dialogoption);
     	        $(window).bind('scroll',function(){
     		        $('#dialog').stop();
                    $('#dialog').animate({top:document.documentElement.scrollTop+200},{duration:200,easing:'swing'});
     		    });
     		    qid=e.data.id;
     	    });//回复窗口开
     	});
     	$('.pager_item,.c_reply,.c_del,#ask').hover(
     	    function(){
     	        $(this).css('cursor','pointer');
     	    },
     	    function(){
     	        $(this).css('cursor','default');
     	    }
     	);
     	
     	 
///////////////////////////////////////////////////////////////////////
     },
     LoadPage:function(){
        this.StateSet();
        this.PageSizeSet();
        var parameter='w=gq&pa='+currentPage+'&ps='+pagesize;
        if(state!=-1){
            parameter+='&fi='+state;
        }
        $('#faq_container').html('<font id=\'loadingfont\' color=\'red\'  >数据加载中...</font><img id="loading"  src="../Images/loading.gif" />');
        $.ajax({
            type:'POST',
            data:parameter,
            url:url,
            error:function(e){
     	        alert(e);
     	    },
     	    success:function(e){
     	        if(e!='0')
     	        {
     	            var data=eval(e.split('|')[0]);
     	            questionIDCollection='';
     	            var o;
     	            totalrecord=parseInt(e.split('|')[1]);
     	            ex.CalcPage();
     	            
     	            for(var i=0 in data){
     	                o=data[i];
     	                questionIDCollection+=o.ID+',';
     	                //1
                        $('<div class="c_faq" id="faq_'+o.ID+'">').appendTo('#faq_container');
                        //2
                        $('<div class="c_handler">').appendTo('.c_faq:last');
                        $('<div class="c_q">').appendTo('.c_faq:last');
                        $('<div class="c_a">').appendTo('.c_faq:last');
                        //3 c_handler
                        $('<div class="c_come">').appendTo('.c_handler:last');
                        $('<div class="c_contact">').appendTo('.c_handler:last');
                        $('<div class="c_date">').appendTo('.c_handler:last');
                        if(isAdmin){
                            $('<div class="c_reply"  id="r_'+o.ID+'">').appendTo('.c_handler:last');
                            $('.c_reply:last').html('回复');
                            $('<div class="c_del" id="r_'+o.ID+'">').appendTo('.c_handler:last');
                            $('.c_del:last').html('删除');
                        }
                        if(o.NickName==''){
                            if(isAdmin){
                                $('.c_come:last').html('来自<span class="c_ip">'+o.IP+'</span>的网友');//带改正
                            }else{
                                var ips=o.IP.split('.');
                                var ip='';
                                for(var i=0;i<4;i++){
                                    if(i==2){
                                        ip+='.*';
                                    }else if(i==0){
                                        ip+=ips[i];
                                    }else{ ip+='.'+ips[i];}
                                }
                                $('.c_come:last').html('来自<span class="c_ip">'+ip+'</span>的网友');//带改正
                            }
                            
                        }
                        else{
                            $('.c_come:last').html('网友&nbsp;<span class="c_ip">'+o.NickName+'</span>');//带改正
                        }
                        if(isAdmin){
                            if(o.Contact.length>11){
                                $('.c_contact:last').html('联系方式：'+o.Contact.substring(0,11))
                            }
                            $('.c_contact:last').attr('title',o.Contact);
                        }
                        var date=o.AskDate;
                        date=eval('new '+date.replace('/','').replace('/',''));
                        $('.c_date:last').html(date.toLocaleString());
                        
                        
                        //3 c_q TODO 问题生成
                        $('<div class="c_q_img">').appendTo('.c_q:last');
                        $('<img alt="问" src="../Images/q.gif" />').appendTo('.c_q_img:last');
                        $('<div class="c_q_c" id="c_q_c_'+o.ID+'">').appendTo('.c_q:last');
                        $('#c_q_c_'+o.ID).html(o.Question);
                        //3 c_q TODO 答案界面生成
                        $('<div class="c_a_img">').appendTo('.c_a:last');
                        $('<img alt="答" src="../Images/a.gif" />').appendTo('.c_a_img:last');
                        $('<div class="c_a_c" id="c_a_c_'+o.ID+'">').appendTo('.c_a:last');
     	            }
     	        }
     	        else
     	        {
     	            $('#faq_container').html('暂无相关数据 您可以 <a href=\'#\'  onclick="$(\'#ask\').click(); "><font color=\'red\'>点击</font></a> 提问 &nbsp;或&nbsp;<a onclick="window.location.reload();" href="#"><font color=\'red\'>刷新</font></a>&nbsp;本页');
     	            $('#last,#next,#first,#pre,#all,#processed,#unprocessed,#ps_1,#ps_2,#ps_3').hide();
     	        }
     	    },
     	    complete:function(){
     	         $('#loadingfont').remove();
     	         $('#loading').remove();
     	         ex.EventAttacth();
     	         ex.GetAnswer();
     	    }
        });
        
        ///////////////////////////////////
     },
     GetAnswer:function(){//获取答案
            var par='w=ga&ic='+questionIDCollection;
            $.ajax({
                type:'POST',
                data:par,
                url:url,
                success:function(a){
                    if(a!=0){
                        var o;
                        var msg=eval(a);
                        for(var i=0 in msg){
                            o=msg[i];
                            var date=o.AnswerDate;
                           date=eval('new '+date.replace('/','').replace('/',''));
                           $('#r_'+o.QuestionID).remove();
                           $('#c_a_c_'+o.QuestionID).html(o.Answer+"<br/>&nbsp;&nbsp;"+date.toLocaleString());
                        }
                    }
                }
            });
     },
     CalcPage:function(){//计算分页
        if(totalrecord%pagesize>0){
            totalpage=parseInt((totalrecord/pagesize).toString().split('.')[0])+1;
        }else totalpage=totalrecord/pagesize;
        if(totalpage==currentPage){
            $('#last,#next').hide();
            $('#first,#pre').show();
        }
        if(currentPage==1){
            $('#first,#pre').hide();
            $('#last,#next').show();
        }
        if(totalpage==1){
            $('#last,#next,#first,#pre').hide();
        }
        if(currentPage<totalpage && currentPage>1){
            $('#last,#next,#first,#pre').show();
        }
        
     }
/////////////////////////////////////////////////
  };


