$(function () {
    //fancybox插件
    $("a[rel='fancybox']").fancybox({
        'titlePosition': 'over',
        'imageScale': true,
        'overlayOpacity': '0.2',
        'overlayColor': '#0daad3',
        'cyclic': true,
        'centerOnScroll': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true,
        'autoScale': true,
        'hideOnContentClick': true,
        'hideOnOverlayClick': true,
        'showNavArrows': true,
        'enableEscapeButton': true,
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over"><span class="V">[' + (currentIndex + 1) +
'/' + currentArray.length + (title.length ? ']</span> ' + title : '') + '</span>';
        }
    });

    //提交评论
    $("#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: 'addproductreview', 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: 'getproductreview', 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;
    });

    //下订单
    $("#productOrder_btn").click(function () {
        var obj = $(this);
        if ($("#productOrder_text").val() != "") {
            var reg = /^[\d|\.|,]+$/;
            if (!reg.test($("#productOrder_text").val())) {
                alert("数量输入必须为数字！");
            } else {
                $.ajax({
                    type: "POST",
                    url: "/Ajax/DataDispose.ashx",
                    data: { com: 'addproductorder', naviid: obj.attr("naviid"), number: $("#productOrder_text").val(), productid: obj.attr("href") },
                    datatype: "json",
                    beforeSend: function (data, textStatus) {
                        obj.attr("disabled", true);
                    },
                    success: function (data, textStatus) {
                        if (data != "") {
                            switch (data) {
                                case "0":
                                    alert("下单失败！");
                                    break;
                                case "1":
                                    alert("您已成功下单，请到会员中心产品订单处查看详情！");
                                    $("#productOrder_text").val("");
                                    break;
                                case "2":
                                    alert("产品下单功能已关闭，请联系管理员开启！");
                                    break;
                                case "3":
                                    alert("您还没有登录，请先登录后再操作！");
                                    location.href = obj.attr("loginurl");
                                    break;
                            }

                            obj.attr("disabled", false);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
                })
            }
        } else {
            alert("请输入订购数量！");
        }
        return false;
    })
})