﻿var qid;

$(document).ready(function(){
    qid = $("#questionId").attr("class");
    
    if($("#tabChangeAnswer").length > 0)
    {
        aid = $("#hdfAnswerID").val();
        $("#tabChangeAnswer").appendTo($("#tdOutChangeAnswer"+aid));
        window.location.hash = "answer"+aid;
    }
    
    //上部回答问题
    $("#btnTopLetMeAnswer").click(function(){
        if($("#topAnswer").children("#tabAnswerArea").length == 0)
        {
            $("#tabAnswerArea").appendTo($("#topAnswer"));
            $("#tabAnswerArea").show();            
        }
        else
        {
            $("#tabAnswerArea").toggle();
        }
        return false;
    });    
    
    //下部回答问题
    $("#btnBottomLetMeAnswer").click(function(){
        if($("#bottomAnswer").children("#tabAnswerArea").length == 0)
        {
            $("#tabAnswerArea").appendTo($("#bottomAnswer"));
            $("#tabAnswerArea").show();            
        }
        else
        {
            $("#tabAnswerArea").toggle();
        }  
        return false; 
    });
    
    //取消回答
    $("#btnCancelAnswer").click(function(){
       $("#tabAnswerArea").toggle();
       return false;
    });
    
    //显示问题补充
    $("#btnShowQuestionAdd").click(function(){
        $("#tabQuestionAddArea").toggle();
        return false;
    });
    
    //隐藏问题补充
    $("#btnCancelAdd").click(function(){
        $("#tabQuestionAddArea").toggle();
        return false;
    });    
    
    //回答问题
    $("#lbtnSubmitAnswer").click(function(e){
        ea = e;
        _UserLogin(ea,"addAnswer();");
        return false;        
    });
    
    //关注问题
    $("#btnAttention").click(function(e){
        ea = e;
        _UserLogin(ea,"addAttention();");  
        return false;      
    });
    
    //悬赏
    $(".btn_xuanshang").click(function(e){
        ea = e;
        addscore_show(ea);
        return false;
    });
    
    //赞同
    $(".btnfeelgood").each(function(){
        $(this).click(function(e){
            ea = e;
            _UserLogin(ea,"addFeelGood('"+$(this).attr("id")+"');"); 
            return false;           
        });
    });
    
    //反对
    $(".btnfeelbad").each(function(){
        $(this).click(function(e){
            ea = e;
            _UserLogin(ea,"addFeelBad('"+$(this).attr("id")+"');"); 
            return false;           
        });
    });
    
    //投票
    $(".btn_tupiao").each(function(){
        $(this).click(function(e){
            ea = e;
            _UserLogin(ea,"addVoteAgree('"+$(this).attr("id")+"');"); 
            return false;           
        });
    });
    
    //收藏
    $("#btnFavoritesQuestion").click(function(e){
        ea = e;
        _UserLogin(ea,"addFavorite();");
        return false;    
    });
    
    //选择最佳答案
    $(".btnSetBestOut a").each(function(){
        $(this).click(function(e){
            ea = e;
            _UserLogin(ea,"setBestAnswer('"+$(this).attr("id")+"');");
            return false;    
        });
    });
});

//回答问题提交前检查
function addAnswer()
{
    V3.ID.BAGASite.WService.LogonService.CheckUserAnswer(qid,function(result){
        if(result == 1){
            alert("问题不存在！");
            return;
        }
        else if(result == 2){
            alert("提问人不能自己回答问题！");
            return;        
        }
        else if(result == 3)
        {
            alert("一个问题只能回答一次！");
            return;        
        }
        else if($("#txtAnswerContent").val().replace(/(^\s*)|(\s*$)/g, "") == "")
        {
            alert("回答不能为空");
            return;
        }
        else
        {
            __doPostBack('lbtnSubmitAnswer','');
        }
    },function(){});
}

