﻿var menuImgRoot = "/App_Themes/Default/Images/icon/";
var timeout = null;
function ImgRestore() { $("a[id*='top']").removeClass().addClass("default") };
function setImg() {
    var rootId = $("#hidRootId").val() == undefined ? "" : $("#hidRootId").val();
    //alert(rootId);
    switch (rootId) {
        case "29": ImgRestore(); $("#top1").removeClass().addClass("selected");
            break;
        case "31": ImgRestore(); $("#top2").removeClass().addClass("selected");
            break;
        case "36": ImgRestore(); $("#top3").removeClass().addClass("selected");
            break;
        case "39": ImgRestore(); $("#top4").removeClass().addClass("selected");
            break;
        case "40": ImgRestore(); $("#top5").removeClass().addClass("selected");
            break;
        case "46": ImgRestore(); $("#top6").removeClass().addClass("selected");
            break;
        case "sale": ImgRestore(); $('#top7').removeClass().addClass("selected");
            break;
        case "shequ": ImgRestore(); $('#top8').removeClass().addClass("selected");
            break;
        default: ImgRestore();
    }
}
$(document).ready(function() {
    getCartAmount();
    var rootId = $("#hidRootId").val() == undefined ? "0" : $("#hidRootId").val();
    setImg();
    //-----------------------------------导航下拉js----------------------------------
    $(document).ready(function() {
        //        closetimer = 0; if ($("#topNav")) {
        //            $("#topNav b ").mouseover(function() {
        //                clearTimeout(closetimer); 
        //                if (this.className.indexOf("clicked") != -1) {
        //                    $(this).parent().next().slideUp(500); $(this).removeClass("clicked");
        //                }
        //                else {
        //                    $("#topNav b ").removeClass(); $(this).addClass("clicked");
        //                    $("#topNav div:visible").slideUp(500); $(this).parent().next().slideDown(500);
        //                }
        //                return false;
        //            });
        //            $("#topNav").mouseover(function() { clearTimeout(closetimer); });
        //            $("#topNav").mouseout(function() {
        //                closetimer = window.setTimeout(function() {
        //                    $("#topNav div:visible ").slideUp(500); $("#topNav b").removeClass("clicked");
        //                }, 100);
        //            });
        //        }
    $("#topNav li").each(function(i) {
            $(this).hover(function() {
                $(this).children("div").show();
            }, function() {
                $(this).children("div").hide();
            });
        });
    });

    //---------------------搜索用jsTop-----------------------------------------------------
    if (document.getElementById("search_txt")) { $("#search_txt").autocomplete("/Ajax/AjaxSearch.aspx") };

    $("#search_txt").focus(function() {

        if ($.trim($("#search_txt").val()) == "关键字或编号查询") {
            $(this).val("");
        }
    }).keypress(function(event) {
        if (event.keyCode == 13) {

            if ($(this).val() != "") {
                location.href = "/search/1--" + encodeURIComponent($.trim($("#search_txt").val().replace("<", "").replace("%", ""))) + ".html";
                return false;
            }
            else {
                location.href = "/search/";
                return false;
            }
        }
    }).blur(function() {
        if ($.trim($("#search_txt").val()) == "") {
            $(this).val("关键字或编号查询");
        }
    });
    $("#search_btn").click(function() {
        if ($.trim($("#search_txt").val()) == "" || $.trim($("#search_txt").val()) == "关键字或编号查询") {
            location.href = "/search/";
        }
        else {
            location.href = "/search/1--" + encodeURIComponent($.trim($("#search_txt").val().replace("<", "").replace("%", ""))) + ".html";
        }
    });
    //---------------------搜索用jsFooter-----------------------------------------------------
    if (document.getElementById("search_txt1")) { $("#search_txt1").autocomplete("/Ajax/AjaxSearch.aspx") };

    $("#search_txt1").focus(function() {

        if ($.trim($("#search_txt1").val()) == "关键字或编号查询") {
            $(this).val("");
        }
    }).keypress(function(event) {
        if (event.keyCode == 13) {

            if ($(this).val() != "") {
                location.href = "/search/1--" + encodeURIComponent($.trim($("#search_txt1").val().replace("<", "").replace("%", ""))) + ".html";
                return false;
            }
            else {
                location.href = "/search/";
                return false;
            }
        }
    }).blur(function() {
        if ($.trim($("#search_txt1").val()) == "") {
            $(this).val("关键字或编号查询");
        }
    });
    $("#search_btn1").click(function() {
        if ($.trim($("#search_txt1").val()) == "" || $.trim($("#search_txt1").val()) == "关键字或编号查询") {
            location.href = "/search/";
        }
        else {
            location.href = "/search/1--" + encodeURIComponent($.trim($("#search_txt1").val().replace("<", "").replace("%", ""))) + ".html";
        }
    });
});



