jQuery(document).ready(function(){
    // DOM 加载完毕时添加搜索提示
    searchTip();
    // 处理 Reply 和 Quote 链接
    commentButton();

});

// 添加搜索框提示
function searchTip() {
    $("input#s").css({backgroundColor:"transparent"});
    $("input#s").parent().css({position:"relative"}).append('<span id="searchtip" style="position:absolute; top:2px; left:10px; color:#999; display:none; z-index:-1;">' + $("input#s").attr("title") + '</span>');

    // 如果搜索框内容为空则显示提示
    if($("input#s").val() == "") {
        $("#searchtip").show();
    }

    // for IE transparent background color issue
    $("#searchtip").click(function(){$("input#s").trigger("focus");});
    // 当鼠标聚焦在搜索框
    $("input#s").focus(function(){
        // 如果搜索框内容为空则隐藏提示
        if($(this).val() == "") {
            $("#searchtip").hide();
        }
    }).blur(function(){
        // 如果搜索框内容为空则显示提示
        if($(this).val() == "") {
            $("#searchtip").show();
        }
    });

    // 当搜索表单提交时
    $("#searchform").submit(function(){
        // 如果搜索框内容为空则不提交
        if($("input#s").val() == "") {
            return false;
        }
        return true;
    });
}

// 给 Reply 和 Quote 链接添加点击事件
function commentButton() {
    // Reply 链接
    $("a.reply").click(function(){
        var parentli = $(this).parent("p").parent("li");
        var str = $("textarea#comment").val();
        var insertStr = '<a href="#'+parentli.attr("id")+'">@'+$(this).attr("title")+' </a>';
        // 如果一次评论中重复回复, 退出操作
        if(str.indexOf(insertStr) > -1) {
            return true;
        }
        // 如果输入框内无内容，将输入框内容设置为需要追加的字符串
        if($.trim(str) == '') {
            //$("textarea#comment").html(insertStr + "\n");
            $("textarea#comment").val(insertStr + "\n");
        } else {
            $("textarea#comment").val($.trim(str) + "\n\n" + insertStr + "\n");
        }
        $("input#comment_parent").val(parentli.attr("id").replace("comment-",""));
        $("textarea#comment").focus();
    });

    // quote 链接
    $("a.quote").click(function(){
        var parentli = $(this).parent("p").parent("li");
        var id = parentli.attr("id").replace("comment-","");
        var author = $("a.reply", parentli).attr("title");
        var str = $("textarea#comment").val();
        var insertStr = '<blockquote cite="#commentbody-'+id+'"><strong><a href="#comment-'+id+'">'+author+'</a> : </strong><br />'+$(this).attr("title")+'</blockquote>';
        // 如果一次评论中重复回复, 退出操作
        if(str.indexOf(insertStr) > -1) {
            return true;
        }
        // 如果输入框内无内容，将输入框内容设置为需要追加的字符串
        if($.trim(str) == '') {
            $("textarea#comment").val(insertStr + "\n");
        } else {
            $("textarea#comment").val($.trim(str) + "\n\n" + insertStr + "\n");
        }
        $("textarea#comment").focus();
    });
}
