$(function () {
    //提交评论
    $("#review_btn").click(function () {
        var obj = $(this);
        if ($("#review_text").val() != "") {
            if ($("#review_text").val().length > 6) {
                $.ajax({
                    type: "POST",
                    url: "/Ajax/DataDispose.ashx",
                    data: { com: 'addarticlereview', naviid: obj.attr("naviid"), toid: obj.attr("toid"), reviewcontent: $("#review_text").val(), mid: obj.attr("mid") },
                    datatype: "json",
                    beforeSend: function (data, textStatus) {
                        obj.attr("disabled", true);
                    },
                    success: function (data, textStatus) {
                        switch (data) {
                            case "0":
                                alert("评论失败！");
                                break;
                            case "1":
                                alert("评论成功！请等待管理员审核！");
                                $("#review_text").val("");
                                break;
                            case "2":
                                alert("评论功能已关闭，请联系管理员开启！");
                                break;
                        }

                        obj.attr("disabled", false);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
                })
            } else {
                alert("评论内容不能少于6个字节");
            }
        } else {
            alert("评论内容不能为空！");
        }
    });

    //初始数据
    var pageIndexNumber = 0;
    showReview(pageIndexNumber + 1);

    //显示评论
    function showReview(pageIndex) {
        $.ajax({
            type: "POST",
            url: "/Ajax/DataDispose.ashx",
            data: { com: 'getarticlereview', page: pageIndex, toid: $("#review_btn").attr("toid") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                $("#reviewNumber").html("loading…");
                $("#loadReviewBtn").val("评论加载中");
                $("#loadReviewBtn").attr("disabled", true);
                $("#loadReviewBtn").attr("class", "grey_btn2 W");
            },
            success: function (data, textStatus) {
                var ShowHtml = "";

                //拆分Josn数据
                var JosnData = eval(data.split('|||')[0]);
                if (JosnData != null) {
                    for (var i = 0; i < JosnData.length; i++) {
                        ShowHtml += "<li>";
                        ShowHtml += "<div class=\"head\"><img class=\"img\" src=\"/Images/review-head.gif\" /></div>";
                        ShowHtml += "<div class=\"content\">";
                        ShowHtml += "<span class=\"Y B\">" + JosnData[i].nickname + "</span><span class=\"Q\">( " + JosnData[i].ipcity + " - " + JosnData[i].ip + " )</span>";
                        ShowHtml += "<div class=\"info V line18\">";
                        ShowHtml += JosnData[i].content;
                        ShowHtml += "</div>";
                        ShowHtml += "</div>";
                        ShowHtml += "<div class=\"time\">";
                        ShowHtml += "<span class=\"G\">(" + JosnData[i].time1 + ")</span><br />";
                        ShowHtml += "<span class=\"V Size9\">" + JosnData[i].time2 + "</span><br />";
                        ShowHtml += "</div>";
                        ShowHtml += "<div class=\"clear-0\"></div>";
                        ShowHtml += "</li>";
                    }
                } else {
                    ShowHtml += "<li>暂无记录</li>";
                }
                $("#reviewListBox").append(ShowHtml);
                $("#reviewNumber").html(" (" + data.split('|||')[1] + ") ");

                $("#loadReviewBtn").val("加载更多");
                $("#loadReviewBtn").attr("disabled", false);
                $("#loadReviewBtn").attr("class", "grey_btn W");
                pageIndexNumber++;
                //判断是否最后一页
                if ($("#reviewListBox li").length >= data.split('|||')[1]) {
                    $("#loadReviewBtn").hide();
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
        })
    }

    //加载评论
    $("#loadReviewBtn").click(function () {
        showReview(pageIndexNumber + 1);
        return false;
    });
})