function SendToFriend ( GameUrl ) {
	$( '#SendToFriendStatus' ).hide ();
	$( '#btnSendToFriend' ).attr ( 'disabled', 'disabled' );

	FufiPostMessage (
		'action/games_Game/SendToFriend',
		{
			gameUrl: GameUrl,
			recipient: $('#txtSendToFriendRecipient').val (),
			message: $('#txtSendToFriendMessage').val (),
			senderEmail: $('#txtSendToFriendYourEmail').val (),
			senderName: $('#txtSendToFriendYourName').val ()
		},
		'SendToFriendStatus',
		function ( Response ) {
			$('#btnSendToFriend').attr ( 'disabled', '' );

			if ( Response.IsError () == false ) {
				TrackPage ( '/game/share/send-to-friend' );
				$( '#txtSendToFriendRecipient' ).val ( '' );
			}
		}
	);

	return false;
}

function OpenSendToFriend () {
	$( '#FlashGameContainer' ).css ( 'visibility', 'hidden' );
	$( '#SendToFriendStatus' ).hide ();

	$( document ).bind (
		'keyup.sendToFriend',
		function ( event ) {
			if ( event.keyCode == 27 ) {
				CloseSendToFriend ();
			}
		}
	);

	$.blockUI ( {
		message: $( '#SendToFriendContainer' ),
		css: {
			border: 'none',
			width: '100px',
			top: '20%',
			cursor: ''
		}
	} );
}

function CloseSendToFriend () {
	$( document ).unbind ( 'keyup.sendToFriend' );
	$( '#FlashGameContainer' ).css ( 'visibility', 'visible' );
	$.unblockUI ();
}

function UpdateNumOfChars ( NumOfEnteredChars ) {
	var maxNumOfChars = 500;
	var numOfCharsLeft = maxNumOfChars - NumOfEnteredChars;

	$( '#NumOfCharsLeft' ).html ( numOfCharsLeft );

	if ( numOfCharsLeft < 0 ) {
		$( '#btnAddComment' ).attr ( 'disabled', 'disabled' );
	} else {
		$( '#btnAddComment' ).attr ( 'disabled', '' );
	}
}

function ShowMessage ( MessageDiv, Response ) {
	var cssClass = 'AjaxError';
	if ( Response.IsError () == false ) {
		cssClass = 'AjaxMsg';
	}

	$( '#' + MessageDiv ).html ( Response.GetContent () ).removeClass ( 'AjaxError' ).removeClass ( 'AjaxMsg' ).addClass ( cssClass ).fadeIn ( 'slow' );
}

function TrackPage ( path ) {
	//urchinTracker ( Path );
	_gaq.push ( [ '_trackPageview', path ] );
}

