/**
 * Created by LGH on 2017/4/12.
 */
function delPrdCodeFromCompare(productCode) {
    var compareProductCodes = $.cookie("rk-com-pc");
    var newCompareProductCodes = "";
    var compareProductCodeArray = compareProductCodes.split("-");
    compareProductCodeArray.splice($.inArray(productCode + "", compareProductCodeArray), 1);
    if (compareProductCodeArray.length > 0) {
        $.each(compareProductCodeArray, function (index, value) {
            newCompareProductCodes = newCompareProductCodes + value + "-";
        });
    }
    if (newCompareProductCodes) {
        newCompareProductCodes = newCompareProductCodes.substring(0, newCompareProductCodes.length - 1);
    } else {
        $(".pop-compare").hide();
        $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
    }
    $.cookie("rk-com-pc", newCompareProductCodes, {expires: 7, path: '/'});
    $(".compare" + productCode).text("加入对比");
    $(".compare" + productCode).removeClass("active");
    $(".compare" + productCode).removeAttr("style");
    initPrdCodeCompare();
};

function initPrdCodeCompare() {
    var globalPrdCompareIdentify = $.cookie("globalPrdCompareIdentify");
    if (!globalPrdCompareIdentify) {
        $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
    }
    if ($.cookie("globalPrdCompareIdentify") == "true") {
        $(".pop-compare").show();
    } else {
        $(".pop-compare").hide();
    }
    /* $(".comparePrdCodes").text("加入对比");*/
    var initCompareProductCodes = $.cookie("rk-com-pc");
    if (initCompareProductCodes) {
        var initCompareProductCodeArray = initCompareProductCodes.split("-");
        if (initCompareProductCodeArray.length > 0) {
            $.each(initCompareProductCodeArray, function (index, value) {
                $(".compare" + value).html("已<br/>添加");
                $(".compare" + value).addClass("active");
            });
        }
    }
    initBottomPrdCompare();
};

function initBottomPrdCompare() {
    //此数组的内容仅仅是个标记，只要是3个元素就可以
    var comparedItemArray = new Array("compareProduct0", "compareProduct1", "compareProduct2");
    var hasItemCount=0;
    var initCompareProductCodes = $.cookie("rk-com-pc");
    if (initCompareProductCodes) {
        var initCompareProductCodeArray = initCompareProductCodes.split("-");
        hasItemCount = initCompareProductCodeArray.length;
        if (initCompareProductCodeArray.length > 0) {
            $.each(initCompareProductCodeArray, function (index, value) {
                $.ajax({
                    url: "/queryProductBaseInfo/" + value,//要请求的服务器url
                    data: {},
                    async: true,   //是否为异步请求
                    cache: false,  //是否缓存结果
                    type: "POST", //请求方式为POST
                    dataType: "json",   //服务器返回的数据是什么类型
                    success: function (data) {  //这个方法会在服务器执行成功是被调用 ，参数result就是服务器返回的值(现在是json类型)
                        var online = data.online;
                        var productInfo;
                        if (online) {
                            productInfo = data.product;
                            var text = "<dt> <a target='_blank' href='/" + $.cookie("cityJianpin") + "/taocan/" + productInfo.code + "'> <img src='" + productInfo.imagePath + "' width='160' height='107'> </a> </dt>";
                            text = text + "<dd> <a class='diff-item-name'>" + productInfo.name + "</a> <p class='p-price'>&yen;<span class='f20'>" + productInfo.decimalSalesPrice + "</span></p> </dd>";
                            text = text + "<span class='close-btn' data-value='" + value + "'></span>";
                            $("#compareProduct" + index).html(text);
                            $("#compareProduct" + index).attr("class", "hasItem");
                        } else {
                            var wrongData = data.wrongData;
                            var text;
                            if (wrongData) {
                                text = "<dd> <a class='diff-item-name c-c3'>此条数据有误，请删除</a> </dd>";
                                text = text + "<span class='close-btn' data-value='" + value + "'></span>";
                                $("#compareProduct" + index).html(text);
                                $("#compareProduct" + index).attr("class", "hasItem");
                            } else {
                                productInfo = data.product;
                                text = "<dt> <a target='_blank' href='/" + $.cookie("cityJianpin") + "/taocan/" + productInfo.code + "'> <img src='" + productInfo.imagePath + "' width='160' height='107'> </a> </dt>";
                                text = text + "<dd> <a class='diff-item-name c-c3'>" + productInfo.name + "</a> <p class='p-price'>&yen;<span class='f20'>已下架</span></p> </dd>";
                                text = text + "<span class='close-btn' data-value='" + value + "'></span>";
                                $("#compareProduct" + index).html(text);
                                $("#compareProduct" + index).attr("class", "hasItem");
                            }
                        }
                    }
                });
            });
        }
    }
    $.each(comparedItemArray, function (index, value) {
        if (index < hasItemCount) {
            return;
        }
        $("#compareProduct" + index).html("<span class='num'>"+(index+1)+"</span><div>最多可同时对比3个体检套餐您可继续添加</div>");
        $("#compareProduct" + index).attr("class", "item-empty");
    });
};

