﻿//初始化函数
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

//显示默认菜单
function topmenuHover(menuobj) {
    var links = menuobj.getElementsByTagName("a");
    for (i = 0; i < links.length; i++) {
        //alert(links[i].href);
        //判断路径如果=本身
        if (window.location.href.indexOf(links[i].href) != -1) {
            links[i].className = 'here';
        } else {
            links[i].className = '';
        }
        links[i].onmouseover = function() {
            for (j = 0; j < links.length; j++) {
                links[j].className = '';
            }
            this.className = 'here';
        }
        links[i].onmouseout = function() {
            for (j = 0; j < links.length; j++) {
                links[j].className = '';
                if (window.location.href.indexOf(links[j].href) != -1) {
                    links[j].className = 'here';
                }
            }
        }
    }
}

//菜单事件
function topmenu() {
    if (!document.getElementById("sMenu")) return false;
    var menuobj = document.getElementById("sMenu");
    topmenuHover(menuobj);
}
addLoadEvent(topmenu);

function $(id) {
    return document.getElementById(id);
}


//菜单事件
function topmenu2() {
    if (!document.getElementById("typeList")) return false;
    var menuobj2 = document.getElementById("typeList");
    topmenuHover(menuobj2);
}
addLoadEvent(topmenu2);


//收藏和设置首页
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

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 ButtonSearch() {

    //url方式传递
    var keyString = document.getElementById("keyWord").value;
    var url = "http://" + document.domain; 


    location.href = url + "/Search.aspx?keyWord=" + escape(keyString) + "&ram=" + Math.random();

    //表单方式传递
    //document.form1.action = "Search.aspx?ram=" + Math.random();
    //document.form1.target = "_blank";
    //document.form1.method = "GET";
    //document.form1.submit();
}

//判断全局回车事件
document.onkeydown = function(e) {   
    // 兼容FF和IE和Opera   
    var theEvent = e || window.event;   
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;   
    var activeElementId = document.activeElement.id;//当前处于焦点的元素的id   
    if (code == 13 && activeElementId == "keyWord") {   
        ButtonSearch();//要触发的方法   
        return false;   
    }   
    return true;   
}


//判断全局回车事件
//function keyDownSearch(e) {
//    // 兼容FF和IE和Opera   
//    var theEvent = e || window.event;
//    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
//    if (code == 13) {
//        ButtonSearch(); //具体处理函数   
//        return false;
//    }
//    return true;
//} 