function OpenWindow ( Url, Width, Height ) {
	window.open ( Url, 'i123_win', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=' + Height + ',width=' + Width );
}

function AddToFavorites ( GameId ) {
	FufiPost (
		'action/games_Game/AddToFavorites',
		{
			GameId: GameId
		},
		function () {
			$( '#AddToFavoritesLink' ).hide ();
			$( '#AddToFavoritesSpan' ).show ();
		}
	);
}

function AddComment () {
	var msgDiv = $( '#AddCommentStatus' );
	msgDiv.hide ();

	var gameId = $( '#GameId' ).val ();
	var comment = $( '#txtComment' ).val ();

	if ( gameId != '' && comment != '' ) {
		$( '#btnAddComment' ).attr ( 'disabled', 'disabled' );

		FufiPost (
			'action/games_Game/AddComment',
			{
				GameId: gameId,
				txtComment: comment
			},
			function ( Response ) {
				if ( Response.IsError () == false ) {
					$( '#txtComment' ).val ( '' );
					$( '#NumOfCharsLeft' ).html ( 500 );
					GetComments ( gameId, 1 );
				}
				else {
					msgDiv.html ( Response.GetContent () ).fadeIn ( 'slow' );
				}

				$( '#btnAddComment' ).attr ( 'disabled', '' );
			}
		);
	}

	return false;
}

function GetComments ( GameId, Page ) {
	FufiGet (
		'action/games_Game/GetComments',
		{
			GameId: GameId,
			Page: Page
		},
		function ( Response ) {
			$( '#CommentsContainer' ).html ( Response.GetRawContent () );
		}
	);

	return false;
}

function DeleteComment ( CommentId, GameId ) {
	if ( confirm ( 'Ali res želite izbrisati ta komentar?' ) ) {
		FufiPost (
			'action/games_Game/DeleteComment',
			{
				CommentId: CommentId,
				GameId: GameId
			},
			function () {
				GetComments ( GameId, $( '#PageNum' ).val () );
			}
		);
	}

	return false;
}

// stars
var starTimer = null;

var starTextEl = null;
var starStrings = Array ();
starStrings[2] = 'slabo';
starStrings[4] = 'še kar';
starStrings[6] = 'dobro';
starStrings[8] = 'zelo dobro';
starStrings[10] = 'odlično';

var inStar = Array ();
inStar[2] = false;
inStar[4] = false;
inStar[6] = false;
inStar[8] = false;
inStar[10] = false;

var gameID, rate, ratedBy;

function StarMouseMove ( StarNum )
{
	if ( inStar[StarNum] == true )
		return;
	else
		inStar[StarNum] = true;

	ClearStarTimer ();

	$('#star_' + StarNum ).css ( 'cursor', 'pointer' );

	//starTextEl.innerHTML = starStrings[StarNum];
	$('#StarsText').html ( starStrings[StarNum] );
	for ( var i = 2; i <= 10; i += 2 )
	{
		if ( i <= StarNum )
			document.getElementById ( 'star_' + i ).src = '/images/app/star_big.gif';
		else
			document.getElementById ( 'star_' + i ).src = '/images/app/blank_star_big.gif';	
	}
}

function StarMouseOut ( StarNum )
{
	inStar[StarNum] = false;
	starTimer = setTimeout ( 'GenerateStars ( ' + rate + ', ' + ratedBy + ', true )', 400 );
}

function ClearStarTimer ()
{
	if ( starTimer )
	{
		clearTimeout ( starTimer );
		starTimer = null;
	}
}

function SaveVote ( GameID, Mark )
{
	ClearStarTimer ();

	FufiPost (
		'action/games_Game/SaveRate',
		{
			id: GameID,
			mark: Mark
		},
		function ( Response )
		{
			GetStars ( gameID, false );
			document.getElementById ( 'StarsStatus' ).innerHTML = 'igra ocenjena';
		}
	);
}

function GetStars ( GameID, EnableVote )
{
	gameID = GameID;

	FufiGet (
		'action/games_Game/GetStars',
		{
			id: GameID
		},
		function ( Response )
		{
			rate = Response.GetRawContent ().substring ( 0, Response.GetRawContent ().indexOf ( ';' ) );
			ratedBy = Response.GetRawContent ().substring ( Response.GetRawContent ().indexOf ( ';' ) + 1 );

			GenerateStars ( rate, ratedBy, EnableVote );
		}
	);
}

function GenerateStars ( Rate, Votes, EnableVote )
{
	var starsHolder = document.getElementById ( 'Stars' );
	starsHolder.innerHTML = '';

	$('#StarsText').html ( 'ocen: ' + Votes );

	if ( EnableVote == true )
		$('#StarsStatus').html ( 'oceni igro' );
	else
		$('#StarsStatus').html ( 'igra ocenjena' );

	ClearStarTimer ();
	for ( var i = 2; i <= 10; i += 2 )
	{
		var newStar = document.createElement ( 'IMG' );
		newStar.setAttribute ( 'id', 'star_' + i );

		if ( i <= Rate )
			newStar.setAttribute ( 'src', '/images/app/star_big.gif' );
		else if ( i > Rate && ( i - Rate ) < 2 )
			newStar.setAttribute ( 'src', '/images/app/half_star_big.gif' );
		else
			newStar.setAttribute ( 'src', '/images/app/blank_star_big.gif' );

		if ( EnableVote == true )
		{
			newStar.onclick = new Function ( "SaveVote('" + gameID + "', '" + i + "');" );
			newStar.onmousemove = new Function ( 'StarMouseMove ( "' + i + '");' );
			newStar.onmouseout = new Function ( 'StarMouseOut ( "' + i + '");' );
		}

		starsHolder.appendChild ( newStar );
	}
}