/**
* 游戏服务器列表操作类 *
* create by jqy @ 2010.09.15 *
* add comListName show by jqy @ 2010.10.26
* fix newServers class show by jqy @ 2011.3.3
*/
function extend(destination, source) {//对象继承方法
	for (var property in source) {
		destination[property] = source[property];
	}
}
function inArray( elem, array ) {//判断数组中是否存在某个元素
	for ( var i = 0, length = array.length; i < length; i++ )
		if ( array[ i ] === elem )
			return i;

	return -1;
};
function isArray( obj ){//判断是否是数组元素
	return Object.prototype.toString.call(obj) === "[object Array]";
};
function GameServers(obj){
	this.obj = obj;
	this.init();
};
GameServers.prototype = {
	init:function(){
		this.setOptions(this.obj);
		this.checkAreaId();
		this.getServerTypes();
		this.getGameServers();
		this.getAreaId();
		this.getGameList();
	},
	setOptions:function(opt){
		this.opt = { 	//默认设置 		*的表示必须要有的
			min_area_id:0,					//*
			max_area_id:0,					//* min和max表示游戏区分的范围  比如mc  1-999
			isp_area_id:0,					//联运区服开始id   例如mc是 800
			allListClass:"server_list_a",	//所有服务器的class
			allLitsName:"双线",				//所有服务器的名称 	暂不使用
			newListClass:"",				//新服class
			newlistName:"新服",				//新服名称
			comListClass:"",				//推荐服class
			comListName:"推荐",				//推荐服名称   
			highlightListClass:"",			//高亮class
			highlightListName:"",			//高亮服名称   暂不使用
			maintainListClass:"",			//维护服class
			maintainListName:"维护",		//维护服名称
			listType:2,						//html模板选择
			type:"page", 					//page:servers.js在页面中引用       ajax:servers.js通过ajax请求   ajax暂不支持
			serversObj:{},					//* 区服信息servers.js中  对象的名称  例如  mc为g_game_mc_servers，当调用方法为page时，为必填项
			ajaxUrl:"",						//ajax地址
			highlightNum:0,					//高亮显示的条目
			ifconsole:false					//调试开关
		};
		extend(this.opt, opt || {});
	},
	console:function(msg){
		if(this.opt.ifconsole){
			console.log(msg);
		}
	},
	checkAreaId:function(){
		if(this.opt.min_area_id >= this.opt.max_area_id){
			this.console("区服范围不正确");
		}
		if(this.opt.isp_area_id != 0 && (this.opt.min_area_id >= this.opt.isp_area_id || this.opt.isp_area_id >= this.opt.max_area_id)){
			this.console("isp区服范围不正确");
		}
	},
	getServerTypes:function(){
		this.serverType = {
			"0" : [this.opt.allListClass, ''],										//所有的服务器
			"1" : [this.opt.newListClass, this.opt.newlistName],					//新服
			"2" : [this.opt.comListClass, this.opt.comListName],					//推荐
			"4" : [this.opt.highlightListClass,this.opt.highlightListName],			//高亮
			"6" : [this.opt.maintainListClass, this.opt.maintainListName]			//维护
		}		
	},
	createListHtml:function(list_type,area_id,allclass,address,area_name,route){//通过模板生成html
		switch(list_type){
					case 1 :
						html = '';
						html += '<li>';
						html += '<a name="' + area_id + '" class="' + allclass + '" href="' + address + '">' + area_name + '</a>';
						html += '<span>(' + route + ')</span>';
						html += '</li>';
						break;
					case 2 :
						html = '';
						html += '<li>';
						html += '<label>';
						html += '<a name="' + area_id + '" class="' + allclass + '" href="' + address + '">' + area_name + '</a>';
						html += '</label>';
						html += '<span>' + route + '</span>';
						html += '</li>';
						break;
				}
		return html;
	},
	getGameServers:function(){//获取区服列表json数据
		this.gameServers = {};
		if(this.opt.type == "page"){
			this.gameServers = this.opt.serversObj;
		}else if(this.opt.type == "ajax"){
			this.console("ajax功能暂时未支持！");
		}else{
			this.console("区服列表获取参数错误！");
		}
		this.console(this.gameServers);
	},
	sortListId:function(ispNum){//区服id排序算法
		return function(a,b){
			if(ispNum != 0 && ((a >= ispNum && b < ispNum)||(a < ispNum && b >= ispNum))){
				return a-b;
			}else{
				return b-a;
			}
		}
	},
	getAreaId:function(){ //获取当前选中区服范围的id数组,并进行排序
		this.areaId = [];
		for(areaid in this.gameServers){
			if(Number(this.gameServers[areaid].area_id) <= this.opt.max_area_id && Number(this.gameServers[areaid].area_id) >= this.opt.min_area_id){
				this.areaId.push(Number(this.gameServers[areaid].area_id));
			}
		}
		var sortListId = this.sortListId(this.opt.isp_area_id);
		this.areaId.sort(sortListId);
		this.console(this.areaId);
	},
	getGameList:function(){	//获取游戏区服信息（重加工）
		this.gameList = [];
		this.newAreaId = [];
		this.comAreaId = [];
		for(var i=0;i<this.areaId.length;i++){
			this.gameList[i] = {};
			extend(this.gameList[i], this.gameServers[this.areaId[i]] || {});
			this.gameList[i].type = this.gameList[i].type.split('|');
			this.gameList[i].ifhighlight = ((i<this.opt.highlightNum) || (inArray("4",this.gameList[i].type) >=0) )? true : false ;
			if(inArray("2",this.gameList[i].type) >= 0){
				this.gameList[i].route = this.serverType["2"][1];
				this.comAreaId.push(this.areaId[i]);
			}
			if(inArray("1",this.gameList[i].type) >= 0){
				this.gameList[i].route = this.serverType["1"][1];
				this.newAreaId.push(this.areaId[i]);
			}
			this.gameList[i].route = inArray("6",this.gameList[i].type) >= 0 ? 	this.serverType["6"][1] : this.gameList[i].route;
			this.gameList[i].allClass = this.opt.allListClass;
			for(var j = 0; j<this.gameList[i].type.length; j++){
				if(typeof this.serverType[this.gameList[i].type[j]] != "undefined"){
					this.gameList[i].allClass += (" "+this.serverType[this.gameList[i].type[j]][0]);
				}
			};
		}
	},
	createListById:function(id){//根据id生成
		var elem = this.gameList[inArray(id,this.areaId)];
		if(typeof elem != "undefined"){
			return this.createListHtml(this.opt.listType,elem.area_id,elem.allClass,elem.address,elem.area_name,elem.route);
		}else{
			return "";
		};
		
	},
	createListByIds:function(ids){//根据id数组生成
		var html = "";
		if(isArray( ids )){
			for(var i = 0; i < ids.length; i++){
				html += this.createListById(ids[i]);	
			};
		}else{
			this.console("传递参数为非数组元素！");
		};
		return html;
	},
	cutArea:function(ar,n){//截取数组
		var idArea = ar.slice();
		var num =idArea.length;
		if(typeof n != "undefined" && n < idArea.length){
			num = n;
		};
		if(typeof n != "undefined" && n <= 0){
			num = 0;
		}
		idArea.splice(num,idArea.length-num);
		return idArea;
	},
	getServersAll:function(n){//获取所有的服务器html  n表示需要的长度  为空表示全部输出
		
		var allIds = this.cutArea(this.areaId,n);
		this.console(allIds);
		return this.createListByIds(allIds);
	},
	getServersNew:function(n){//获取新服
		var newIds = this.cutArea(this.newAreaId,n);
		this.console(newIds);
		return this.createListByIds(newIds);
		
	},
	getServersCom:function(n){//获取推荐服
		var comIds = this.cutArea(this.comAreaId,n);
		this.console(comIds);
		return this.createListByIds(comIds);
	},
	getServersActive:function(ar,n){//获取激活服（也就是根据区服数组来显示的方法）   ar:区服数组  n：显示条目
		var activeIds = [];
		if(isArray(ar)){
			for(var i = 0;i< this.areaId.length;i++ ){
				if(inArray(this.areaId[i],ar) >= 0){
					activeIds.push(this.areaId[i]);
				}
			}
		}else{
			this.console("显示激活区服传递了非数组参数");
		}
		activeIds = this.cutArea(activeIds,n);
		this.console(activeIds);
		return this.createListByIds(activeIds);
	}
}
