/* for yolax.com ver 2.0 */
/* Add by Micah @ 2008/10/28 14:47:50 */

var nodeValSeeker;
if (!Prototype.Browser.IE) nodeValSeeker = 'textContent';
else nodeValSeeker = 'text';

var ListStyles = {
	'general': ["<li><span class=\"title\"><a href=\"$link$\" target=\"_blank\">$title$</a></span></li>\n"],
	'hotnews': ["<li><span class=\"title\"><a href=\"$link$\" target=\"_blank\">$title$</a></span><span class=\"date\">$dateline$</span></li>\n"],
	'recommendedGames': ["<li><div class=\"img\"><a href=\"$link$\" target=\"_blank\"><img src=\"$image$\" width=\"120\" height=\"140\" border=\"0\" /></a>"+
		"<div class=\"platform %1%\">$platformtitle$</div>%2%"+
		"</div>"+
		"<div class=\"title\"><a href=\"$link$\" target=\"recommendedGameWin\">$title$</a></div>"+
		//"<div class=\"misc\">%3%"+
		//"&nbsp;&nbsp;<a id=\"favo_id_$pId$\" href=\"javascript:void(0);\" onclick=\"bookmark('gameinfo', $pId$, true, this);\">加入收藏</a>"+
		//"</div>"+
		//"<div class=\"misc\">收藏:$totalfavorite$次 关注:$totalfocus$次</div>"+
		"</li>",
		['platform','1','ps3','x360'],
		['recommended','1','<div class="hot"></div>',''],
		['hasfree','1',"<a href=\"/product.html?id=$pId$\">立即租赁</a>","<a href=\"/product.html?id=$pId$\">马上预订</a>"]],
	'newgames': ["<li class=\"bg1\"><span class=\"gametitle\"><a href=\"$link$\" target=\"_blank\">$title$</a></span><span class=\"platform\"><a href=\"product_list.html?platform=$platform$\" target=\"_blank\">$platformtitle$</a></span><span class=\"gametype\"><a href=\"product_list.html?platform=$platform$&gametype=$gametype$\" target=\"_blank\">$gametypetitle$</a></span></li>"]
};

// ajax method
// 2009/01/14 16:08:46
var _ajax = Class.create({
	callFunc:null,

	initialize: function(o) {
		var api = o.api, showTips = o.showTips ? true : false;
		var checklogin = o.login==true ? true:false;
		location.pathname.sub(/\/(\w+)\//, function(match){
			if (['passport'].include(match[1].toLowerCase())) {
				checklogin = Object.isUndefined(o.login)||o.login ? true : false;
			}
		});
		this.callFunc = o.callFunc;

		if (showTips) {this.loadingTip();}

		var queryObject = o.queryObject, queryString = '';
		if (!Object.isUndefined(queryObject)) {
			if (Object.isArray(queryObject)) {
				var a = [];
				for(var i=0;i<queryObject.length;i++) {
					a.push(Object.isString(queryObject[i])||Object.isElement(queryObject[i]) ? $(queryObject[i]).serialize() : $H(queryObject[i]).toQueryString());
				}
				queryString = a.join("&");
			} else {
				queryString = Object.isString(queryObject)||Object.isElement(queryObject) ? $(queryObject).serialize() : $H(queryObject).toQueryString();
			}
		}

		if (o.procMethod&&o.procMethod.toUpperCase() == 'POST') {
			new Ajax.Request(api,{
				method: 'post',
				evalJS: false,
				requestHeaders:["Cache-Control","no-cache"],
				postBody: queryString,
				onSuccess: (function(transport, json) {
					//this.callOutterFunction.apply(this, [transport, checklogin]);
					this.callOutterFunction.apply(this, [transport, json || transport.responseJSON, checklogin]);
				}).bind(this)
			});
		} else {
			new Ajax.Request(api+'?'+queryString,{
				method:'get',
				evalJS: false,
				requestHeaders:["Cache-Control","no-cache"],
				onSuccess: (function(transport, json) {
					//this.callOutterFunction.apply(this, [transport, checklogin]);
					this.callOutterFunction.apply(this, [transport, json || transport.responseJSON, checklogin]);
				}).bind(this)
			});
		}
	},

	loadingTip : function(s) {
		var obj = $('loadingtip_area');
		if (!obj) {
			obj = $(document.createElement('div'));
			obj.id = 'loadingtip_area';
			Element.update(obj, "<div id=loadingtip_bg></div><table border=0 width=100% height=100%><tr><td valign=\"middle\" align=\"center\"><div id=loadingtip_cont><table width=100% height=100%><tr><td align=center valgin=middle><img src=\"../img/loading.gif\" width=\"16\" height=\"16\" align=\"absmiddle\" />&nbsp;&nbsp;正在加载...</td></tr></table></div></td></tr></table>");
			new Insertion.Bottom(document.body, obj);
			//document.body.appendChild(obj);
		}
		if (s == null) {
			$('loadingtip_cont').style.cssText = "left:"+(document.documentElement.scrollLeft+document.documentElement.clientWidth/2-150)+"px;top:"+(document.documentElement.scrollTop+document.documentElement.clientHeight/2-40)+"px;";
			obj.style.cssText = 'display:block;height:'+document.documentElement.scrollHeight+'px;width:'+document.documentElement.scrollWidth+'px;';
		} else {
			obj.style.display = 'none';
		}
	},

	callOutterFunction : function(transport, json, checklogin) {
		var callFunc = this.callFunc;
		var s = "", xml = transport.responseXML;
		this.loadingTip(true);
		
		//checklogin&&
		if (!mainEngine.parseCode(xml)) return;
		
		/*
		if (Object.isArray(callFunc)) {
			callFunc[0][callFunc[1]].apply(callFunc[0], callFunc.length>2?[xml, json, transport].concat(callFunc.slice(2)):[xml, json, transport]);
		} else if (Object.isFunction(callFunc)) {
			callFunc(xml);
		}
		*/
		try {
			attachFunction(Object.isArray(callFunc) ? (callFunc[0] instanceof Function ? [callFunc[0], xml].concat(callFunc.slice(1)) : callFunc.slice(0,2).concat(xml).concat(callFunc.slice(2))).concat([json, transport]) : [callFunc, xml, json, transport]);
		} catch(e) {}
	}
});

