﻿var def = "0";
var isLogin = false;
var LoginStatus = -1; //-1 未载入 0 正在登陆中 1 完成
var loginUserID = 0;

var iszeroProduct = false;//用于是否是面议产品

//index.shtml setTab
function setTab(name, cursel, n) {
    for (i = 1; i <= n; i++) {
        var menu = document.getElementById(name + i);
        var con = document.getElementById("con_" + name + "_" + i);
        menu.className = i == cursel ? "hover" : "";
        con.style.display = i == cursel ? "block" : "none";
    }
}

//menuInfo
var menuInfo = function () {
    //menuFix
    $("#nav li").each(function (i) {
        $(this).attr("id", "mm" + i.toString());
        $(this).mouseover(function () { $(this).addClass("current") });
        $(this).mouseout(function () { $(this).removeClass("current"); if (def != null) { $("#mm" + def).addClass("current"); } });
    });
    if (def != null) { if (def == 3 && location.href.toString().toLowerCase().indexOf("com/activity") <= 0) { def = 7; }  $("#mm" + def).addClass("current"); }
}

//my js
var LoadInfo = function() {
    LoginInfo();
    menuInfo();
}
var LoginInfo = function () {
    LoginStatus = 0;
    $.get("/User/UserHandler.ashx",
        { action: "get", r: Math.random() },
         function (data) {
             var json = eval("(" + data + ")");
             //login info
             if (json.userName != "") {
                 $("#userInfo").html($.sp.format("欢迎您，{0} <a href=\"/user/\">我的帐户</a> <a href=\"/user/login.aspx?action=logout\">退出</a>", json.userName));
                 isLogin = true;
                 LoginStatus = 1;
                 loginUserID = json.userID;
             }
             else {
                 $("#userInfo").html($.sp.format("<a href=\"/user/login.aspx?backurl={0}\" onclick=\"return !showDialog({ title: '用户登录', iUrl: '/User/loginDialog.aspx' }); \">登录</a> / <a href=\"/user/login.aspx?backurl={0}\">注册</a>", json.url));
                 LoginStatus = -1;
             }
             //cart info
             $("#shopCartTooltip").html($.sp.format('共{0}件商品，总价约{1}元，会员可优惠', json.pCount, parseFloat(json.pPrice).toFixed(2)));
             $("#shopCart").append("(" + json.pCount + ")")
                .tooltip({ tip: '.tooltip', effect: 'slide', position: 'bottom center', offset: [5, 0] });
         });
}

var scrollableShow = function (isShowBig, num) {
    if (isShowBig) {
        try{
        $(".bigScrollable").scrollable({ circular: true, mousewheel: true }).navigator().autoscroll({ interval: 5000 });
        } catch (ex) { }
    }
    var htmlContent = "";
    $("#brandChained .items").children().each(function (i) {
        if (i == 0)
            htmlContent += ("<div><div>" + $(this).html() + "</div>");
        else if (i % num == 0)
            htmlContent += ("</div><div><div>" + $(this).html() + "</div>");
        else if (i == $("#brandChained .items").children().length)
            htmlContent += ("<div>" + $(this).html() + "</div></div>");
        else
            htmlContent += ("<div>" + $(this).html() + "</div>");
    });
    $("#brandChained .items").html(htmlContent);
    $("#brandChained").scrollable({ circular: true, mousewheel: true, next: ".top_right", prev: ".top_left" }).navigator().autoscroll({ interval: 4000 });
}

