﻿var compare_products;
var product_name = "产品";
var csstop = 70;
var cssleft = 30;
var FolderName = "";
var num;
$(document).ready(function() {

    //==有时间改成字符截取
    if (document.location.href.indexOf("DC") > 0) {
        FolderName = "DC";
    }
    if (document.location.href.indexOf("NB") > 0) {
        FolderName = "notebook";
    }
    if (document.location.href.indexOf("Print") > 0) {
        FolderName = "Print";
    }
    if (document.location.href.indexOf("AllInOne") > 0) {
        FolderName = "AllInOne";
    }
    if (document.location.href.indexOf("CPUDesktop") > 0) {
        FolderName = "cpudesktop";
    }
    if (document.location.href.indexOf("CPUMobile") > 0) {
        FolderName = "CPUMobile";
    }
    if (document.location.href.indexOf("GC") > 0) {
        FolderName = "GC";
    }
    if (document.location.href.indexOf("MB") > 0) {
        FolderName = "MB";
    }
    num = "" + Math.round(Math.random() * 10000);
    $.ajax({
        type: "GET",
        data: "action=init&num=" + num, //data
        dataType: "text",
        error: function(data) {
            alert("error : " + data);
        },
        success: function(data)	//callback
        {
            compare_products = $.evalJSON(data);
            _init_compare_list();
            $("#drag1").css("top", csstop);
            $("#drag1").css("left", cssleft);
            init();
        }
    });
    $("#drag1").scrollFollow({
        offset: 100
    });
});

function _init_compare_list() {
    $("#drag1 > div").each(function(i) {
        if (i >= 1) {
            $(this).remove();
        }
    });
    $("#drag1").height(20);
    for (var i = 0; i < 4; i++) {
        if (compare_products.Select[i] != undefined && compare_products.Select[i] != null) {
            var id = compare_products.Select[i].id;
            if (id != undefined && id != null && id != '') {
                var name = compare_products.Select[i].name;
                var pic_path = compare_products.Select[i].image_path;
                if (pic_path.indexOf("~") >= 0) {
                    pic_path = pic_path.replace("~", "..");
                }
                _addToCompareList(id, name, pic_path, false);
            }
        }
    }
}

function _insert_into_compare_list(pre) {
    var productID = $("#" + pre + "hidden_id").val();
    if ($("#" + productID).attr("id") != undefined) {
        alert("此款" + product_name + "已在比较列表中");
        return false;
    }

    if ($("#drag1 > div").length == 5) {
        alert("最多只能添加4款" + product_name);
        return false;
    }
    var manufacture = $("#" + pre + "Manufacturer").text();
    var model = $("#" + pre + "lbtn" + "Name").text();
    var pic_path = $("#" + pre + "hidden_pic_path").val();
    var name = manufacture + model;

    //对图片路径进行调整
    pic_path = pic_path.replace("~/", "../../");
    num = "" + Math.round(Math.random() * 10000);
    $.ajax({

        type: "GET",
        data: "action=add&ID=" + productID + "&Name=" + escape(name) + "&PicPath=" + escape(pic_path) + "&num=" + num,
        error: function(textStatus, errorThrown) {
            alert("error1" + textStatus);
        },
        success: function(data, textStatus) {
            _addToCompareList(productID, name, pic_path);
        }
    });
    return false;
}

