$("#sendremark").hover(function()
{
    $(this).css("background-color", "#EFDFFD");
}, function()
{
    $(this).css("background-color", "#D3EAF0");
})
.tooltip();


$('#remark').autoResize(
{   // 文本框改变大小时触发事件，这里改变了文本框透明度
    onResize : function(){
        $(this).css({
            opacity:0.8
        });
    },
    animate: true,
    // 动画效果回调触发事件，这里改变了文本框透明度
    animateCallback : function(){
        $(this).css({
            opacity:1
        });
    },
    //动画效果持续时间（ms），默认150
    animateDuration : 300,
    // 每次改变大小时，扩展（缩小）的高度（像素），默认20
    extraSpace : 30,
    //当文本框高度大于多少时，不再扩展，出现滚动条，默认1000
    limit: 500
})
.tooltip();



/*
 * 加载评论
 */
function load_remark()
{
    var $remark_view    = $("#remarklistbox");
    var news_comment_url= "/news/comment/"+newsid+"/";
    $.post(news_comment_url,function(data)
    {
        var error   = data.error;
        var errormsg= data.errormsg;
        var txtalert= data.alert;
        var txthtml = data.content;
        if (error != 0)
        {
            //alert("加载评论失败:\n" + error + "\n" +errormsg);
        }
        if ("" == txthtml)
            txthtml = "暂无评论...<br />快来抢沙发啊!"
        $remark_view.html(txthtml);

    },"json")
}
$(function(){load_remark();});



//读取Cookie
$(function()
{
    if ($.cookie('username'))
        $("#username").val($.cookie('username'));
});
/*
 * 发表评论
 */
$("#sendremark").click(function()
{
    var $name   = $("#username");
    var $remark = $("#remark");
    if ( "" == $remark.val() )
    {
        alert("请输入您要发表的评论!");
        return false;
    }
    if ("" == $name.val())
    {
        $name.val("匿名人士");
    }else
    {
        $.cookie('username',$name.val(),{
            expires:30,
            path:"/",
            domain:site_host
        });
    }
    //提交数据
    $.post("/news/remark/"+newsid+"/",{
        user:$name.val(),
        content:$remark.val(),
        quote:0
    },function(data)

    {
        var error   = data.error;
        var errormsg= data.errormsg;
        var txtalert= data.alert;
        var txthtml = data.content;
        if (error != 0)
        {
            $("#remarkmsg").html(errormsg+"!").show();
            //alert(errormsg+"!");
        }else
        {
            $("#remarkmsg").html("评论发表成功!\n感谢您的支持!").show();
            $("#remark").val("");
            //重新加载评论;
            load_remark();
        }
    },"json");
});


function mark_support(supobj, comment_id, act, num)
{
    var supurl  ="/news/";
    num++;
    var newstr  = "(" +num+")";
    var msgbox  = "#re_msg"+comment_id;
    if ("s" == act)
    {
        supurl  += "sremark/";
        newstr  =  "支持"+newstr;
    }
    else
    {
        supurl  += "aremark/";
        newstr  =  "反对"+newstr;
    }
    supurl  += comment_id+"/";

    $.post(supurl, function(data)
    {
        var error   = data.error;
        var errormsg= data.errormsg;
        var txtalert= data.alert;
        var txthtml = data.content;
        if (error != 0)
        {
            $(msgbox).html(errormsg+", 感谢您的支持!").show();
        }else
        {
            $(supobj).html(newstr);
            $(msgbox).html("感谢您的支持!").show();
        }
    },"json");
}

function mark_report(comment_id)
{
    var supurl  ="/news/report/"+comment_id;
    $.post(supurl, function()
    {
        $("#re_msg"+comment_id).html("我们已经记录下您所举报的评论,感谢您的支持!").show();
    },"json");
}

/*
 * 引用功能
 */
function mark_quote(comment_id, qluo, qusername)
{
    mark_quote_close();
    var newusername = "";
    if ($.cookie('username'))
        newusername = $.cookie('username');
    else
        newusername = "匿名人士";

    var qstr    = "";
    qstr    += '<dd id="remarkq">';
    qstr    += '<div id="remarkeditboxq">';
    qstr    += '<span id="quotenote">引用 '+qluo+'楼 ['+qusername+'] 的评论:</span>';
    qstr    += '<textarea name="remarktxtq" id="remarktxtq" rows="4" cols="60"></textarea>';
    qstr    += '<span class="span_boxq">';
    qstr    += '<input id="quote" value="'+comment_id+'" type="hidden" />';
    qstr    += '<span class="usernameqnote">您的大名:</span>';
    qstr    += '<input id="usernameq" value="'+newusername+'" />';
    qstr    += '</span>';
    qstr    += '<span id="sendremarkq" onclick="javascript:mark_quote_send();">发表评论</span>';
    qstr    += '<span id="sendremarkqx" onclick="javascript:mark_quote_close();">取消</span>';
    qstr    += '</div>';
    qstr    += '</dd>';

    $("#remarkid"+comment_id).append(qstr);
}

/*
 * 关闭引用
 */
function mark_quote_close()
{
    $("#remarkq").remove();
}

/*
 * 发表引用的评论
 */
function mark_quote_send()
{
    var quote   = $("#quote").val();
    var username= $("#usernameq").val();
    var content = $("#remarktxtq").val();
    if (!username)
    {
        $("#usernameq").focus();
        alert("请输入您的大名!");
    }else if (!content)
    {
        $("#remarkq").focus();
        alert("请输入您要发表的内容");
    }else
    {
        $.cookie('username',username,{
            expires:30,
            path:"/",
            domain:site_host
        });
        //发表
        $.post("/news/remark/"+newsid+"/",{
            user:username,
            content:content,
            quote:quote
        },function(data)

        {
            var error   = data.error;
            var errormsg= data.errormsg;
            var txtalert= data.alert;
            var txthtml = data.content;
            if (error != 0)
            {
                $("#remarkmsg").html(errormsg+"!").show();
                alert(errormsg+"!");
            }else
            {
                $("#remarkmsg").html("评论发表成功!\n感谢您的支持!").show();
                $("#remark").val("");
                //重新加载评论;
                load_remark();
            }
        },"json");
    }

}