function $(id){return document.getElementById(id);}
function trim(stringToTrim) {	return stringToTrim.replace(/^\s+|\s+$/g,"");}

var Cookie={};
Cookie.set=function(name,value,hours){
    if(arguments.length>2){
        var expireDate=new Date(new Date().getTime()+hours*3600000);
        document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expireDate.toGMTString();
    }else{
        document.cookie = name + "=" + escape(value) + "; path=/;"; 
    }
}
Cookie.get=function(name){
	var search = name + "=";
	var offset = document.cookie.indexOf(search);
	if (offset != -1) {
		offset += search.length;
		var end = document.cookie.indexOf(";", offset);
		if (end == -1){
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, end));
	}else {
		return "";
	}
}

var F={};
F.COOKIE_NAME = 'you_have_played';
F.play = function(id,name,type){
	var t = encodeURIComponent(type);
	var url = "http://Flash.JxGame.Net/video/"+id+".html";
	window.open(url);
	
	F.writeHistory(id,name,type);
}
F.record = function(id,name,type){
	var t = encodeURIComponent(type);
	F.writeHistory(id,name,type);
}
F.writeHistory = function(id,name,type){
	var newValue = F.checkDuplicated(id,name,type);
	Cookie.set(F.COOKIE_NAME,newValue,24*30);
}
F.checkDuplicated = function(id,name,type){
	var second = parseInt((new Date()).getTime()/1000);
	var oldValue = Cookie.get(F.COOKIE_NAME);
	var oldArr = oldValue.split("|");
	var j = 0;
	
	for(j=0; j<oldArr.length; j++){
		if((oldArr[j].split("#"))[0] == id){
			break;
		}
	}
	oldArr.splice(j,1);
	oldArr = oldArr.slice(0,9);
	oldValue = oldArr.join("|");
	var newValue  = (id + "#" +name + "#" + type + "#" + second + "|" ) + oldValue;
	return newValue;
}

function checkHistory(){
	if(hasHistory()){
		showHistory();
	}
}	
function hasHistory(){
	if(Cookie.get(F.COOKIE_NAME)){
		return true;
	}else{
		return false;
	}
}
function showHistory(){
	var str = '<ul class="history_list">';
	var cookieStr = Cookie.get(F.COOKIE_NAME);
	if(cookieStr){
		var arr = cookieStr.split("|");
		var showedNum = 0;
		for(var i=0; i<arr.length; i++){
			if(!arr[i]){continue;}
			if(showedNum >= 7){break;}
			var id = arr[i].split("#")[0];
			var name = arr[i].split("#")[1];
			var dispname = name.substring(0,6)+((name.length>6)?"...":"");
			var type = arr[i].split("#")[2];
			var oldsecond = arr[i].split("#")[3];
			var timeStr = formatTime(oldsecond);
			str += '<li><a href="javascript:F.play(\''+id+'\',\''+name+'\',\''+type+'\');" title="'+name+'">'+dispname+'</a><span class="time">'+timeStr+'</span></li>';
			showedNum += 1;
		}
		
	}
	str += '</ul>';
	
	$("div_history").innerHTML = str;
	
}

function formatTime(str) {
	var post_date = new Date(str*1000);
	var now = new Date();
	
	if (now - post_date > 24*60*60*1000){
		var minute = post_date.getMinutes()<10?"0"+post_date.getMinutes():post_date.getMinutes();
		return (post_date.getMonth()+1)+"-"+post_date.getDate();
	}
	else if( now - post_date > 60*60*1000 ){
		var hour = Math.ceil((now.getTime()-post_date.getTime())/3600000);
		return hour+"小时前";
	}
	else if(now > post_date){
		var minute = Math.ceil((now.getTime()-post_date.getTime())/60000);
		return minute+"分钟前";
	}
	else{
		return "日期失效";
	}
}
 
var TINY={};

//function T$(i){return document.getElementById(i)}
//function T$$(e,p){return p.getElementsByTagName(e)}