function attachFunction(func, emptyFun, debug){
	if (debug) alert(func);
	emptyFun = Object.isUndefined(emptyFun)||!(emptyFun instanceof Function) ? (function(){}) : emptyFun;
	return Object.isUndefined(func) ? emptyFun() :
		Object.isFunction(func) ? func.apply() :
			Object.isArray(func) ?
				(typeof eval(func[0]) == 'object' && eval(func[0]) instanceof Object && (eval(func[0])[func[1]] instanceof Function)) ? eval(func[0])[func[1]].apply(eval(func[0]), func.slice(2))
					: eval(func[0]) instanceof Function ?
						eval(func[0]).apply(this, func.slice(1)) : emptyFun()
			: Object.isString(func) ?
				typeof eval(func) == 'object' && eval(func) instanceof Object ? eval(func).apply()
					: emptyFun()()
		: emptyFun()
}

var Base = Class.create({
	targetList: {},
	linkList: {},
	__intervals: {},
	lockedList: {},

	initTargets: function() {
		var h = $H(this.targetList);
		h.each((function(node, index){
			this.targetList[node[0]] = $(node[1]);
		}).bind(this));
	},

	getLink: function(type, queryObject) {
		var query = $H(queryObject).toQueryString();
		return this.linkList[type]+(query.blank() ? "" : "?"+query);
	},

	_openTips: function(msg) {
		if (this.lockedList.systemTips) return;
		if (this.__intervals.tips) window.clearTimeout(this.__intervals.tips);
		var pnl = this._createTipsPanel('', {'width':'300px', 'height':'auto'});
		pnl.objects.content.update(msg);
		pnl.open();
		var args = $A(arguments);
		if (args.length>1&&!Object.isNumber(args[1])&&Object.keys(this).include("_setTips"+args[1].type.capitalize())) {
			this['_setTips'+args[1].type.capitalize()](pnl, args[1].doFn);
			pnl.wh(args[2]);
		} else {
			this._clearTips(args[1]);
		}
	},

	_createTipsPanel: function(cn) {
		if (!Object.isUndefined(this.targetList.systemTips)) this.targetList.systemTips.objects.self.remove(); //return this.targetList.systemTips;
		var args = $A(arguments);
		var pnl = this.targetList.systemTips = new WiiWin({
			className: Object.isUndefined(cn)||cn.blank()?'sign_in':cn,
			style: Object.extend(
				{zIndex:1000, width:'auto', height:'auto', paddingBottom:'20px'},
				!Object.isUndefined(args[1]) ? args[1] : {}
			)
		});
		return pnl;
	},

	_clearTips: function(freq) {
		if (Object.isUndefined(this.targetList.systemTips)) return;
		var args = $A(arguments);
		if (this.lockedList.systemTips&&(Object.isUndefined(args[1])||!args[1])) return;
		this.lockedList.systemTips = false;
		this.__intervals.tips = window.setTimeout(this.targetList.systemTips.close.bind(this.targetList.systemTips), (Object.isUndefined(freq)||!Object.isNumber(freq)?2:freq)*1000);
	},

	_setTipsConfirm: function(pnl, fns) {
		var container = pnl.objects.content;
		new Insertion.Bottom(container, "<p class=\"tips_confirm_field\" style=\"text-align:center;\"><input type=button value=\"确 认\" style=\"width:60px;height:28px;\" />&nbsp;&nbsp;<input type=button value=\"取 消\" style=\"width:60px;height:28px;\" /></p>");
		var fld = container.down('p.tips_confirm_field');
		var ipts = fld.getElementsBySelector('[type="button"]');
		ipts.each((function(node, index) {
			//if (Object.isFunction(fns[index])) {
				//node.observe('click', (function(){fns[index]();this._clearTips(0, true)}).bind(this));
				node.observe('click', (function(){
					attachFunction(fns[index]);
					this._clearTips(0, true);
				}).bind(this));
			//} else {
			//	node.observe('click', (function(){this._clearTips(0, true)}).bind(this));
			//}
		}).bind(this));
		pnl.objects.closer.hide();
		this.lockedList.systemTips = true;
		ipts[0].activate();
	},

	_setTipsAlert: function(pnl, fn) {
		var container = pnl.objects.content;
		new Insertion.Bottom(container, "<p class=\"tips_confirm_field\" style=\"text-align:center;\"><input type=button value=\"确 认\" style=\"width:60px;height:28px;\" /></p>");
		var ipt = container.down('input');
		//ipt.observe('click', (function(){if (Object.isFunction(fn)){fn();}this._clearTips(0, true)}).bind(this));
		ipt.observe('click', (function(){
			attachFunction(fn);
			this._clearTips(0, true);
		}).bind(this));
		pnl.objects.closer.hide();
		this.lockedList.systemTips = true;
		ipt.activate();
	},

	_freeTips: function(ele, msg) {
		if (this.lockedList.systemTips) return;
		if (this.__intervals.tips) window.clearTimeout(this.__intervals.tips);
		var xy = $(ele).cumulativeOffset(), pnl = this._createTipsPanel('cont_float', {
			'left':xy[0]+$(ele).offsetWidth+'px', 'top':xy[1]+'px', 'width': Prototype.Browser.IE&&parseFloat(navigator.appVersion.match(/(?:MSIE)\s([\w.]+)/)[1])<7 ? '150px' : 'auto', 'height': 'auto', 'padding': '4px'
		});
		pnl.xy(ele,true);
		pnl.objects.content.update(msg);
		pnl.objects.closer.hide();
		pnl.open(false);
		var args = $A(arguments);
		if (args.length>2&&!Object.isNumber(args[2])&&Object.keys(this).include("_setTips"+args[2].type.capitalize())) {
			this['_setTips'+args[12].type.capitalize()](pnl, args[2].doFn);
			pnl.wh(args[3]);
		} else {
			//this._clearTips(args[2]);
		}
	}
});

