$(document).ready(function() {
	var page = window.location.href;
	page = page.split('/')[3].split('?')[0];
	switch(page)
	{
		case 'category':
		case 'cat.php':
			page = 'cat';
			break;
		case 'play':
		case 'game.php':
			page = 'game';
			break;
		case 'user':
			page = 'user';
			break;
		case 'docs':
			page = 'docs';
			break;
		default:
			page = 'index';
			break;
	}
//box which automatically
//shows the next site after 10 seconds
	var cache = [];

	// in some pages full games list is visible by default
	/*if($('#fullGamesList').is(':visible'))
	{
		$('#fullGamesListButton').css({color: 'darkred'});
	}*/
	$('#fullGamesListButton').css({color: 'darkred'});
	$('#fullGamesListButton').bind('click', function() {
		if($('#fullGamesList').is(':visible'))
		{
			$(this).css({color: 'black'});
		}
		else
		{
			$(this).css({color: 'darkred'});
		}
		$('#fullGamesList').slideToggle();
	});

	// Top Ten Games section
	$('#topTenGamesList li:eq(0)').addClass('topHover');
	$('#topTenGamesList li a').bind('click', function(ev) {
		var $li = $(this).parent();
		var gameId = $li.attr('class');
		gameId = gameId.split('-')[1];
		gameId = parseInt(gameId);

		$li.siblings().removeClass('topHover');
		$li.addClass('topHover');

		if(!cache[gameId])
		{
			$.getJSON('game_info.php', {id: gameId}, function(obj) {
				cache[gameId] = obj;
				showTop10Game(obj);
			});
		}
		else
		{
			showTop10Game(cache[gameId]);
		}

		ev.preventDefault();
		ev.stopPropagation();
	});
	// End Top Ten Games

	// Featured Games Pagination 
	if($('#latestGamesBox').is('div'))
	{
		var featuredGames = [];
		$('#latestGamesList div.whiteGameBox').each(function(i) {
			var id = $(this).attr('id');
			featuredGames[featuredGames.length] = id.split('featuredGameId-')[1];
		});
		
		
		
		$('#featuredPagination a').bind('click', function(ev) {
			var currentGameId = $('#latestGame').attr('class');
			currentGameId = currentGameId.split('featuredGameId-')[1];

			$this = $(this);
			var gameId = 0;
			if($this.hasClass('prev'))
			{
				for(var i=0; i < featuredGames.length; i++)
				{
					if(featuredGames[i] == currentGameId)
					{
						if(i == 0)
						{
							gameId = featuredGames[featuredGames.length-1];
						}
						else
						{
							gameId = featuredGames[i-1];
						}
					}
				}
			}
			else if($this.hasClass('next'))
			{
				for(var i=0; i < featuredGames.length; i++)
				{
					if(featuredGames[i] == currentGameId)
					{
						if(i == featuredGames.length-1)
						{
							gameId = featuredGames[0];
						}
						else
						{
							gameId = featuredGames[i+1];
						}
					}
				}
			}
			else // 1 2 3...
			{
				// text  contains items position
				var itemNo = parseInt($this.text()) - 1;
				gameId = featuredGames[itemNo];
			}

			if(!cache[gameId])
			{
				$.getJSON('game_info.php', {id: gameId, f: false}, function(obj) {
					cache[gameId] = obj;
					showFeaturedGame(obj);
				});
			}
			else
			{
				showFeaturedGame(cache[gameId]);
			}
			ev.preventDefault();
		});
	}
	
	// next every 10 seconds
	setInterval(function() {
		$('#featuredPagination a.next').trigger('click');
	}, 10000);
	
	function showFeaturedGame(obj)
	{
		if(obj.status)
		{
			$('#latestGame').hide();
			$('#latestGame a.game-url').attr('href', obj.data.url);

			$('#latestGame a.game-url img').attr('src', obj.data.large_img);
			$('#latestGame div.game-title').text(obj.data.title);
			$('#latestGame div.game-description').text(obj.data.description);

			$('#latestGame').attr('class', 'featuredGameId-'+obj.data.id);

			$('#latestGame').slideDown('slow');
		}
		else
		{
			alert('We are sorry but an error occurred. Here is the message that server has returned:\n\n\t'+obj.msg);
		}
	}

	function showTop10Game(obj)
	{
		if(obj.status)
		{
			$('#topTenGamesItem').hide();
			$('#topTenGamesItem a.game-url').attr('href', obj.data.url);
			$('#topTenGamesItem a.game-url img').attr('src', obj.data.large_img);
			$('#topTenGamesItem div.game-title div').text(obj.data.title);
			$('#topTenGamesItem div.game-description').text(obj.data.description);

			//$('#topTenGamesItem').attr('class', 'featuredGameId-'+obj.data.id);

			$('#topTenGamesItem').slideDown('slow');
		}
		else
		{
			alert('We are sorry but an error occurred. Here is the message that server has returned:\n\n\t'+obj.msg);
		}
	}
	// End Featured Games Pagination
});