﻿var City = {
    controlShow: { "id": { "clickId": "changeCityTag", "showId": "citySpan" }, "css": { "open": "open_city", "close": "close_city"} },
    cityNameObject: { "id": "cityName" },
    guilderObject: { "id": "guildTag" },
    //初始化城市设置
    InitCitySetting: function() {
        if (typeof HeaderCity != 'undefined'
            && typeof HeaderCity["currentCity"] != 'undefined'
            && HeaderCity["currentCity"]["id"] > 1) {
            var cookieValue = HeaderCity["currentCity"]["id"] + "|" + HeaderCity["currentCity"]["name"] + "|" + HeaderCity["currentCity"]["urlSpell"];
            MaiCheDomHelper.readCookie("citymessage", cookieValue, { expires: 7, path: '/', domain: 'maiche.cn' });
            return;
        }

        var cookieValue = MaiCheDomHelper.readCookie("citymessage");
        if (cookieValue != null
            && cookieValue != ""
            && cookieValue.indexOf('|') > -1) {
            var cityList = cookieValue.split('|');
            HeaderCity["currentCity"]["id"] = cityList[0];
            HeaderCity["currentCity"]["name"] = cityList[1];
            HeaderCity["currentCity"]["urlSpell"] = cityList[2];
            this.SettingControlName();
            return;
        }

        if (typeof bit_locationInfo == 'undefined'
            || typeof bit_locationInfo["cityId"] == 'undefined') {
            this.SettingControlName();
            return;
        }
        var cityId = bit_locationInfo["cityId"];

        if (typeof HeaderCity["supportCityList"] != 'undefined'
            && HeaderCity["supportCityList"]["c" + cityId] != 'undefined') {

            var obj = HeaderCity["supportCityList"]["c" + cityId];
            if (obj) {
                HeaderCity["currentCity"]["id"] = obj["id"];
                HeaderCity["currentCity"]["name"] = obj["Name"];
                HeaderCity["currentCity"]["urlSpell"] = obj["urlSpell"];
            }
        }
        this.SettingControlName();
    },
    //设定控件的名称
    SettingControlName: function() {
        var url = "";
        if (HeaderCity["currentCity"]["id"] < 1) {
            url = "http://www.maiche.cn/index.html";
        }
        else if (HeaderCity["isChange"]["bool"].toLowerCase() == "false" && HeaderCity["isChange"]["domain"] == "4s") {
            url = "http://4s.maiche.cn/" + HeaderCity["currentCity"]["urlSpell"] + "/";
        }
        else if (HeaderCity["isChange"]["bool"].toLowerCase() == "false") {
            this.initGuildTag();
            return;
        }
        else {
            url = "http://" + HeaderCity["currentCity"]["urlSpell"] + ".maiche.cn";
        }
        window.location.replace(url);
    }, //设定头部导航名称
    initGuildTag: function() {
        var control = document.getElementById(this.cityNameObject["id"]);
        if (!control) return;
        control.innerHTML = HeaderCity["currentCity"]["name"];
        //设置标签头
        control = document.getElementById(this.guilderObject["id"]);
        if (!control) return;

        var alist = control.getElementsByTagName("a");
        if (alist == null || alist.length < 1) return;

        for (var i = 0; i < alist.length; i++) {
            var obj = alist[i];
            var objValue = obj.getAttribute("initUrl");
            if (objValue == null || objValue == "") continue;

            objValue = objValue.replace(/@city@/g, HeaderCity["currentCity"]["urlSpell"]);
            obj.setAttribute("href", objValue);
        }
    },
    //显示城市选择块
    ShowCitySelectSpan: function() {
        if (typeof this.controlShow == 'undefined'
            || typeof this.controlShow["id"] == 'undefined'
            || typeof this.controlShow["css"] == 'undefined'
            || typeof this.controlShow["id"]["showId"] == 'undefined'
            || typeof this.controlShow["css"]["open"] == 'undefined'
            || typeof this.controlShow["css"]["close"] == 'undefined') return;
        var showControl = document.getElementById(this.controlShow["id"]["showId"]);
        if (!showControl) return;

        //        var cssName = showControl.className;
        //        if (cssName == this.controlShow["css"]["open"])
        //            showControl.className = this.controlShow["css"]["close"];
        //        else
        showControl.className = this.controlShow["css"]["open"];
    }, //关闭显示
    CloseCitySelectSpan: function() {
        if (typeof this.controlShow == 'undefined'
            || typeof this.controlShow["id"] == 'undefined'
            || typeof this.controlShow["css"] == 'undefined'
            || typeof this.controlShow["id"]["showId"] == 'undefined'
            || typeof this.controlShow["css"]["open"] == 'undefined'
            || typeof this.controlShow["css"]["close"] == 'undefined') return;
        var showControl = document.getElementById(this.controlShow["id"]["showId"]);
        if (!showControl) return;

        var cssName = showControl.className;
        if (cssName == this.controlShow["css"]["open"])
            showControl.className = this.controlShow["css"]["close"];
    },
    //初始化城市块显示设置
    initShow: function() {
        if (typeof this.controlShow == 'undefined'
            || typeof this.controlShow["id"] == 'undefined'
            || typeof this.controlShow["id"]["clickId"] == 'undefined') return;

        var control = document.getElementById(this.controlShow["id"]["clickId"]);
        if (!control) return;
        var pro = this;
        MaiCheDomHelper.addEvent(control, "mouseover", function() { pro.ShowCitySelectSpan(); }, false);
        MaiCheDomHelper.addEvent(control, "mouseout", function () { timer = setTimeout('var pro=City;pro.CloseCitySelectSpan()', 200); }, false);

        var showControl = document.getElementById(this.controlShow["id"]["showId"]);
        if (!showControl) return;

        MaiCheDomHelper.addEvent(showControl, "mouseover", function () { clearTimeout(timer); pro.ShowCitySelectSpan(); }, false);
        MaiCheDomHelper.addEvent(showControl, "mouseout", function() { pro.CloseCitySelectSpan(); }, false);
    },
    init: function() {
        if (!document.getElementById) return;
        this.initShow();
        this.InitCitySetting();
    }
}
City.init();
