// 开启IE6背景图片缓存
try{
document.execCommand('BackgroundImageCache', false, true);
} catch(e){}

$(function(){
	// 首页新闻选项卡切换
	$('#right_layer').each(function(){

		var self = $(this);
		self.find('div.news-tab a').mouseover(function(){
			self.find('div.news-tab a').each(function(){
				$(this).attr('class', $(this).attr('class').replace('mouseon', ''))
			});
			$(this).attr('class', $(this).attr('class').replace('', 'mouseon'));
			$(self.find('div.news-list div').css('display', 'none').get(self.find('div.news-tab a').index(this))).css('display', 'block');
		});
	});
	
	$('#Content').each(function(){

		var self = $(this);
		self.find('div.layoutRight_2 h2 a').mouseover(function(){
			self.find('div.layoutRight_2 h2 a').each(function(){
				$(this).attr('class', $(this).attr('class').replace('btton_on', 'black'))
			});
			$(this).attr('class', $(this).attr('class').replace('black', 'btton_on'));
			$(self.find('div.ul_gamelist span').css('display', 'none').get(self.find('div.layoutRight_2 h2 a').index(this))).css('display', 'block');
		});
	});
	
	$('.ul_playergame').each(function(){
		var self = $(this);
		self.find('li p a').mouseover(function(){
			$(self.find('div.gamepop').css('display', 'none').get(self.find('li p a').index(this))).css('display', 'block');
		});
	});
});
function fnumber(s)  
{  
   n = 0;  
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
   var l = s.split(".")[0].split("").reverse(),  
   r = s.split(".")[1];  
   t = "";  
   for(i = 0; i < l.length; i ++ )  
   {  
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
   }  
   return t.split("").reverse().join("");  
};
function getDays(year, month){
	year = parseInt(year);
	month = parseInt(month);
	if(isNaN(year) || isNaN(month) || year < 1 || month < 1 || month > 12)
		return null;
	var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if(year % 4 == 0)
		days[1] = 29;
	return days[month-1];
};

function redirect_url(){
	var url=document.location.search
	if (url>""){
		var str=url.replace(/%2F/g, "/");
		str=str.replace(/%3A/g, ":");
		str=str.replace(/%3D/g, "=");
		str=str.replace(/%3F/g, "?");
		str=str.replace(/%26/g, "&");
		str=str.replace(/%40/g, "@");
		
		str=str.replace("?url=","");
		return str;
	}else{
		return null;
	}	
}
var JOO = {
	mode: '',
	// 设置模式，static - 静态、instance - 可实例化
	setMode: function(mode){
		this.mode = mode;
	},
	// 给指定的包或方法添加对象或函数
	apply: function(packageName, object){
		if(this.mode != 'static' && this.mode != 'instance'){
			this.mode = '';
			return alert('模式只能设置为static或instance');
		}

		if(object === undefined)
			return false;
		var names = packageName.split('.');

		var pobj = window;
		for(var i=0; i<names.length-1; i++){
			if(pobj[names[i]] !== undefined){
				pobj = pobj[names[i]];
			}else{
				pobj[names[i]] = function(){};
				pobj[names[i]].className = names.slice(0, i+1).join('.');
				pobj[names[i]].parent = pobj;
				pobj = pobj[names[i]];
			}
		}
		if(pobj !== undefined){
			if(object != null && typeof object == 'object' && object.length == undefined){
				for(var i in object){
					this.apply([packageName, i].join('.'), object[i]);
				}
				return true;
			}

			if(this.mode == 'static' || pobj.prototype == undefined){
				if(pobj[names.slice(-1)])
					return alert(packageName + ' 已存在');
				pobj[names.slice(-1)] = object;
			}else if(this.mode == 'instance'){
				if(pobj.prototype[names.slice(-1)])
					return alert(packageName + ' 已存在');
				pobj.prototype[names.slice(-1)] = object;
			}
		}
		return true;
	}
};

