$(document).ready(function(){
	$('#thumb-index').find('div').bind('click', toggleSelect);
	x = setTimeout("bannerChange()", 3000);
	$('#btn').hover(btnFadeIn, btnFadeOut);
})

isAnimated = false; //this is going to function as a lock to prevent multiple animations happening in a row
t=0;

function toggleSelect(){
	
	clearTimeout(x);
	
	id = $(this).attr('id');
	
	id = parseInt(id.replace('b', ''));
	
	if(id != t && !isAnimated){

		isAnimated = true;

		$('#thumb-index').find('div').removeClass('selected');
		$(this).addClass('selected');

		$('#wrapGames').find('img').fadeOut(300);
		
		setTimeout("$('#wrapGames').find('img').eq(id).fadeIn(300)", 300);
		
		switch(id){
		
			case 0:
				t = 0;
				$('#btn').attr('href', 'http://jd.perfectworld.com');
				break;
				
			case 1:
				t = 1;
				$('#btn').attr('href', 'http://boi.perfectworld.com');
				break;
				
			case 2:
				t = 2;
				$('#btn').attr('href', 'http://www.torchlight2game.com');
				break;
		
		}
		
		setTimeout("isAnimated = false", 600);
		
	}
	
	x = setTimeout("bannerChange()", 3000);
	
}

function bannerChange(){
	
	t++;
	
	$('#wrapGames').find('img').fadeOut(300);
	
	if(t == 1){
		
		setTimeout("$('#wrapGames').find('img').eq(t).fadeIn(300)", 300);
		
	}else if (t ==2){
		
		setTimeout("$('#wrapGames').find('img').eq(t).fadeIn(300)", 300);
	
	}else{
		
		t = 0;
		
		setTimeout("$('#wrapGames').find('img').eq(t).fadeIn(300)", 300);
		
	}
	
	switch(t){
	
	case 0:
		
		$('#btn').attr('href', 'http://jd.perfectworld.com');
		break;
		
	case 1:
		
		$('#btn').attr('href', 'http://boi.perfectworld.com');
		break;
		
	case 2:
		
		$('#btn').attr('href', 'http://www.torchlight2game.com');
		break;

	}
	
	$('#thumb-index').find('div').removeClass('selected');
	$('#thumb-index').find('div').eq(t).addClass('selected');

	x = setTimeout("bannerChange()", 3000);
	
}

function btnFadeIn(){
	
		$('#btn').append('<div id="btn-ro"></div>')
		$('#btn-ro').fadeIn(300);
}

function btnFadeOut(){
	
		$('#btn-ro').fadeOut(300);
		setTimeout("$('#btn-ro').remove()", 300);
	
}