//-------------------------------common function--------------------
function QueryString(qs) {
    var s = location.href;
    s = s.replace("?", "?&").split("&");
    var re = "";
    for (i = 1; i < s.length; i++)
        if (s[i].indexOf(qs + "=") == 0)
        re = s[i].replace(qs + "=", "");
    return re;
}
function getCartAmount() {
    $.ajax({
        type: "GET",
        url: "/ajax/shoppingCartHandle.aspx",
        data: "action=getCount",
        success: function(data) {
            $("#spanCartCount,.cartCount").text(data);
        },
        error: function(msg) {
        }
    });
}
//失去焦点
function OnBlurSet(txtname, txtvalue) { var objTxt = document.getElementById(txtname); if (objTxt.value == "") { objTxt.value = txtvalue; return; } }
//获取焦点
function OnFocusSet(txtname, txtvalue) { var objTxt = document.getElementById(txtname); if (objTxt.value == txtvalue) { objTxt.value = ""; return; } }



//-------------------------------gotop--------------------
(function() {

    function $(id) {
        return document.getElementById(id);
    }

    function setStyleDisplay(id, status) {
        $(id).style.display = status;
    }

    function goTop(acceleration, time) {
        acceleration = acceleration || 0.1;
        time = time || 5;

        var dx = 0;
        var dy = 0;
        var bx = 0;
        var by = 0;
        var wx = 0;
        var wy = 0;

        if (document.documentElement) {
            dx = document.documentElement.scrollLeft || 0;
            dy = document.documentElement.scrollTop || 0;
        }
        if (document.body) {
            bx = document.body.scrollLeft || 0;
            by = document.body.scrollTop || 0;
        }
        var wx = window.scrollX || 0;
        var wy = window.scrollY || 0;

        var x = Math.max(wx, Math.max(bx, dx));
        var y = Math.max(wy, Math.max(by, dy));

        var speed = 1 + acceleration;
        window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
        if (x > 0 || y > 0) {
            var invokeFunction = "MGJS.goTop(" + acceleration + ", " + time + ")"
            window.setTimeout(invokeFunction, time);
        }
    }

    function switchTab(showPanels, hidePanels, activeTab, activeClass, fadeTab, fadeClass) {
        $(activeTab).className = activeClass;
        $(fadeTab).className = fadeClass;

        var panel, panelList;
        panelList = showPanels.split(',');
        for (var i = 0; i < panelList.length; i++) {
            var panel = panelList[i];
            if ($(panel)) {
                setStyleDisplay(panel, 'block');
            }
        }
        panelList = hidePanels.split(',');
        for (var i = 0; i < panelList.length; i++) {
            panel = panelList[i];
            if ($(panel)) {
                setStyleDisplay(panel, 'none');
            }
        }
    }

    function loadCommentShortcut() {
        $('comment').onkeydown = function(moz_ev) {
            var ev = null;
            if (window.event) {
                ev = window.event;
            } else {
                ev = moz_ev;
            }
            if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
                $('submit').click();
            }
        }
        $('submit').value += ' (Ctrl+Enter)';
    }

    window['MGJS'] = {};
    window['MGJS']['$'] = $;
    window['MGJS']['setStyleDisplay'] = setStyleDisplay;
    window['MGJS']['goTop'] = goTop;
    window['MGJS']['switchTab'] = switchTab;
    window['MGJS']['loadCommentShortcut'] = loadCommentShortcut;

})();