// get node value(textContent(FF,etc...) or text(IE)) methods
Element.addMethods({
	nodeText: function(element) {
		return element[nodeValSeeker];
		//return element.tagName;
	}
});

// loading list
// 2008/10/29 16:06:30
function loadList(obj, fl, type) {
	try { obj = $(obj).update(); } catch(e) {return;}
	var query = null, args = $A(arguments);
	if (args.length>3) {
		query = type;
		type = args[3];
	}
	new _ajax(Object.extend({api:fl, showTips:false, callFunc:function(xml){
		var lst = $A(xml.getElementsByTagName('item'));
		lst.each(function(node) {
			var s = ListStyles[type][0].replace(/\$(\w+)\$/g, function(a, b){
				return Element.nodeText(node.getElementsByTagName(b)[0]);
			}).replace(/%(\w+)%/g, function(a, b){
				var c = ListStyles[type][b];
				return (Element.nodeText(node.getElementsByTagName(c[0])[0])==c[1] ? c[2] : c[3]).replace(/\$(\w+)\$/g, function(a1, b1){
					return Element.nodeText(node.getElementsByTagName(b1)[0]);
				});
			});
			new Insertion.Bottom(obj, s);
		});
		if (lst.length<1) new Insertion.Bottom(obj, "<li><span class=\"title\">没有相关信息...</span></li>");
		//new Insertion.Bottom(obj, "<div class=clear></div>");
		}
	}, query?{queryObject:query}:{}));
}

function val2int(v) {
	if (/^[0-9]+$/.test(v)) {
		v = parseInt(v.gsub(/^([0]+)([0-9]+)$/, function(match){
			return match[2];
		}));
	} else {
		v = parseInt(v);
	}
	return isNaN(v) ? 0 : v;
}

