$(document).ready(init);

function closeWindows()
{
	$( "#tell-a-friend-window" ).hide();
	$( "#rate-it-window" ).hide();
	$( "#add-comment-window" ).hide();
	$( "#add-hint-window" ).hide();
	
	return true;
}

function init()
{
	$("#screens a").click(
		function()
		{
			ScreenClick( $(this).attr( 'id' ) );
			
			return false;
		}
	);
	
	$("#rate-it-button").click( 
		function ()
		{
			closeWindows();
			$( "#rate-it-window" ).show();
			
			return false;
		}
	);
	$("#tell-a-friend-button").click(
		function ()
		{
			closeWindows();
			$( "#tell-a-friend-window" ).show();
			
			return false;
		}
	);
	$("#game-add-comment-button").click(
		function()
		{
			closeWindows();
			$( "#add-comment-window" ).show();
			
			return false;
		}
	);
	$("#game-add-hint-button").click(
		function()
		{
			closeWindows();
			$( "#add-hint-window" ).show();
			
			return false;
		}
	);
	
	var id = new String( $("#rate-it-buttons a").attr( 'href' ) );
	var re = /game=(.*)\&/g;
	
	re.test( id );
	id = RegExp.$1;
	CookieName = "rate" + id;
	
	$("#game-all-comments-button").click(
		function()
		{
			$("div.game-comments-content").load( $("#game-all-comments-button").attr( "href" ), {}, CommentsLoaded );
			return false;
		}
	);
	
	$("#game-all-hints-button").click(
		function()
		{
			$("#game-hints-content").load( $("#game-all-hints-button").attr( "href" ), {}, HintsLoaded );
			return false;
		}
	);
	
	
	
	_start = document.cookie.indexOf( CookieName + "=" );
	
	
	
	if ( _start != -1 )
	{
		_start = _start + CookieName.length + 1; 
		_end = document.cookie.indexOf( ";", _start );
		if ( _end == -1 )
		{
			_end = document.cookie.length;
		}
		
		var Rate = unescape( document.cookie.substring( _start, _end ) );
		
		var Stars = "";
		
		for ( i = 0; i < Rate; i++ )
		{
			Stars += '<a class="rate-it-hovered"></a>';
		}
		for ( i = Rate; i < 5; i++ )
		{
			Stars += '<a></a>';
		}
		
		$("#rate-it-buttons").html( Stars );
	}
	else
	{
		$("#rate-it-window a").click( closeWindows );
		
		$("#rate-it-window a").mouseover( 
			function () 
			{ 
				$(this).addClass( "rate-it-hovered" );
				$(this).prevAll( "a" ).addClass( "rate-it-hovered" );
			} 
		);
		$("#rate-it-window a").mouseout( 
			function () 
			{ 
				$(this).removeClass( "rate-it-hovered" );
				$(this).prevAll( "a" ).removeClass( "rate-it-hovered" );
			} 
		);
		
		$("#dl-rate-it-window a").mouseover( 
			function () 
			{ 
				$(this).addClass( "rate-it-hovered" );
				$(this).prevAll( "a" ).addClass( "rate-it-hovered" );
			} 
		);
		$("#dl-rate-it-window a").mouseout( 
			function () 
			{ 
				$(this).removeClass( "rate-it-hovered" );
				$(this).prevAll( "a" ).removeClass( "rate-it-hovered" );
			} 
		);
	}
	
	if ( $(".rate-hint-buttons").length )
	{
		var id = new String( $(".rate-hint-buttons a").attr( 'href' ) );
		var re = /hint=(.*)\&/g;
		
		re.test( id );
		id = RegExp.$1;
		CookieName = "hint" + id;
		
		_start = document.cookie.indexOf( CookieName + "=" );
		
		if ( _start != -1 )
		{
			_start = _start + CookieName.length + 1; 
			_end = document.cookie.indexOf( ";", _start );
			if ( _end == -1 )
			{
				_end = document.cookie.length;
			}
			
			var Rate = unescape( document.cookie.substring( _start, _end ) );
			
			var Stars = "";
			
			for ( i = 0; i < Rate; i++ )
			{
				Stars += '<a class="rate-hint-hovered"></a>';
			}
			for ( i = Rate; i < 5; i++ )
			{
				Stars += '<a></a>';
			}
			
			$("#rate-hint-buttons-" + id).html( Stars );
		}
		else
		{
			$(".dl-rate-hint-window a").mouseover( 
				function () 
				{ 
					$(this).addClass( "rate-hint-hovered" );
					$(this).prevAll( "a" ).addClass( "rate-hint-hovered" );
				} 
			);
			$(".dl-rate-hint-window a").mouseout( 
				function () 
				{ 
					$(this).removeClass( "rate-hint-hovered" );
					$(this).prevAll( "a" ).removeClass( "rate-hint-hovered" );
				} 
			);
		}
	}
	
	$("#rate-it-window img").click( closeWindows );
	$("#tell-a-friend-window img").click( closeWindows );
	$("#add-comment-window img").click( closeWindows );
	$("#add-hint-window img").click( closeWindows );
	
	$( "#check-all" ).click(
		function ()
		{
			if ( $(this).attr( 'checked' ) )
			{
				$("input[name='check-messages[]']").attr( 'checked', true );
			}
			else
			{
				$("input[name='check-messages[]']").attr( 'checked', false );
			}
		}
	);
}

function CommentsLoaded( response, status, XMLRequest )
{
	$("a.game-comments-pagination").click(
		function()
		{
			$("div.game-comments-content").load( $(this).attr( "href" ), {}, CommentsLoaded );
			return false;
		}
	);
	$("#game-all-comments-button").click(
		function()
		{
			$("div.game-comments-content").load( $("#game-all-comments-button").attr( "href" ), {}, CommentsLoaded );
			return false;
		}
	);
}

function HintsLoaded( response, status, XMLRequest )
{
	$("a.game-hints-pagination").click(
		function()
		{
			$("#game-hints-content").load( $(this).attr( "href" ), {}, HintsLoaded );
			return false;
		}
	);
	$("#game-all-hints-button").click(
		function()
		{
			$("#game-hints-content").load( $("#game-all-hints-button").attr( "href" ), {}, HintsLoaded );
			return false;
		}
	);
}

function ScreenClick( tabid )
{
	var s = new String( tabid );
	
	var id = "#g-content-" + s;
	$( id ).siblings().hide();
	$( id ).show();
}