//  TINY.slider=function(){
//	function slide(n,p){this.n=n; this.init(p)}
	//slide.prototype.delayHandler = null;
	//slide.prototype.init=function(p){
		//var s=T$(p.id), u=this.u=T$$('ul',s)[0], c=T$$('li',u), l=c.length, i=this.l=this.c=0;
		//if(p.navid&&p.activeclass){this.g=T$$('li',T$(p.navid)); this.s=p.activeclass}
		//this.a=p.auto||0; this.p=p.resume||0; this.v=p.vertical||0; s.style.overflow='hidden';
		//for(i;i<l;i++){if(c[i].parentNode==u){this.l++}}
		//if(this.v){;
		//	u.style.top=0; this.h=p.height||c[0].offsetHeight; u.style.height=(this.l*this.h)+'px'
		//}else{
	//		u.style.left=0; this.w=p.width||c[0].offsetWidth; u.style.width=(this.l*this.w)+'px'
	//	}
	//	this.pos(p.position||0,this.a?1:0)
	//},
	//slide.prototype.auto=function(){
	//	this.u.ai=setInterval(new Function(this.n+'.move(1,1)'),this.a*1000)
	//},
	//slide.prototype.move=function(d,a){
		//var n=this.c+d, i=d==1?n==this.l?0:n:n<0?this.l-1:n; this.pos(i,a)
	//},
	//slide.prototype.posold=function(p,a){
		//clearInterval(this.u.ai); clearInterval(this.u.si);
		//var o=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left),
		//t=this.v?p*this.h:p*this.w, d=t>Math.abs(o)?1:-1; t=t*-1; this.c=p;
		//if(this.g){for(var i=0;i<this.l;i++){this.g[i].className=i==p?this.s:''}}
		//this.u.si=setInterval(new Function(this.n+'.slide('+t+','+d+','+a+')'),10)
	//},
	//slide.prototype.slide=function(t,d,a){
	//	var o=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left);
		//if(o==t){
		//	clearInterval(this.u.si); if(a||(this.a&&this.p)){this.auto()}
		//}else{
		//	var v=o-Math.ceil(Math.abs(t-o)*.15)*d+'px';
		//	this.v?this.u.style.top=v:this.u.style.left=v
		//}
//	},
//	slide.prototype.pos=function(p,a){
	//	if(slide.delayHandler){
		//	clearTimeout(slide.delayHandler);
	//	}
	//	slide.delayHandler = setTimeout(new Function(this.n+".posold("+p+","+a+")"),200);
	//};
	//return{slide:slide}
//}(); 