JOO.setMode('instance');
JOO.apply('TaskChecker', {
	checkTask: [], // 检查任务列表,按先后顺序检查完成后回调一个方法
	checkedTask: [], // 已检查过的任务
	checkProgress: 0,
	checkTaskDebug: false,
	checkTaskDebugEl: null,
	checkComplete: function(){},
	debug: function(msg){
		if(this.checkTaskDebugEl)
			this.checkTaskDebugEl.value += "\r\n" + msg;
	},
	/**
	 * 初始化检查任务
	 *
	 * @author dengxh at 2010-12-11
	 * @return void
	 */
	initCheckTask: function(){
		this.checkTask = [];
		this.checkedTask = [];
		this.checkProgress = 0;
		this.checkComplete = function(){};
	},

	// 检查完成事件
	onCheckComplete: function(callback){
		this.checkComplete = callback;
	},
	/**
	 * 增加一个检查任务
	 *
	 * @author dengxh at 2010-12-11
	 * @return void
	 */
	addCheckTask: function(taskType, task){
		if(typeof task == 'function')
			this.checkTask.push([taskType, task]);
	},
	/**
	 * 执行所有任务
	 *
	 * @author dengxh at 2010-12-11
	 * @return void
	 */
	checkTaskNow: function(lastReturn){
		if(lastReturn === false)
			return false;
		this.debug('-------------------------------------');
		this.debug('开始检查任务，索引：' + this.checkProgress);
		if(this.checkedTask[this.checkProgress] == true){
			this.debug('任务已处理，程序退出');
			this.checkProgress++;
		}else if(this.checkProgress < this.checkTask.length){
			this.debug('任务索引在队列中');
			this.checkedTask[this.checkProgress] = true;
			if(this.checkTask[this.checkProgress][0] == 'noajax'){
				this.debug('任务为普通任务(noajax)');
				if(!this.checkTask[this.checkProgress][1]()){
					this.debug('任务执行失败，当前索引：' + this.checkProgress);
					this.debug('任务执行失败，函数：' + this.checkTask[this.checkProgress][1]);
					this.checkProgress++;
					this.initCheckTask();
					return false;
				}else{
					this.debug('函数执行成功，索引：' + this.checkProgress);
					this.checkProgress++;
					this.checkTaskNow();
				}
			}else{
				this.debug('任务为ajax任务，异步处理进程：'+this.checkProgress);
				this.checkProgress++;
				this.checkTask[this.checkProgress-1][1]();
				return false;
			}
		}else{
			this.debug('所有检查任务执行完成！');
			this.checkComplete();
			this.initCheckTask();
		}
	},
	/**
	 * 绑定执行完成事件
	 *
	 * @author dengxh at 2010-12-11
	 * @author function completeEvent
	 * @return void
	 */
	onCheckComplete: function(completeEvent){
		this.checkComplete = completeEvent;
	}
});

var CustomFn = {
	setHomePage: function(){
		var url = window.location, domain, d = document, w = window;
		domain = url.protocol + '//' + url.hostname;
		if (d.all){
			d.body.style.behavior='url(#default#homepage)';
			d.body.setHomePage(domain);
		}else if (w.sidebar){
			if(w.netscape){
				try{
					w.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				}catch(e){
					alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值改为true" );
				//return false;
				}
			}
			try{
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage', domain);
			}catch(e){
			//return false;
			}
		}
	},

	addFavorite: function() {
		if (document.all){
			window.external.addFavorite(window.location.href, document.title);
		}else if (window.sidebar){
			window.sidebar.addPanel(document.title, window.location.href, '');
		}
	}
}