//异步执行添加关注
function addAttention()
{
    V3.ID.BAGASite.WService.DataSubmit.AddAttention(qid,function(result){
        var oldcount = parseInt($("#attentionCountValue").html());
        if(result) {
            $("#attentionCountValue").html(oldcount+1);            
            alert("关注成功！");
        }else{
            alert("您已经对该问题添加过关注！");
        }
        $("#btnAttention").unbind("click");
        $("#btnAttention").css("cursor","default");        
    },function(){});
}

//异步执行赞同
function addFeelGood(oid)
{
    aid = oid.substr(11);
    V3.ID.BAGASite.WService.DataSubmit.AnswerFeel(qid,aid,"0",function(result){
        var oldcount = parseInt($("#feelgoodcount"+aid).html());
        if(result) {
            $("#feelgoodcount"+aid).html(oldcount+1);
            alert("支持成功！");
        }else{
            alert("您已经对该问题表示过赞同或反对！");
        }
        removeFeel(aid);
    },function(){});
}

//异步执行反对
function addFeelBad(oid)
{
    aid = oid.substr(10);
    V3.ID.BAGASite.WService.DataSubmit.AnswerFeel(qid,aid,"1",function(result){
        var oldcount = parseInt($("#feelbadcount"+aid).html());
        if(result){
            $("#feelbadcount"+aid).html(oldcount+1);
            alert("反对成功！");
        }else{
            alert("您已经对该问题表示过赞同或反对！");
        }
        removeFeel(aid);
    },function(){});
}

//去掉赞同反对的按钮功能
function removeFeel(aid)
{
    $("#btnfeelbad"+aid).unbind("click");
    $("#btnfeelbad"+aid).css("cursor","default");
    $("#btnfeelgood"+aid).unbind("click");
    $("#btnfeelgood"+aid).css("cursor","default");
}

//异步执行投票
function addVoteAgree(oid)
{
    aid = oid.substr(12);
    V3.ID.BAGASite.WService.DataSubmit.AnswerVote(qid,aid,function(result){
        if(result != "" ) alert(result);
        alert("投票成功！");
        window.location.href = window.location.href ;
    },function(){});
}

//异步悬赏积分
function addQuestionScore(score)
{
    if(score == "") score = "10";
    V3.ID.BAGASite.WService.DataSubmit.AddQuestionScore(qid,score,function(result){
        if(result){
            var oldscore = parseInt($("#questionScore").html());
            $("#questionScore").html(parseInt(score)+oldscore);
            $("#questionScore2").html(parseInt(score)+oldscore);
            if((parseInt(score)+oldscore) >= 50)
            {
                $("#scoreBackGround").attr("background","/images/wenti/tu_xuanshang.green.gif")
            }
            else
            {
                $("#scoreBackGround").attr("background","/images/wenti/tu_xuanshang.gif")            
            }
            alert("成功追加悬赏"+score+"积分！");
        }
        else
        {
            alert("您的积分不足！");
        }
    },function(){});
}

//添加收藏
function addFavorite()
{
    V3.ID.BAGASite.WService.DataSubmit.AddFavorite(qid,function(result){
        if(result){
            if($("#btnFavoritesQuestion img").attr("title") == "点击添加到收藏"){
                $("#btnFavoritesQuestion img").attr("title","点击取消收藏");
                $("#btnFavoritesQuestion img").attr("src","/images/wenti/tu_shoucang.gif");
            }else{
                $("#btnFavoritesQuestion img").attr("title","点击添加到收藏");
                $("#btnFavoritesQuestion img").attr("src","/images/wenti/tu_shoucang.kong.gif");            
            }
        }
    },function(){});
}

//选择最佳答案
function setBestAnswer(oid)
{
    aid = oid.substr(7);
    V3.ID.BAGASite.WService.DataSubmit.SetBestAnswer(qid,aid,function(result){
        if(result){
            window.location.reload();
        }
    },function(){});
}

