//默认图标。
var DEFAULT_ICON="http://image.r2online.cn/R2OB/images/community/r2.bmp";
//倒计时
var EndTime = new Date();
var getLimitDate = {
    run: function() {
        EndTime = new Date(EndTime.getFullYear(), EndTime.getMonth(), EndTime.getDate(), 19, 0, 0);
        EndTime = new Date(EndTime.getTime() + (6 - EndTime.getDay()) * 24 * 60 * 60 * 1000);
        this.doCountDown();
    },
    doCountDown: function() {
        var NowTime = new Date();
        var nMS = EndTime - NowTime.getTime() >= 0 ? EndTime - NowTime.getTime() : 0;
        var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));
        var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
        nH = nH < 10 ? ("0" + nH) : nH
        var nM = Math.floor(nMS / (1000 * 60)) % 60;
        nM = nM < 10 ? ("0" + nM) : nM
        var nS = Math.floor(nMS / 1000) % 60;
        nS = nS < 10 ? ("0" + nS) : nS
        $("RemainDay").innerHTML = nD;
        $("RemainTime").innerHTML = nH + ":" + nM + ":" + nS;

        if (nMS > 1000) {
            setTimeout(arguments.callee, 1000);
        }
    }
};

//当前页属于区域ID值 和 服务器的ID值
var currLocalPage = document.location.href.substr(document.location.href.lastIndexOf("/"));
var currIdcId = currLocalPage.replace(/[^\d]/g, "");
var currSvrId = currIdcId.substr(2, 3);
currIdcId = currIdcId.substr(0, 2);
var currIdcName = "";
for (var tmpidc in idc) {
    if (idc[tmpidc] == currIdcId)
        currIdcName = tmpidc;
}
var currSvrName = "";
for (var tmpsvr in svr[currIdcName]) {
    if (svr[currIdcName][tmpsvr] == currSvrId)
        currSvrName = tmpsvr;
}
var selIdcId = currIdcId;
//////////////////////////////////////////

var getAreaServersData = {
    run: function() {
        buildMenu();
    }
}

//构建服务器选择列表
function buildMenu() {
    var menuIdc = createDropMenu({
        $target: $("d1"),
        onChange: function(text, value) {
            //menuSvr.setOptions(svr[text]);
            selIdcId = value;
            for (var tmpsvr in svr[text]) {
                getServerCityConsortiaData.run(svr[text][tmpsvr]);
                break;
            }
        },
        className: "dropmenu"
    });
    var menuSvr = createDropMenu({
        $target: $("d2"),
        onChange: function(text, value) {
            getServerCityConsortiaData.run(value);
        },
        className: "dropmenu"
    });
    menuIdc.setOptions(idc, currIdcName);
    menuSvr.setOptions(svr[currIdcName], currSvrName);
    getServerCityConsortiaData.run(currSvrId);
}

//获取服务器下城池 工会，Map，Top 5 信息
var getServerCityConsortiaData = {
        run: function(serverMark) {
        if (selIdcId != currIdcId || currSvrId != serverMark) {
            document.location.href = "siegeMap.html?" + selIdcId + serverMark;
            return;
        }
        buildMapList(ServerDetailStore.cityConsortiaItems);
        buildConsTopList(ServerDetailStore.topConsortia);
        buildConsNewList(ServerDetailStore.newConsortia);
        buildAdvPlayer(ServerDetailStore.advancedPlayer);
    }
}

//点击地图，显示工行详细信息
var getConsortiaInfData = {
    run: function(ele) {
        for (var n = 0; n < ServerDetailStore.cityConsortiaItemsDetail.length; n++) {
            if (ele.className == ServerDetailStore.cityConsortiaItemsDetail[n].mapId) {
                var oData = ServerDetailStore.cityConsortiaItemsDetail[n];
                for (var i in oData) {
                    if (i.toLowerCase().indexOf("mapid") >= 0) continue;
                    if (i.toLowerCase().indexOf("logo") > 0) {
                        var $img=$("ele" + i);
			$img.src = oData[i];
			EventManager.bind($img,"error",function(){this.src=DEFAULT_ICON;});
                    }
                    else {
                        $("ele" + i).innerHTML = oData[i];
                    }
                }
            }
        }
    }
}

