/* 定义$方法 */
var $ = function(id) { var obj = typeof id == "string" ? document.getElementById(id) : id; return obj; }

///////////////////////////////////////////////////////////////////

var ls = {};

/* 获取浏览器类型 */
ls.isIE = /msie/i.test(navigator.userAgent);
ls.isFF = /firefox/i.test(navigator.userAgent);

///////////////////////////////////////////////////////////////////

ls.ajax = {
    // xmlhttp对象
    xmlHttp: function() {
        if (ls.isIE) {
            var a = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
            for (var i = 0, l = a.length; i < l; i++) {
                try {
                    return new ActiveXObject(a[i]);
                } catch (e) { };
            }
            return false;
        } else {
            return new XMLHttpRequest();
        };
    },

    // 载入文件
    callFile: function(url, callBack) {
        var xmlhttp = this.xmlHttp();
        var async = !!callBack;
        xmlhttp.open("GET", url, async);
        if (ls.isFF) xmlhttp.overrideMimeType("text/html");
        if (async) {
            xmlhttp.onreadystatechange = this.state(xmlhttp, callBack);
            xmlhttp.send(null);
        } else {
            xmlhttp.send(null);
            return this.result(xmlhttp);
        }
    },

    // AJAX（POST）提交
    request: function(url, callBack) {
        var s = url.indexOf("?");
        var arg = null;
        if (s != -1) {
            arg = url.substring(s + 1);
            url = url.substring(0, s);
        }
        var xmlhttp = this.xmlHttp();
        var async = !!callBack;
        xmlhttp.open("POST", url, async);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        if (ls.isFF) xmlhttp.overrideMimeType("text/html");
        if (async) {
            xmlhttp.onreadystatechange = this.state(xmlhttp, callBack);
            xmlhttp.send(arg);
        } else {
            xmlhttp.send(arg);
            return this.result(xmlhttp);
        }
    },

    // 状态改变
    state: function(xmlhttp, callBack) {
        return function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) callBack(xmlhttp.responseText);
                else callBack(false);
            };
        }
    },

    // 返回结果
    result: function(xmlhttp) {
        return (xmlhttp.status == 200) ? xmlhttp.responseText : false;
    }
}

// 加入收藏
AddFavorite = function(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加。");
        }
    };
}

//设为首页
SetHome = function(obj, vrl) {
    try {
        obj.style.behavior = "url(#default#homepage)";
        obj.setHomePage(vrl);
    }
    catch (e) {
        alert("对不起，目前只有IE支持这个功能！");
    };
}

/* 载入图表导航 */
load_head = function(map_type) {
    ls.ajax.callFile("../include/head.jsp?cptype=" + map_type + "&r=" + Math.random(), function(response) {
        if (response == false) return;
        $("topmenu").innerHTML = response;
    });
}
