/*初始化*/
$(function () {
    InitMenu();
    InitSearch();
    search.searchRedirect();
})


//初始化菜单
function InitMenu() {
    var m_cnt = $("#menu a").length;
    if (m_cnt > 0) {
        $($("#menu a")[m_cnt - 1]).addClass("menu_b");
        $("#menu a").each(function (i) {
            if ($(this).attr("id") == "0") {
                if (i >= 1) { $($("#menu a")[i - 1]).addClass("menu_b"); }
                $(this).addClass("menu_a");
            } else {
                //$(this).mouseover(function() { if (i >= 1) { $($("#menu a")[i - 1]).addClass("menu_b") } $(this).addClass("menu_a") });
                //$(this).mouseout(function() { if (i >= 1 && $(this).attr("id") != "0") { $($("#menu a")[i - 1]).removeClass("menu_b") } if ($(this).attr("id") != "0") $(this).removeClass("menu_a") });
            }
        });
    }
}

//初始化搜索框
function InitSearch() {
    if ($("#sch li").length > 0) { $("#sch li").click(function () { $("#sch li").removeClass("on"); $(this).addClass("on"); $("#tp").val($(this).attr("tp")); }); }
}


var searchtxt = "请输入搜索关键词...";
var searchurl = "/mall/search.asp";
var search = {};
search.searchRedirect = function () {
    $("#sop li a").click(function () {
        var actionUrl = "";
        searchType = $(this).attr('tp');
        switch (searchType) {
            case '0':
                actionUrl = searchurl;
                break;
            default:
                actionUrl = searchurl + '?tp=' + searchType;
                break;
        }

        if ("" == actionUrl) return false;
        if (typeof (changeTp) != 'undefined') changeTp(searchType);
        $("#tp").val(searchType);
        $("#frm").attr("action", actionUrl);
        $("#opt b").html($(this).html());
        $("#sop").hide();
        return false;
    });

    $("#btsch").click(function () { if (!check()) { window.location.href = "/mall/search.asp"; return false; } $("#frm").attr("action", searchurl).submit(); });
    $("#op").hover(function () { $("#sop").show(); }, function () { $("#sop").hide(); });
    $("#kw").attr("name", "kw")
    .click(function () {
        if ($(this).val() == searchtxt) { $(this).val(""); }; 
        clearTimeout(switchSearchWordEvent);
        setupAC(document.getElementById("kw"), document.getElementById("btsch"));
    })
    .mouseover(function () { $(this).css("color", "#000"); })
    .mouseout(function () { if ($(this).val() == searchtxt) { $(this).css("color", "#ccc") }; })
    .keypress(function (e) { e || (e = window.event); if (e.keyCode == 13) { if (!check()) return false; $("#frm").attr("action", searchurl).submit(); return false; }; });
    
    SwitchInputSearchWord();
}

function check() {
    var _kw = $("#kw").val();
    if ("" == _kw || searchtxt == _kw) { $("#kw").focus(); return false; }
    return true;
}

function dosearch(ctrlname) {
    var kws = $("input[name='" + ctrlname + "']");
    var strkey = "";
    var actionUrl = "";
    for (var i = 0; i < kws.length; i++) {
        if (kws[i].checked) {
            strkey += kws[i].value + " ";
        }
    }
    if (strkey != "") {
        actionUrl = searchurl + "?kw=" + escape(strkey.trim());
        window.location.href = actionUrl;
    }
    return false;
}


function SetHome(obj,vrl){
    try{
        obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
    }
    catch(e){
        if(window.netscape) {
            try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
            }  
            catch (e)  { 
                    alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");  
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',vrl);
        }
    }
}

function AddFavorite(sURL, sTitle){
    try{
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e){
        try{
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e){
            alert("对不起，您的浏览器不支持此操作!\n请您使用菜单栏或Ctrl+D收藏本站。");
        }
    }
}