//公会地图
function buildMapList(oData) {
    var item = $("ulMapList");
    for (var i = 0; i < oData.length; i++) {
        var liEle = $("<li>");
        
        // 加入了对 oData[i] 是否为空的判断，add by hanjie
        if(oData[i]){
        	liEle.className = oData[i].mapId;
                
	        liEle.sIndex = i;
	        liEle.onclick = function() {
	            getConsortiaInfData.run(this);
	            if (currDownBtn != "") {
	                $(currDownBtn).style.backgroundPosition = "0px 0px";
	                currDownBtn = "";
	            };
	            togglePopup("panelConsortiaInfo");
	        }
	
	        var imgEle = $("<img>");
	        EventManager.bind(imgEle,"error",function(){this.src=DEFAULT_ICON;});
	        imgEle.className = "imgSize01 fl";	        
	        imgEle.src = oData[i].LogoImg;
	        
	        imgEle.alt = "";
	        liEle.appendChild(imgEle);
	
	        var spanName = $("<span>");	        
	        spanName.innerHTML = oData[i].Name;	        
	        liEle.appendChild(spanName);
	
	        item.appendChild(liEle);
        }
    }
}

//构建公会排名列表
function buildConsTopList(oData) {
    var item = $("topConsPanelFirstItem");
    var nextItem = item.nextSibling;
    for (var i = 0; i < oData.length; i++) {
        while (nextItem.nodeType != 1) {
            nextItem = nextItem.nextSibling;
        }
        var topimgNode = nextItem.firstChild;
        while (topimgNode.nodeType != 1) {
            topimgNode = topimgNode.nextSibling;
        }
        topimgNode = topimgNode.cloneNode(true);
        nextItem.innerHTML = "";
        nextItem.appendChild(topimgNode);

        var divLogo = $("<div>");
        divLogo.className = "tp";
        var imgEle = $("<img>");
        EventManager.bind(imgEle,"error",function(){this.src=DEFAULT_ICON;});
        imgEle.className = "imgSize01 fl";
        imgEle.src = oData[i].LogoImg;
        imgEle.alt = "";
        divLogo.appendChild(imgEle);
	nextItem.appendChild(divLogo);
        var divName = $("<div>");
        divName.className = "name";
        divName.innerHTML = oData[i].Name;
        nextItem.appendChild(divName);

        var divNum = $("<div>");
        divNum.className = "rs";
        divNum.innerHTML = oData[i].MemberCount;
        nextItem.appendChild(divNum);

        nextItem = nextItem.nextSibling;
    }
}

//构建最新公工会列表
function buildConsNewList(oData) {
    var item = $("newConsPanelFirstItem");
    var nextItem = item.nextSibling;
    for (var i = 0; i < oData.length; i++) {
        while (nextItem.nodeType != 1) {
            nextItem = nextItem.nextSibling;
        }
        var topimgNode = nextItem.firstChild;
        while (topimgNode.nodeType != 1) {
            topimgNode = topimgNode.nextSibling;
        }
        topimgNode = topimgNode.cloneNode(true);
        nextItem.innerHTML = "";
        nextItem.appendChild(topimgNode);

        var divLogo = $("<div>");
        divLogo.className = "tp";
        var imgEle = $("<img>");
        EventManager.bind(imgEle,"error",function(){this.src=DEFAULT_ICON;});
        imgEle.className = "imgSize01 fl";
        imgEle.src = oData[i].LogoImg;
        imgEle.alt = "";
        divLogo.appendChild(imgEle);
        nextItem.appendChild(divLogo);

        var divName = $("<div>");
        divName.className = "name";
        divName.innerHTML = oData[i].Name;
        nextItem.appendChild(divName);

        var divNum = $("<div>");
        divNum.className = "rs";
        divNum.innerHTML = oData[i].MemberCount;
        nextItem.appendChild(divNum);

        nextItem = nextItem.nextSibling;
    }
}

