$(document).ready( init );

function init()
{
	$("#menu-all-games a").focus( function() { if(this.blur)this.blur(); } );
	$("#menu-top-scores a").focus( function() { if(this.blur)this.blur(); } );
	$("#tab-srcTAF").focus( function() { if(this.blur)this.blur(); } );
	
	$(".game-preview-RU .game-preview-icon a").tooltip(
		{
			bodyHandler: function()
			{
				return $(this).parents(".game-preview-RU").children(".game-preview-tooltip").html();
			},
			track: false, 
			extraClass: "fancy",
			showURL:  false
		}
	);
	
	$(".game-preview-num-big-RU .game-preview-icon-big a").tooltip(
		{
			bodyHandler: function()
			{
				return $(this).parents(".game-preview-num-big-RU").children(".game-preview-tooltip").html();
			},
			track: false, 
			extraClass: "fancy",
			showURL:  false
		}
	);
	
	$("#screens a").click(
		function()
		{
			ScreenClick( $(this).attr( 'id' ) );
			
			return false;
		}
	);
	
	$("#menu-all-games a").click(
		function()
		{
			if ( ($(this).attr( 'href' ) != "") && ($(this).attr( 'href' ) != undefined) ) {
				location.href = $(this).attr( 'href' );
			}

			$( "#menu-all-games li" ).removeClass( 'active' );
			$(this).parents( 'li' ).addClass( 'active' );
			
			TabClick( $(this).attr( 'id' ) );
			
			return false;
		}
	);
	
	$("#menu-top-scores a").click(
		function()
		{
			$( "#menu-top-scores li" ).removeClass( 'active' );
			$(this).parents( 'li' ).addClass( 'active' );
			
			TabClick( $(this).attr( 'id' ) );
			
			return false;
		}
	);
	
	$("#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" );
		} 
	);
	
	$("#tab-srcTAF").toggle( function() { return srcTAFClick(this); }, function() { return srcGRClick( this ); } );
	
	var id = new String( $("#rate-it-window a").attr( 'href' ) );
	var re = /game=(.*)\&/g;
	
	re.test( id );
	id = RegExp.$1;
	CookieName = "rate" + 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-it-hovered"></a>';
		}
		for ( i = Rate; i < 5; i++ )
		{
			Stars += '<a></a>';
		}
		
		$("#rate-it-window").html( Stars );
	}
	
	$("#game-post-comment-button").click(
		function ()
		{
			$("#game-post-comment-window").show();
			
			return false;
		}
	);
	
	$("#game-comments .pagination a").click(
		function()
		{
			$(this).siblings( "a" ).removeClass( 'active' );
			$(this).addClass( 'active' );
			$("#game-comments-list").load( $(this).attr( "href" ), {}, CommentsLoaded );
			return false;
		}
	);
	
	$("#post-comment-button").click(
		function()
		{
			$("#post-form").submit();
			return false;
		}
	);
	
	$("#close-post-window").click(
		function()
		{
			$("#game-post-comment-window").hide();
			return false;
		}
	);
}

function CommentsLoaded( response, status, XMLRequest )
{
	
}

function srcTAFClick( obj )
{
	TabClick( $(obj).attr( 'id' ) );
	
	$(obj).text( 'Game ratings' );
	$(obj).attr( 'id', 'tab-srcGR' );
	
	return false;
}

function srcGRClick( obj )
{
	TabClick( $(obj).attr( 'id' ) );
	
	$(obj).text( 'Tell-a-friend' );
	$(obj).attr( 'id', 'tab-srcTAF' );
	
	return false;
}

function TabClick( tabid )
{
	var s = new String( tabid );
	if ( s.match( /tab\-(.*)/ ) )
	{
		var id = "#" + RegExp.$1;
		$( id ).siblings().hide();
		$( id ).show();
	}
}

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