﻿function mritem(obj, height) {
    $(obj).find("ul:first").animate({
        marginLeft: "-" + height + "px"
    }, 500, function () {
        $(this).css({ marginLeft: "0px" }).find("li:first").appendTo(this);
    });
}
var myimg = setInterval('mritem("#header_guild","100")', 5000);
$(function () {
    menu(document.URL),
    $("#keywords").focus(function () {
        if ($(this).val() == "输入商品名称或关键字进行搜索") {
            $(this).addClass("input_sel");
            $(this).val("");
        }
    }),
    $("#keywords").blur(function () {
        if ($(this).val() == "") {
            $(this).removeClass("input_sel");
            $(this).val("输入商品名称或关键字进行搜索");
        }
    }),
    myimg,
    $("#header_guild").hover(
    function () { clearInterval(myimg); },
    function () {
        myimg = setInterval('mritem("#header_guild","100")', 5000)
    })
})
var menu = function (url) {
    var file = url.split("/");
    var menuli = $("#header_menu li");
    if (file[3] == "" || file[3] == "Default.aspx") { $(menuli[0]).find("a").addClass("sel"); }
    else if (file[3].toLowerCase().indexOf("news") != -1) { $(menuli[1]).find("a").addClass("sel"); }
    else if (file[3].toLowerCase().indexOf("pro") != -1) { $(menuli[2]).find("a").addClass("sel"); }
    else if (file[4] && file[4].toLowerCase().indexOf("info-9") != -1) { $(menuli[3]).find("a").addClass("sel"); }
    else if (file[4] && file[4].toLowerCase().indexOf("info-5") != -1) { $(menuli[4]).find("a").addClass("sel"); }
    else if (file[4] && file[4].toLowerCase().indexOf("info-2") != -1) { $(menuli[5]).find("a").addClass("sel"); }
    else if (file[4] && file[4].toLowerCase().indexOf("info-10") != -1) { $(menuli[7]).find("a").addClass("sel"); }
    else { $(menuli[0]).find("a").addClass("sel"); }
}

var prosearch = function () {
    if ($("#keywords").val() == "输入商品名称或关键字进行搜索" || $("#keywords").val() == "") {
        alert("请输入要搜索的产品名称或关键字！");
        return false;
    }
}

var buypro = function (id) {
    $.get("/control/Buy.ashx?id=" + id + "&" + Math.random(), function (data) {
        var item = data.split("|");
        alert(item[0]);
        if (item[1] != 0) {
            $(".carcount").html(item[1]);
            $("#spcart").html(item[2]);
        }
    });
}

var buynow = function (id, count) {
    $.get("/control/Buy.ashx?id=" + id + "&count=" + count + "&" + Math.random(), function (data) {
        var item = data.split("|");
        alert(item[0]);
        if (item[1] != 0) {
            $(".carcount").html(item[1]);
            $("#spcart").html(item[2]);
        }
    });
}

var keeppro = function (id) {
    $.get("/control/Keep.ashx?id=" + id + "&" + Math.random(), function (data) {
        alert(data);
    });
}
var updatepro = function (ProductT, CostPrice, SKU) {
    var ptotal = $("#num" + SKU).val();
    if (ptotal == ProductT) {
        alert("无更新操作！"); return;
    }
    $.get("/control/carter.ashx",
    { ProductTotal: ptotal, ProductT: ProductT, CostPrice: CostPrice, SKU: SKU, at: 'update', Math: Math.random() },
    function (data) {
        var item = data.split("|");
        alert(item[0]);
        if (item[1] != 0) {
            /*$("#sTotalPrice").html("产品总价：￥" + item[1]);
            var TotalPrice = parseFloat($("#num" + SKU).val()) * CostPrice;
            $("#sp" + SKU).html("￥" + TotalPrice.toFixed(2));
            $("#spcart").html(item[1]);*/
            window.open("/ShoppingCarts.aspx?" + Math.random());
        }
    });
}
var deletepro = function (ProductT, CostPrice, SKU) {
    if (confirm("确定要删除？")) {
        $.get("/control/carter.ashx",
        { ProductTotal: $("#num" + SKU).val(), ProductT: ProductT, CostPrice: CostPrice, SKU: SKU, at: 'delete', Math: Math.random() },
        function (data) {
            var item = data.split("|");
            alert(item[0]);
            if (item[1] != 0) {
                $("#tr" + SKU).hide();
                $("#sTotalPrice").html("产品总价：￥" + item[2]);
                $("#spcart").html(item[2]);
                $(".carcount").html($($(".carcount")[0]).text() - 1);
            }
        });
    }
}
var selall = function () {
    $("#prolist input[type=checkbox]").attr("checked", 'true');
}
var selfs = function () {
    $("#prolist input[type=checkbox]").each(function () {
        if ($(this).attr("checked")) { $(this).removeAttr("checked"); }
        else { $(this).attr("checked", 'true'); }
    })
}
var buypros = function () {
    var str = "";
    $("#prolist input[type=checkbox]").each(function () { 
        if(this.checked){
            str += $(this).val() + ",";}
    });
    if (str != "") {
        str = str.substring(0, str.length - 1);
        $.get("/control/Buy.ashx?ids=" + str + "&" + Math.random(), function (data) {
            var item = data.split("|");
            alert(item[0]);
            if (item[1] != 0) {
                $(".carcount").html(item[1]);
                $("#spcart").html(item[2]);
            }
        });
    }
    else {
        alert("至少要选择一件产品！");
    }
}
var Domain = {
    Homepage: function () {
        if (document.all) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage("http://" + window.location.host);
        }
        else if (window.sidebar) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                }
                catch (e) {
                    alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入about:config,然后将项signed.applets.codebase_principal_support 值该为true");
                    history.go(-1);
                }
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', window.location.href);
        }
    },
    Favorite: function Favorite() {
        var sURL = window.location.url;
        var sTitle = document.title;
        if (sTitle == null) {
            var t_titles = document.getElementByTagName("title")
            if (t_titles && t_titles.length > 0) {
                sTitle = t_titles[0];
            } else {
                sTitle = "";
            }
        }
        try {
            window.external.addFavorite(sURL, sTitle);
        }
        catch (e) {
            try {
                window.sidebar.addPanel(sTitle, sURL, "");
            }
            catch (e) {
                alert("加入收藏失败,请手动添加.");
            }
        }
    }
}