//构建高级玩家列表
function buildAdvPlayer(oData) {
    var item = $("advPalyerPanelFirstItem");
    var nextItem = item.nextSibling;
    for (var i = 0; i < oData.length; i++) {
        while (nextItem.nodeType != 1) {
            nextItem = nextItem.nextSibling;
        }
        var topimgNode = nextItem.firstChild;
        while (topimgNode.nodeType != 1) {
            topimgNode = topimgNode.nextSibling;
        }
        topimgNode = topimgNode.cloneNode(true);
        nextItem.innerHTML = "";
        nextItem.appendChild(topimgNode);

        var divName = $("<div>");
        divName.className = "name02";
        divName.innerHTML = oData[i].playerName;
        nextItem.appendChild(divName);

        var divLevel = $("<div>");
        divLevel.className = "level";
        divLevel.innerHTML = "<em class=\"fnt_03\">" + oData[i].level + "</em>级";
        nextItem.appendChild(divLevel);

        nextItem = nextItem.nextSibling;
    }
}

//
var currDownBtn = "";
//显示公会排名
function showTopConsortia() {
    if ($("panelConsortiaTopInfo").style.display == "" || $("panelConsortiaTopInfo").style.display == "none") {
        currDownBtn = this.id;
    }
    else {
        this.style.backgroundPosition = "0px 0px";
        currDownBtn = "";
    }
    togglePopup("panelConsortiaTopInfo");
    var sBP = this.style.backgroundPosition;
    this.style.backgroundPosition = sBP;
}

//显示最新公会
function showNewConsortia() {
    if ($("panelConsortiaNewInfo").style.display == "" || $("panelConsortiaNewInfo").style.display == "none") {
        currDownBtn = this.id;
    }
    else {
        this.style.backgroundPosition = "0px 0px";
        currDownBtn = "";
    }
    togglePopup("panelConsortiaNewInfo");
    var sBP = this.style.backgroundPosition;
    this.style.backgroundPosition = sBP;
}

//显示高级玩家
function showAdvancedPlayer() {
    if ($("panelAdvancedPlayerInfo").style.display == "" || $("panelAdvancedPlayerInfo").style.display == "none") {
        currDownBtn = this.id;
    }
    else {
        this.style.backgroundPosition = "0px 0px";
        currDownBtn = "";
    }
    togglePopup("panelAdvancedPlayerInfo");
    var sBP = this.style.backgroundPosition;
    this.style.backgroundPosition = sBP;
}

function oMOver(event) {
    if (currDownBtn == this.id) return;
    this.style.backgroundPosition = "0px -30px";
}
function oMOut(event) {
    if (currDownBtn == this.id) return;
    this.style.backgroundPosition = "0px 0px";
}
function oMDown(event) {
    if (currDownBtn != "" && currDownBtn != this.id) {
        $(currDownBtn).style.backgroundPosition = "0px 0px";
    };
    this.style.backgroundPosition = "0px -60px";
}
function closeListPanel(panelName) {
    togglePopup(panelName);
    $(currDownBtn).style.backgroundPosition = "0px 0px";
    currDownBtn = "";
}
function initTopList() {
    var btnTopCons = $("btnTopCons");
    EventManager.bind(btnTopCons, "mouseover", oMOver);
    EventManager.bind(btnTopCons, "mouseout", oMOut);
    EventManager.bind(btnTopCons, "mouseup", showTopConsortia);
    EventManager.bind(btnTopCons, "mousedown", oMDown);

    var btnNewCons = $("btnNewCons");
    EventManager.bind(btnNewCons, "mouseover", oMOver);
    EventManager.bind(btnNewCons, "mouseout", oMOut);
    EventManager.bind(btnNewCons, "mouseup", showNewConsortia);
    EventManager.bind(btnNewCons, "mousedown", oMDown);

    var btnAdvPlayer = $("btnAdvPlayer");
    EventManager.bind(btnAdvPlayer, "mouseover", oMOver);
    EventManager.bind(btnAdvPlayer, "mouseout", oMOut);
    EventManager.bind(btnAdvPlayer, "mouseup", showAdvancedPlayer);
    EventManager.bind(btnAdvPlayer, "mousedown", oMDown)
}