var scrollableShow1 = function (isShowBig, num) {
    if (isShowBig) {
        try{
            $(".bigScrollable").scrollable({ circular: true, mousewheel: true }).navigator().autoscroll({ interval: 5000 });
        }catch(ex){}
    }
    var htmlContent = "";
    $("#brandChained1 .items").children().each(function (i) {
        if (i == 0)
            htmlContent += ("<div><div>" + $(this).html() + "</div>");
        else if (i % num == 0)
            htmlContent += ("</div><div><div>" + $(this).html() + "</div>");
        else if (i == $("#brandChained1 .items").children().length)
            htmlContent += ("<div>" + $(this).html() + "</div></div>");
        else
            htmlContent += ("<div>" + $(this).html() + "</div>");
    });
    $("#brandChained1 .items").html(htmlContent);
    $("#brandChained1").scrollable({ circular: true, mousewheel: true, next: ".top_right", prev: ".top_left" }).navigator().autoscroll({ interval: 4000 });
}

var getSImage = function(filePath) {
    var fileName = filePath.toString().substring(filePath.toString().lastIndexOf("/"), filePath.toString().length);
    return (filePath.toString().replace(fileName, "/thumb" + fileName));
}

var cnCalendar = function(e) {
    this.dateObj = e != undefined ? e : new Date;
    this.SY = this.dateObj.getFullYear();
    this.SM = this.dateObj.getMonth();
    this.SD = this.dateObj.getDate();
    this.weekday = function() {
        var a = ["\u661F\u671F\u65e5", "\u661F\u671F\u4e00", "\u661F\u671F\u4e8c", "\u661F\u671F\u4e09", "\u661F\u671F\u56db", "\u661F\u671F\u4e94", "\u661F\u671F\u516d"];
        return a[this.dateObj.getDay()]
    };
    this.YYMMDD = function() {
        var a = [this.SY + '\u5E74' + (this.SM + 1), "\u6708", this.SD, "\u65e5"];
        return a.join("")
    }
}

//邮件订阅添加
$(function () {
    $("#addEmail").click(function () {
        var email_reg = /^[_\.0-9a-zA-Z+-]+@([0-9a-zA-Z]+[0-9a-zA-Z-]*\.)+[a-zA-Z]{2,4}$/; //验证email的格式. 
        if ($("#email").val() == "") {
            alert("邮件地址不能为空！");
            return;
        } else if (!email_reg.exec($("#email").val())) {
            alert("请输入正确的email格式！");
        } else {
            $.getJSON(
           "/WebService/email.ashx",
           { email: $("#email").val() },
           function (json) {
               if (json.success == "true")
                   alert("成功订阅邮件，已向您的邮箱发送订阅成功信息！");
               else
                   alert("操作失败：" + json.info);
           });
        }
    });
});


//网站换肤
$(function () {
    var $li = $("#skin li");
    $li.click(function () {
        switchSkin(this.id);
    });
    var cookie_skin = getCookie("MyCssSkin");
    if (cookie_skin) {
        switchSkin(cookie_skin);
    } else {
        switchSkin("skin_0");
    }

    //产品分类
    $("li a", ".p-0-cat").hover(function (e) {
        $(".p-0-sub-cat").remove();
        $(".p-0-sub-cat-a-border").remove();

        var $p0lia = $(this);
        var $p0li = $(this).parent("li");
        var $p0dl = $("div", $p0li);
        if ($p0dl.length <= 0)
            return;
        var $p0SubCatABorder = $("<div>").attr("class", "p-0-sub-cat-a-border").appendTo("body")
			.css({
			    left: ($p0li.offset().left + 5),
			    top: ($p0li.offset().top + 1),
			    zIndex: "99999"
			})
			.html('<a href="' + $("a:first", $p0li).attr("href") + '">' + $("a:first", $p0li).text() + '</a>');

        var $p0SubCatContent = $("<div>").attr("class", "p-0-sub-cat").appendTo("body").html($p0dl.html())
									.css({
									    left: ($p0li.offset().left + 165),
									    top: ($p0li.offset().top + 1),
									    zIndex: "9999"
									});

        //绑定事件 
        $p0SubCatContent.hover(function () { }, function (e) {
            var x = e.pageX;
            var y = e.pageY;
            if (x > ($p0lia.offset().left + 165) || x < $p0lia.offset().left || y <= ($("ul", ".p-0-cat").offset().top + 1) || y >= ($("ul", ".p-0-cat").offset().top + $("ul", ".p-0-cat").height())) {
                $(this).remove();
                $p0SubCatABorder.remove();
            }
        });

        $p0SubCatABorder.hover(function () { }, function (e) {
            var x = e.pageX;
            var y = e.pageY;
            if (x > ($p0lia.offset().left + 165) || x < $p0lia.offset().left || y <= ($("ul", ".p-0-cat").offset().top + 1) || y >= $("ul", ".p-0-cat").offset().top + $("ul", ".p-0-cat").height()) {
                $(this).remove();
                $p0SubCatContent.remove();
            }
        });
    }, function () { });

});

