/**
 * @author boshu
 */
var indexWindowDb = {};
$(document).ready(function(){
	
	//推荐游戏Tab
	$('#point_tab a').click(function(){
		$('#point_tab a').removeClass('current');
		$(this).addClass('current');
		var thisIndex = $('#point_tab>li').index($(this).parent());
		
		$('#point_panel>div').css('display','none');
		$('#point_panel>div')
			.eq(thisIndex)
			.css('display','block')
			.find('.pic').each(function(){
				var self = $(this);
				if (!self.attr('loaded')) {
					var t = self.attr('id').split('_');
					self.html('<img src="' + _c.picPath[t[0]] + indexPicDb[t[1]] + '" />' + self.html());
					self.attr('loaded',1);
				}
			});
		$('#point_link').attr('href',$('#point_tab a').eq(thisIndex).attr('href'));
		return false;
	});
	
	//推荐游戏Tab
	$('#recommend_tab a').click(function(){
		$('#recommend_tab a').removeClass('current');
		$(this).addClass('current');
		var thisIndex = $('#recommend_tab>li').index($(this).parent());
		
		$('#recommend_panel>div').css('display','none');
		$('#recommend_panel>div')
			.eq(thisIndex)
			.css('display','block')
			.find('.pic').each(function(){
				var self = $(this);
				if (!self.attr('loaded')) {
					var t = self.attr('id').split('_');
					self.html('<img src="' + _c.picPath[t[0]] + indexPicDb[t[1]] + '" />' + self.html());
					self.attr('loaded',1);
				}
			});
		$('#recommend_link').attr('href',$('#recommend_tab a').eq(thisIndex).attr('href'));
		return false;
	});
	
	//热门游戏Tab
	$('#hot_tab a').click(function(){
		$('#hot_tab a').removeClass('current');
		$(this).addClass('current');
		
		var thisIndex = $('#hot_tab>li').index($(this).parent());
		if(indexWindowDb['hot'+thisIndex]){
			$('#hot_panel').html(indexWindowDb['hot'+thisIndex]);
		}else{
			$.get(_c.domain+'public/index_hot_'+(thisIndex+1)+'.html',function(body){
				indexWindowDb['hot'+thisIndex] = body;
				$('#hot_panel').html(body);
			});
		}
		$('#hot_link').attr('href',$('#hot_tab a').eq(thisIndex).attr('href'));
		
		return false;
	});
	
	//热门游戏Tab
	$('#new_tab a').click(function(){
		$('#new_tab a').removeClass('current');
		$(this).addClass('current');
		
		var thisIndex = $('#new_tab>li').index($(this).parent());
		if(indexWindowDb['new'+thisIndex]){
			$('#new_panel').html(indexWindowDb['new'+thisIndex]);
		}else{
			$.get(_c.domain+'public/index_new_'+(thisIndex+1)+'.html',function(body){
				indexWindowDb['new'+thisIndex] = body;
				$('#new_panel').html(body);
			});
		}
		$('#new_link').attr('href',$('#new_tab a').eq(thisIndex).attr('href'));
		
		return false;
	});
	
	/**
	 * 延时加载图片初始化
	 */
	$(window).scroll(function(){
		lazyload();
	});
	
	lazyload();
	
});

/*
 * 延时加载图片
 */
var hot_game_load = false;
var game_new_load = false;
var zt_load = false;
var recommend_game_load = false;
var point_game_load = false;
function lazyload(){
	var scrollTop = $(window).scrollTop();
	
	if(scrollTop>100 && !hot_game_load){
		$('#hot_game .pic').each(function(){
			var t = $(this).attr('id').split('_');
			if(t[0]==0){
				var server = _c.gamePicPaths[Math.floor(Math.random()*3)];
				if(typeof(server)=='undefined'){
					server = _c.gamePicPaths[2];
				}
			}else{
				var server = _c.picPath[t[0]];
			}
			$(this).prepend('<img src="'+server+indexPicDb[t[1]]+'" />'+$(this).html());
		});
		indexWindowDb.hot0 = $('#hot_panel').html();
		hot_game_load = true;
	}
	
	if (scrollTop > 600 && !game_new_load) {
		$('#game_new .pic').each(function(){
			var t = $(this).attr('id').split('_');
			if(t[0]==0){
				var server = _c.gamePicPaths[Math.floor(Math.random()*3)];
				if(typeof(server)=='undefined'){
					server = _c.gamePicPaths[2];
				}
			}else{
				var server = _c.picPath[t[0]];
			}
			$(this).prepend('<img src="'+server+indexPicDb[t[1]]+'" />'+$(this).html());
		});
		indexWindowDb.new0 = $('#new_panel').html();
		game_new_load = true;
	}
	
	if (scrollTop > 900 && !zt_load) {
		$('#zt .picb').each(function(){
			var t = $(this).attr('id').split('_');
			if(t[0]==0){
				var server = _c.gamePicPaths[Math.floor(Math.random()*3)];
				if(typeof(server)=='undefined'){
					server = _c.gamePicPaths[2];
				}
			}else{
				var server = _c.picPath[t[0]];
			}
			$(this).prepend('<img src="'+server+indexPicDb[t[1]]+'" />'+$(this).html());
		});
		zt_load = true;
	}
	
	if (scrollTop > 1200 && !point_game_load) {
		$('#point_game .pic').each(function(){
			var t = $(this).attr('id').split('_');
			if(t[0]==0){
				var server = _c.gamePicPaths[Math.floor(Math.random()*3)];
				if(typeof(server)=='undefined'){
					server = _c.gamePicPaths[2];
				}
			}else{
				var server = _c.picPath[t[0]];
			}
			$(this).prepend('<img src="'+server+indexPicDb[t[1]]+'" />');
		});
		point_game_load = true;
	}
	
	if (scrollTop > 1600 && !recommend_game_load) {
		$('#recommend_game .pic').each(function(){
			var t = $(this).attr('id').split('_');
			if(t[0]==0){
				var server = _c.gamePicPaths[Math.floor(Math.random()*3)];
				if(typeof(server)=='undefined'){
					server = _c.gamePicPaths[2];
				}
			}else{
				var server = _c.picPath[t[0]];
			}
			$(this).prepend('<img src="'+server+indexPicDb[t[1]]+'" />'+$(this).html());
		});
		recommend_game_load = true;
	}
	
//	if(hot_game_load && game_new_load && zt_load && recommend_game_load && ph_load){
//		$(window).unbind('scroll');
//	}
}

