﻿ var IE6 = (navigator.userAgent.indexOf("MSIE 6.0") > 0);
var IE7 = (navigator.userAgent.indexOf("MSIE 7.0") > 0);
var IE8 = (navigator.userAgent.indexOf("MSIE 8.0") > 0);
var ubType = (navigator.userAgent.indexOf("MSIE") > 0);
var divShowTop = 0;
var divShowLeft = 0;
function loadCityWeather() {
    var UserSelectCity = getCookie("UserSelectCity");
    if (UserSelectCity == null)
        cityOnchangeTop("北京");
    else
        cityOnchangeTop(UserSelectCity);
}
function cityOnchangeTop(cityName) {
    if (cityName == "北京") {
        document.getElementById("imgWeather").style.display = "inline";
        document.getElementById("ifrWeather").style.display = "none";
    }
    else {
        document.getElementById("ifrWeather").src = "http://www.haon8.com/web/Weather/WeatherIfarem.aspx?city=" + encodeURIComponent(cityName);
        document.getElementById("imgWeather").style.display = "none";
        document.getElementById("ifrWeather").style.display = "inline";
        document.getElementById("divAreaCity").style.display = "none";
    }
    setCookie(cityName);
}
function loadCityTop(selectCity, that) {
    if (IE6||IE7||IE8) {
        divShowTop = event.clientY;
        divShowLeft = event.clientX;
    }
    else {
        divShowTop = that.offsetTop;
        divShowLeft = that.offsetLeft;
    }
    var xmlhttp = null;
    if (ubType)
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    else
        xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "/xml/city/" + selectCity + ".xml?" + Math.random(), false);
    xmlhttp.send(null);
    Do(xmlhttp.responseText);
}
function createXml(str) {
    if (IE6||IE7||IE8) {
        var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        xmlDom.loadXML(str);
        return xmlDom;
    }
    else
        return new DOMParser().parseFromString(str, "text/xml")
}
function Do(selectAreaTop) {
    var xmlobject = createXml(selectAreaTop); //(new DOMParser()).parseFromString(selectAreaTop, "text/xml");
    var bi = xmlobject.documentElement.childNodes;
    var defaultSelect = "";
    if (bi != null && bi.length > 0) {
        document.getElementById("ulCity").innerHTML = "";
        for (i = 1; i < bi.length; i++) {
            if (bi[i].attributes != null)
                document.getElementById("ulCity").innerHTML += "<li><a onclick=\"cityOnchangeTop('" + bi[i].attributes[1].value + "')\">" + bi[i].attributes[1].value + "</a></li>";
        }
        document.getElementById("divAreaCity").style.display = "inline";
        document.getElementById("divAreaCity").style.position = "absolute";
        document.getElementById("divAreaCity").style.left = divShowLeft + "px";
        document.getElementById("divAreaCity").style.top = divShowTop + "px";
    }
}
//写cookie
function setCookie(str) {
    var expires = new Date();
    expires.setTime(expires.getTime() + 3 * 30 * 24 * 60 * 60 * 1000); //三个月失效
    document.cookie = "UserSelectCity=" + escape(str) + ";expires=" + expires.toGMTString() + ";path=/;";
}
//读cookie
function getCookie(cookieName) {
    var cookieString = document.cookie;
    var start = cookieString.indexOf(cookieName + '=');
    // 加上等号的原因是避免在某些 Cookie 的值里有
    // 与 cookieName 一样的字符串。
    if (start == -1) // 找不到
        return null;
    start += cookieName.length + 1;
    var end = cookieString.indexOf(';', start);
    if (end == -1) return unescape(cookieString.substring(start));
    return unescape(cookieString.substring(start, end));
}