var _Ajax = {
	tipSelector: null,
	tipText: '请稍候...',

	/**
	 * 在指定元素内显示一个提示
	 *
	 * @author dengxh at 2009-12-22
	 * @param string selector 元素选择器
	 * @param string text 显示的文字
	 * @return void
	 */
	tip: function(selector, text){
		this.tipSelector = selector || this.tipSelector;
		this.tipText = text || this.tipText;
		return this;
	},

	/**
	 * 显示提示
	 *
	 * @author dengxh at 2009-12-22
	 * @return void
	 */
	showTip: function(){
		if(this.tipSelector != null){
			$(this.tipSelector).find('*').css('display', 'none').find('input').css('disabled', true);
			$(this.tipSelector).append('<span class="ajax-tip">'+ this.tipText +'</span>');
		}
		return this;
	},

	/**
	 * 隐藏提示
	 *
	 * @author dengxh at 2009-12-22
	 * @return void
	 */
	hideTip: function(){
		if(this.tipSelector != null){
			$(this.tipSelector).find('span.ajax-tip').remove();
			$(this.tipSelector).find('*').css('display', 'inline').find('input').css('disabled', false);
		}
	},
	
	/**
	 * 发送POST请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	post: function(url, params, callback){
		this.showTip();
		$.post(url, $.extend(params, {
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		}, 'json');
	},

	/**
	 * 发送GET请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	get: function(url, params, callback){
		this.showTip();
		$.get(url, $.extend(params, {
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		}, 'json');
	},

	/**
	 * 发送ajax post表单请求
	 *
	 * @author dengxh at 2009-3-30
	 * @param string formID 表单ID ( FIX BY QINCHH AT 2010-02-09 允许传入对象 )
	 * @param function fn 回调函数
	 * @param submit 提交按钮 ( ADD BY QINCHH AT 2010-02-09 )
	 * @return void
	 */
	postForm: function(formId, fn){
		fn = fn || function(){};
		var f = typeof(formId) == 'object' ? formId : $('#' + formId);
		f.ajaxSubmit({
			success: fn,
			data: {
				caller: 'ajax'
			},
			dataType : 'json'
		});
	},

	/**
	 * 发送GET JSON请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	getJSON: function(url, params, callback, type){
		this.showTip();
		type = type || 'jsonp';
		if( type == 'jsonp' ){
			if( url.indexOf('?') == -1 )
				url += '?callback=?';
			else
				url += '&callback=?'
		}
		$.getJSON(url, $.extend(params, {
			format: type,
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		});
	}
};
var Ajax = Ajax || _Ajax;

String.prototype.utfLength = function(){
	return this.replace(/[^\x00-\xff]/ig, '***').length;
}

var Tip = Tip || {
	/**
	 * 显示正在加载
	 */
	showLoading: function(){
		if (!$("#loading", top.document).is("div")) {
			$("body", top.document).append('<div id="loading" class="loading">正在加载...</div>');
		}
		var w = $("#loading", top.document);
		var topPx = top.document.documentElement.scrollTop || 0;
		w.css("top", topPx);
		w.show();
	},

	/**
     * 隐藏正在加载
     */
	hideLoading: function(){
		if ($("#loading", top.document)) {
			$("#loading", top.document).hide();
		}
	},

	/**
     * 弹出提示框
     */
	toolTip: function(objName, fn){
		$("#" + objName).tooltip({
			delay: 0,
			showURL: false,
			extraClass: "tip",
			fixPNG: true,
			left: -120,
			bodyHandler: fn
		});
	},

	/**
     * popup弹出页
     */
	popPage : function(title, url, width, height){
		window.popup = new Popup({
			contentType:1,
			scrollType:'auto',
			isReloadOnClose:false,
			width: width||650,
			height: height||550
		});
		window.popup.setContent("contentUrl", url);
		window.popup.setContent("title", title);
		window.popup.build();
		window.popup.show();
	},

	/**
     * popup弹出HTML
	 *
	 * @author dengxh at 2009-10-19
	 * @param string title 标题
	 * @param string html 内容
	 * @param int width 宽度
	 * @param int height 高度
	 * @return void
     */
	popHtml : function(title, html, width, height){
		window.popup = new Popup({
			contentType:2,
			scrollType:'auto',
			isReloadOnClose:false,
			width: width||650,
			height: height||550
		});
		window.popup.setContent("contentHtml", html);
		//alert(window.popup.info.contentHtml);return;
		window.popup.setContent("title", title);
		window.popup.build();
		window.popup.show();
	},
	popClose : function(){
		window.popup.close();
	}
}

var Tab = {
	container : '.selectButton a',
	url : [],
	normal : 'changeBt_up',
	highlight: 'changeBt_on',
	pattern: /[0-9a-z\.]*$/,
	myurl : window.location.href,

	//把地址压入数组
	append: function(url){
		this.url.push(url.match(this.pattern).toString().split('.')[0]);
	},
	//通过对象o获取数据
	create: function(o){
		this.container =  o.container || this.container;
		this.normal = o.normal || this.normal;
		this.highlight= o.highlight || this.highlight;
	},
	//改变选项卡样式
	change : function(){
		var no = 0;
		for(var i = 0; i < this.url.length; i++){
			if(this.myurl.match(this.pattern).toString() == this.url[i].toString())
				no = i;
			else if(this.myurl.match(this.pattern).toString().indexOf(this.url[i].toString()) != -1)
				no = i;
		}
		$(this.container).eq(no).removeClass(this.normal);
		$(this.container).eq(no).addClass(this.highlight);
	}
}