function jumpToUrl(sex) {
    var compareProductCodes = $.cookie("rk-com-pc");
    var compareProductCodeArray = compareProductCodes.split("-");
    if ((compareProductCodeArray.length > 1) && (compareProductCodeArray.length < 4)) {
        $.goToUrl("/taocan/productCompare/" + sex + "/" + compareProductCodes, true);
    }
};
$(document).ready(function () {
    initPrdCodeCompare();
    $(".comparePrdCodes").click(function () {
        tempNumber = 1;
        var productCode = $(this).parent().parent().parent().data("value") + "";
        if (!productCode || typeof(productCode) == "undefined" || (productCode == "undefined")) {
            productCode = $(this).data("value") + "";
        }
        if (!productCode || typeof(productCode) == "undefined") {
            return;
        }
        var compareProductCodes = $.cookie("rk-com-pc");
        var compareProductCodeArray = [];
        if (compareProductCodes) {
            $.cookie("globalPrdCompareIdentify",true,{ expires: 7, path: '/' });
            compareProductCodeArray = compareProductCodes.split("-");
            if (compareProductCodeArray.length > 3) {
                compareProductCodeArray = compareProductCodeArray.slice(0, 3);
            } else if (compareProductCodeArray.length == 3) {
                if ($.inArray(productCode, compareProductCodeArray) >= 0) {
                    delPrdCodeFromCompare(productCode);
                } else {
                    $(this).next().show(300).delay(1000).hide(300);
                    //$(".com-item-btn").show(300).delay(1000).hide(300);
                }
            } else {
                if ($.inArray(productCode, compareProductCodeArray) < 0) {
                    compareProductCodes = compareProductCodes + "-" + productCode;
                    $.cookie("rk-com-pc", compareProductCodes, {expires: 7, path: '/'});
                    initPrdCodeCompare();
                    $(this).text("取消");
                    $(this).css("line-height", "36px");
                } else {
                    delPrdCodeFromCompare(productCode);
                }
            }
        } else {
            $.cookie("rk-com-pc", productCode, {expires: 7, path: '/'});
            initPrdCodeCompare();
            $(this).text("取消");
            $(this).css("line-height", "36px");
            $(".pop-compare").show();
            $.cookie("globalPrdCompareIdentify", true, {expires: 7, path: '/'});
        }
    });
    $(".comparePrdCodes").hover(function () {
        if ($(this).text() == "已添加") {
            $(this).text("取消");
            $(this).css("line-height", "36px");
        }
    }, function () {
        if ($(this).text() == "取消") {
            $(this).html("已<br/>添加");
            $(this).removeAttr("style");
        }
    });
    $("#startCompareButton").click(function () {
        initPrdCodeCompare();
        var compareProductCodes = $.cookie("rk-com-pc");
        var compareProductCodeArray = compareProductCodes.split("-");
        if (compareProductCodeArray.length > 1) {
            $(".sel-type").show();
        }
    });
    $(".sel-type-cbtn").click(function () {
        $(".sel-type").hide();
    });
    $(".hide-me").click(function () {
        initPrdCodeCompare();
        $(".pop-compare").hide();
        $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
    });
    $(".show-me").click(function () {
        initPrdCodeCompare();
        var globalPrdCompareIdentify = $.cookie("globalPrdCompareIdentify");
        if (!globalPrdCompareIdentify) {
            $(".pop-compare").hide();
            $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
        }
        if ($.cookie("globalPrdCompareIdentify") == "true") {
            $(".pop-compare").hide();
            $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
        } else {
            $(".pop-compare").show();
            $.cookie("globalPrdCompareIdentify", true, {expires: 7, path: '/'});
        }
    });
    $(".diff-items").delegate(".close-btn", "click", function () {
        var productCode = $(this).data("value");
        if (productCode) {
            delPrdCodeFromCompare(productCode);
        }
    });
    $(".del-items").click(function () {
        var compareProductCodes = $.cookie("rk-com-pc");
        var compareProductCodeArray = compareProductCodes.split("-");
        if (compareProductCodeArray.length > 0) {
            $.each(compareProductCodeArray, function (index, value) {
                $(".compare" + value).text("加入对比");
                $(".compare" + value).removeClass("active");
            });
        }
        $.cookie("rk-com-pc", "", {expires: 7, path: '/'});
        $.cookie("globalPrdCompareIdentify", false, {expires: 7, path: '/'});
        initPrdCodeCompare();
    });
    //多余显示省略号
    $(".diff-item-name").each(function () {
        var len = $(this).text().length;   //当前HTML对象text的长度
        if (len > 14) {
            var str = "";
            str = $(this).text().substring(0, 13) + "...";  //使用字符串截取，获取前13个字符，多余的字符使用“......”代替
            $(this).html(str);                   //将替换的值赋值给当前对象
        }
    });
});