// main class
// 2008-10-30 1:13:04
var clsMain = Class.create(Base, {
	initialize: function() {
		Event.observe(window, 'load', this.initSearch.bind(this));
	},

	initSearch: function() {
		var st = $('st'), sb = $('sb'), kw;
		if (!st||!sb) return;
		if (st.getValue()!='游戏名称') st.setStyle({'color':'#000'});

		st.observe('focus', function(){
			if (this.getValue()=='游戏名称') {
				this.value = '';
				this.setStyle({'color':'#000'});
			}
		}).observe('blur', function(){
			if (this.getValue().blank()) {
				this.value = '游戏名称';
				this.setStyle({'color':'#CCC'});
			}
		}).observe('mouseover', function(){
			this.activate();
		});
		sb.observe('click', function(){
			kw = st.getValue();
			if (kw.length<2||kw=='游戏名称') return st.activate();
			location.href = "/product_list.html?keyword="+kw;
		});
	},

	parseCode: function(xml) {
		var code = xml instanceof Object ? xml.getElementsByTagName('parsecode') : '', pass = false;
		code = Object.isArray(code) ? Element.nodeText(code[0]) : '';
		switch (code)
		{
			//case 'global_gologin':
			//case 'yolax_alert_login':
			case 'loggedout2':
			var path = false;
			location.pathname.sub(/\/(\w+)\//, function(match){
				if (['passport'].include(match[1].toLowerCase())) {
					path = match[1];
					this._openTips("您的登录已超时，请重新登陆", {type:'alert', doFn:function(){location.href="/passport"}});
				}
			});
			if (!path) user.openLogon();
			break;

			default:
			pass = true;
		}
		return pass;
	},

	alert: function(s, o) {
		if (o == null) {
			window.alert(s);
			return;
		}
		if (o.type == 'confirm') {
			if (window.confirm(s)) {
				o.doFn[0].apply(o);
			} else {
				o.doFn[1].apply(o);
			}
		}
	},

	footer: function() {
		document.write(
			'<div id="outlink">'+				
				'<div class="outlink_img"><a href="http://www.taoxi.com"  target="_blank"><img src="http://www.yolax.com/img/outlink/taoxi.jpg" width="80" height="30" alt="淘洗坊数码冲印" /></a></div>'+
				'<div class="outlink_img"><a href="http://www.eggame.net"  target="_blank"><img src="http://www.yolax.com/img/outlink/syqp.jpg" width="80" height="30" alt="上游棋牌" /></a></div>'+
				'<div class="outlink_img"><a href="http://www.blufans.com"  target="_blank"><img src="http://www.yolax.com/img/outlink/blufans.gif" width="80" height="30" alt="布鲁蓝光网 蓝光中文网" /></a></div>'+				
				'<div class="outlink_img"><a href="http://www.bluraytime.com"  target="_blank"><img src="http://www.yolax.com/img/outlink/bluraytime.gif" width="80" height="30" alt="蓝光时代" /></a></div>'+
				'<div class="outlink_img"><a href="http://www.meyd.cn"  target="_blank"><img src="http://www.yolax.com/img/outlink/manbu.gif" width="80" height="30" alt="漫步小游戏" /></a></div>'+
				
			'</div>' +
		'<div class="clear"></div>'+	
	'   <ul>' +
	'    <li id="footer_homepage"><a href="javascript:void(0)" onclick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.yolax.com\');return(false);" style="behavior:url(#default#homepage);">设置自动访问游乐世</a></li>'+
	'    <li>|</li><li id="footer_favorite"><a href="javascript:window.external.addFavorite(\'http://www.yolax.com\',\'游乐世，次世代主机碟片任你玩\')">将游乐世加入收藏夹</a></li>'+
	'    <li>|</li><li id="footer_job"><a href="/bbs/showthread.php?t=897">游乐世诚聘英才</a></li>'+
	'    <li>|</li><li id="footer_aboutus"><a href="/faq.html?f=52">什么是游乐世</a></li>'+
	'    <li>|</li><li id="footer_aboutus"><a href="/faq.html?f=22" style="font-weight:bold;color:#E33;">我有疑问咨询</a></li>' +
	'    <div class=clear></div>' +
	'   </ul>' +
	'  <div class="clear"></div>' +
	'   <div id="footer_info">' +
	'     <p>信息产业部备案号：<a href=\"http://www.miibeian.gov.cn\" target=\"_blank\">沪ICP备08001389号</a> &nbsp;&nbsp;客服热线:021-58520720&nbsp;&nbsp; 客服QQ1:919542578&nbsp;&nbsp;客服QQ2:1026980604&nbsp;  </p>' +
	'     <p>本站全面支援IE6/7/8,FireFox1.5/2.x/3.x,Safari,Chrome,Opera浏览器，如您的浏览器在使用中遭遇问题，请使用上述浏览器</p>' +
	'     <p>Copyright 2007-2009&copy;, All Rights Reserved &nbsp;&nbsp;www.yolax.com</p>' +
	'  </div>');
	}
});

// initialize
// 2008-10-30 1:13:21
var mainEngine = new clsMain();