//切换颜色
function switchSkin(skinName) {
    $("#" + skinName).addClass("selected")                //当前<li>元素选中
					   .siblings().removeClass("selected");  //去掉其他同辈<li>元素的选中
    $("#theme").attr("href", "/css/skin/" + skinName + ".css"); //设置不同皮肤
    setCookie("MyCssSkin", skinName, 30);
}

//设置cookie
function setCookie(name, value, days) {
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
}

//写入cookie
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}

//大型专题背景
var closeBodyBg = function () {
    $("body").css("background", "none");
    $("#closeBodyBgLeft").css("display", "none");
    $("#closeBodyBgRight").css("display", "none");
}
var showBodyBg = function (bgImg, top, isIndex) {
    var isShowBg = getCookie("isShowBg");
    if (isIndex || !isShowBg) {
        $("body").css({ "background": "url(" + bgImg + ") repeat-x 0px " + top });
        //$("<div id=\"closeBodyBgLeft\"><a href=\"#\" onclick=\"return closeBodyBg();\">X</a></div>").insertAfter("#headBody");
        $("<div id=\"closeBodyBgRight\"><a href=\"#\" onclick=\"return closeBodyBg();\">X</a></div>").insertAfter("#headBody");
        //$("#closeBodyBgLeft").css({ position: "absolute",left: "30px", background: "#fff", padding: "3px 6px", border: "1px #999 solid" });
        $("#closeBodyBgRight").css({ position: "absolute",top:"10px", left: ($(window).width() - 10 - 20) + "px", background: "#fff", padding: "3px 6px", border: "1px #999 solid" });
        setCookie("isShowBg", "true", 1);
    }
}

var showDialog = function (config) {
    try {
        var settings = { title: "", content: "", iUrl: "", btnText: "", btnFun: function () { $('#dialoglg').dialog('close'); }, width: 500, height: 300 };
        jQuery.extend(settings, config);

        if ($("#dialoglg").size() <= 0) {
            $('<div id="dialoglg" title="' + settings.title + '"></div>').appendTo("body");
        }

        if (settings.iUrl != "") {
            $('#dialoglg').html('<iframe frameborder="0" id="dialogF" name="dialogF" scrolling="auto" src="' + settings.iUrl + '" style="HEIGHT: 100%; overflow:hidden;VISIBILITY: inherit; WIDTH: 100%; Z-INDEX: 2"></iframe>');
        }
        else {
            $('#dialoglg').html(settings.content);
        }
        var dialog = "$('#dialoglg').dialog({";
        dialog += "resizable: false,";
        dialog += "width: " + settings.width + ",";
        dialog += "height: " + settings.height + ",";
        dialog += "modal: true";
        if (settings.btnText != "") {
            dialog += ",buttons: {";
            dialog += "'" + settings.btnText + "': " + settings.btnFun + "";
            dialog += "}";
        }
        dialog += "});";


        eval(dialog);
        $("#ui-dialog-title-dialog").html(settings.title);
        $(".ui-dialog-titlebar-close").html("X").css({ "text-indent": "0px" });
        return true;
    } catch (e) {
        $('#dialoglg').html("");
        return false;
    }

}