var LAZY=(function(){
	var pResizeTimer = null;
	var imgs={};
	function addEventHandler (oTarget, sEventType, fnHandler) {
		if (oTarget.addEventListener) {
			oTarget.addEventListener(sEventType, fnHandler, false);
		} else if (oTarget.attachEvent) {
			oTarget.attachEvent("on" + sEventType, fnHandler);
		} else {
			oTarget["on" + sEventType] = fnHandler;
		}
	};
	function resize(){
		if(pResizeTimer) return '';
		pResizeTimer = setTimeout(function(){
							resize_run();
							try{
								clearTimeout(pResizeTimer);
							}
							catch(e){}
							pResizeTimer=null;
						}, 100);
	}
	function resize_run(){
		var min={};
		var max={};
		//min.Top=document.documentElement.scrollTop;
        min.Top = document.body.scrollTop + document.documentElement.scrollTop;
		min.Left=document.documentElement.scrollLeft;
		max.Top=min.Top+document.documentElement.clientHeight;
		max.Left=min.Left+document.documentElement.clientWidth;

		for(var i in imgs){
			if(imgs[i]){
				var _img=imgs[i];
				var img=document.getElementById(i);
				var width = img.clientWidth;
				var height = img.clientHeight;
				var wh=position(img);
				if(
					(wh.Top>=min.Top && wh.Top<=max.Top && wh.Left>=min.Left && wh.Left<=max.Left)
					||
					((wh.Top+height)>=min.Top && wh.Top<=max.Top && (wh.Left+width)>=min.Left && wh.Left<=max.Left))
				{
					img.src=_img.src;
					delete imgs[i];
				}

			}
		}
	}

	function position(o){
		var p={Top:0,Left:0};
		while(!!o){
			p.Top+=o.offsetTop;
			p.Left+=o.offsetLeft;
			o=o.offsetParent;
		}
		return p;
	}
	
	return {
		init:function(){
			for(var i=0;i<document.images.length;i++){
				var img = document.images[i];
				var config={};
				config.id = img.id;
				config.src = img.getAttribute('_src');
				if(config.src && !config.id){
					config.id = encodeURIComponent(config.src) + Math.random();
					img.id = config.id;
				}
				if(!config.id || !config.src) continue;
				LAZY.push(config);
			}
		},
		push:function(config){
			imgs[config.id] = config;
		},
		run:function(){
			resize_run();
			addEventHandler(window,'scroll',resize);
			//addEventHandler(window,'resize',resize);
		}
	};
})(); 
var slideshow;
function gameSearch(){
	var skey = trim($("skey").value)
	if(skey && (skey != "请输入游戏名称")){
		window.open("http://Flash.JxGame.Net/search.asp?searchword="+encodeURIComponent($("skey").value));
		$("skey").value = "请输入游戏名称";
	}
}
function sk(key){
	window.open("http://Flash.JxGame.Net/search.asp?searchword="+encodeURIComponent(key));
}
function checkRtn(){
	if(event.keyCode == 13){
		gameSearch();
	}
}
function clearTips(){
	var o = $("skey");
	if(trim(o.value) == "请输入游戏名称"){
		o.value = "";
	}
}
function defaultTips(){
	var o = $("skey");
	if(trim(o.value)=='')
	{o.value='请输入游戏名称'}
}
function initSearch(){
	$("btn_search").onclick = gameSearch;
	$("skey").onkeypress = checkRtn;
	$("skey").onblur = defaultTips;
	$("skey").onfocus = clearTips;
}
function initSlideShow(){
	slideshow=new TINY.slider.slide('slideshow',{
		id:'slider',
		auto:3,
		resume:true,
		vertical:true,
		navid:'pagination',
		activeclass:'on',
		position:0
	});
	$("icon1").onmouseover = function(){slideshow.pos(0);}
	$("icon2").onmouseover = function(){slideshow.pos(1);}
	$("icon3").onmouseover = function(){slideshow.pos(2);}
	$("icon4").onmouseover = function(){slideshow.pos(3);}
}
function switch0(i){
	$("li_0_0").setAttribute("class","");
	$("li_0_0").setAttribute("className","");
	$("li_0_1").setAttribute("class","");
	$("li_0_1").setAttribute("className","");
	$("li_0_"+i).setAttribute("class","on");
	$("li_0_"+i).setAttribute("className","on");
	if(i == 0){
		$("yue_renqi").style.display = "block";
		$("zong_renqi").style.display = "none";
	}else if(i == 1){
		$("yue_renqi").style.display = "none";
		$("zong_renqi").style.display = "block";
	}
}
function switch1(i){
	$("li_1_0").setAttribute("class","");
	$("li_1_0").setAttribute("className","");
	$("li_1_1").setAttribute("class","");
	$("li_1_1").setAttribute("className","");
	$("li_1_"+i).setAttribute("class","on");
	$("li_1_"+i).setAttribute("className","on");
	if(i == 0){
		$("rank_pingfen").style.display = "block";
		$("rank_pinglun").style.display = "none";
	}else if(i == 1){
		$("rank_pingfen").style.display = "none";
		$("rank_pinglun").style.display = "block";
	}
}
function typeClassOn(typeid){
	var arr = ["li0","li1","li2","li3","li4","li5","li6","li7","li8","li9","li10","li11"];
	for(var p in arr){
		$(arr[p]).setAttribute("class",(arr[p]=="li0")?"n1":"");
		$(arr[p]).setAttribute("className",(arr[p]=="li0")?"n1":"");
	}
	$("li"+typeid).setAttribute("class",(typeid==0)?"n1 on":"on");
	$("li"+typeid).setAttribute("className",(typeid==0)?"n1 on":"on");
}
function typeDivOn(typeid){
	var arr = ["div0","div1","div2","div3","div4","div5","div6","div7","div8","div9","div10","div11"];
	for(var p in arr){
		$(arr[p]).style.display = "none";
	}
	$("div"+typeid).style.display = "block";
}
function switch_type(typeid){
	typeClassOn(typeid);
	typeDivOn(typeid);
}
function initSwitch(){

	//$("li_0_0").onclick=function(){switch0(0);}
	//$("li_0_1").onclick=function(){switch0(1);}
	//$("li_1_0").onclick=function(){switch1(0);}
	//$("li_1_1").onclick=function(){switch1(1);}
	$("li0").onclick=function(){switch_type(0);}
	$("li1").onclick=function(){switch_type(1);}
	$("li2").onclick=function(){switch_type(2);}
	$("li3").onclick=function(){switch_type(3);}
	$("li4").onclick=function(){switch_type(4);}
	$("li5").onclick=function(){switch_type(5);}
	$("li6").onclick=function(){switch_type(6);}
	$("li7").onclick=function(){switch_type(7);}
	$("li8").onclick=function(){switch_type(8);}
	$("li9").onclick=function(){switch_type(9);}
	$("li10").onclick=function(){switch_type(10);}
	$("li11").onclick=function(){switch_type(11);}
}
function creat_pcgame_list(){
	if(typeof(saleGames)=='undefined'){return;}
	var str=[];
	for(var i=0;i<saleGames.length;i++){
		var p=saleGames[i];
		if(i<3){
			str[i]='<li class="tophot"><span class="num num'+(i+1)+'">'+(i+1)+'</span><a href="'+p.url+'" target="_blank" title="'+p.productName+'">'+p.productName+'</a><span class="hotnum">已售出：<strong>'+p.soldNum+'</strong>件</span></li>'
		}else{
			str[i]='<li><span class="num">'+(i+1)+'</span><a href="'+p.url+'" target="_blank" title="'+p.productName+'">'+p.productName+'</a><span class="hotnum"><strong>'+p.soldNum+'</strong>件</span></span></li>'
		}
	}
	$('ul_pcgame_list').innerHTML=str.join('');
	$('div_pcgame').style.display='block';
	
	var lis=$('game_list_1').getElementsByTagName('li');
	for(i=lis.length-6;i<lis.length;i++){
		lis[i].style.display='none';
	}
	lis[lis.length-7].className='nobg';
	
}
function creat_pcgame_tags(){
	if(typeof(gameTypes)=='undefined'){return;}
	gameTypes=gameTypes.sort(function(){return Math.random()>0.5?1:0})
	var str=[];
	var len=0;
	for(var i=0;i<gameTypes.length;i++){
		var p=gameTypes[i];
		len+=p.name.length;
		if(len>25){
			break;
		}
		str[i]='<a class="'+(i==0?'on':'')+'" href="'+p.url+'" target="_blank">'+p.name+'</a>';
	}
	$('tag_pcgame').innerHTML='<h3 class="tagt8">单机类：</h3>'+str.join('&nbsp;');
	$('tag_pcgame').style.display='';
}
function init(){
	checkHistory();
//	initSlideShow();
	initSwitch();
}
init();
LAZY.init();
LAZY.run();
/*
for(var i=0;i<2;i++){
	var lis=$('game_list_'+i).getElementsByTagName('li');
	lis[lis.length-1].className='nobg';
}
*/