function _addToCompareList(id, name, pic_path, _init) {
    if (name.length <= 18) {
        if ($("#drag1 > div").length == 1) {
            $("#drag1").height($("#drag1").height() + 120);
        }
        else
            $("#drag1").height($("#drag1").height() + 120);
    }
    else {
        if ($("#drag1 > div").length == 1) {
            $("#drag1").height($("#drag1").height() + 120);
        }
        else
            $("#drag1").height($("#drag1").height() + 128);
    }
    $("#drag1").append("<div id ='" + id + "'></div>");

    $("<a></a>").attr("href", "../../" + FolderName + "/detail/" + id).text(name).appendTo("#" + id);
    //添加图片
    $("#" + id).append("<input id=\"img_" + id + "\" class = \"_image\" type=\"image\" src=\"" + pic_path + "\"/>");
    $("#img_" + id).click(function() {
        //alert(pID);
        javascript:
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(pic_path, "", false, "", "../../" + FolderName + "/detail/" + id, false, false));
    });
    //增加相框的长度和添加删除按钮
    $("#" + id).append("<input id=\"del_" + id + "\" class = \"_del\" type = \"image\" src=\"../../images/icon_chip/close.gif\"/>");
    if (_init == undefined || _init == false)
        $("#del_" + id).click(function() {
            //删除
            qs = "action=delete&ID=" + id;
            num = "" + Math.round(Math.random() * 10000);
            $.ajax({
                type: "GET",
                data: qs + "&num=" + num,
                error: function(textStatus, thrown) {
                    alert("del1 error:" + textStatus);
                },
                success: function(data, textStatus) {
                    //alert(id);
                    $("div").remove("#" + id);
                    //                _delete_product(id);
                    if (name.length <= 18) {
                        if ($("#drag1 > div").length == 2) {
                            $("#drag1").height($("#drag1").height() - 125);
                        }
                        else
                            $("#drag1").height($("#drag1").height() - 105);
                    }
                    else {
                        if ($("#drag1 > div").length == 2) {
                            $("#drag1").height($("#drag1").height() - 135);
                        }
                        else
                            $("#drag1").height($("#drag1").height() - 115);
                    }
                    init();
                }
            });
            return false;
        });
    return false;
}
function clear() {
}
function _dragToCompareList(ev, ui) {
    //获取image的id和src
    var imgId = $(ui.draggable).children("input").attr("id");
    //获取image name链接的id
    var i = imgId.lastIndexOf('_');
    var pre = imgId.substring(0, imgId.lastIndexOf('_') + 1);
    _insert_into_compare_list(pre);
    return false;

}

function init() {
    $("#drag1").draggable({
        zIndex: 100000,
        snapTolerance: 10,
        stop: function(e, ui) {
            var offset = $("#drag1").offset();
            var compareListOffset = "top:" + csstop + ",left:" + cssleft;
        }
    });
    $("#drag1").droppable({
        accept: '.draggable2',
        tolerance: 'touch',
        drop: _dragToCompareList
    });
    $(".draggable2").draggable({
        cancel: 'none',
        helper: 'clone',
        zIndex: 100000
    });
}
//点击添加按钮
function clickAddButton(el) {
    $("#drag1").css('display', 'block');
    var aId = el.id;
    //aId中最后一个"_"的位置
    var i = aId.lastIndexOf('_');
    //获取id的前半部分
    var pre = aId.substring(0, i + 1);
    _insert_into_compare_list(pre);
    return false;
}

//点击比较按钮
function compare2() {
    //	alert("compare2");
    var allItems = $("#drag1 > div");
    if (allItems.length <= 1) {
        alert("请选择要比较的" + product_name);
        return false;
    }
    else if (allItems.length > 5) {
        alert("进行比较" + product_name + "数不能超过4个");
        return false;
    }
    var qs = "";
    for (var i = 1; i < allItems.length; i++) {
        var str = $(allItems[i]).attr("id");
        qs += "&" + "product" + i + "=" + str;
    }
    num = "" + Math.round(Math.random() * 10000);
    $.ajax({

        type: "GET",
        data: "action=compare" + qs + "&num=" + num,
        async: true,
        error: function(textStatus, thrown) {
            alert("Error1 : " + textStatus);
        },
        success: function(data, textStatus) {
            document.location = "Comparese.aspx";
        }
    });
}

function mouseOnAction(el) {
    $(el).draggable("destroy");
    $(el).draggable({
        cancel: 'none',
        helper: 'clone',
        zIndex: 100
    });
}

function clear1() {
    qs = "action=clear";
    num = "" + Math.round(Math.random() * 10000);
    $.ajax({
        type: "GET",
        data: qs + "&num=" + num,
        async: true,
        dataType: "text",
        error: function(textStatus, thrown) {
            alert("del2 error:" + textStatus);
        },
        success: function(data, textStatus) {
            $("#drag1 > div").each(function(i) {
                if (i >= 1) {
                    $(this).remove();
                }
            });
            $("#drag1").height(20);
            return false;
        }
    });
    return false;
}

function closefloat() {
    $("#drag1").css('display', 'none');
}