/* -----------------------------------------------------------------------
	Fuel Industries - All Girl Arcade
	Games Javascript
----------------------------------------------------------------------- */

keepalive();

if( typeof AGA == "undefined"  ) var AGA = {};

AGA.games = {
  
   mapping: '',
   gameid: 0,
   
   init: function( mapping, gameid )
   {
      this.mapping = mapping;
      this.gameid = gameid;
   },
   
   refresh: function()
   {
      var that = this;
      $('#loadawards').load( '/www/games/modules/get_awards.php', { force: 1, mapping: that.mapping } );
      $('#leaderboardbox').load( '/www/games/modules/get_leaderboard.php', { mapping: that.mapping } );
   },
   
   refresh_all: function()
   {
      this.refresh_awards();
      this.refresh_score();
      this.refresh_user();
   },
   
   refresh_awards: function()
   {
      $('#loadawards').load( '/www/games/modules/get_awards.php', { force: 1, mapping: AGA.games.mapping } );
   },

   refresh_score: function()
   {
      $('#leaderboardbox').load( '/www/games/modules/get_leaderboard.php', { mapping: AGA.games.mapping } );
   },
   
   refresh_user: function()
   {
      $('#userbox').load( '/www/html/userbox.php', { ajax: 1 } );
   }
   
};

AGA.rating = {
	
	init: function( gameid, mapping )
	{
		$('.rater a').mouseover( function()
		{
			var start = $(this).attr('id').match(/([0-9])/)[0];
			
			for( var i=1; i<=5; i++ )
			{
				if( i <= start ) $('.rater #rate'+i).addClass('full').removeClass('empty');
				else $('.rater #rate'+i).addClass('empty').removeClass('full');
			}
		});
		
		$('.rater a').mouseout( function()
		{
			$('.rater a').removeClass('full').removeClass('empty');
		});
		
		$('.rater a').click( function()
		{
			AGA.rating.rate( $(this).attr('id').match(/([0-9]+)/)[0], gameid, mapping );
		});
      
	},
	
	rate: function( rating, gameid, mapping )
	{
		$.ajax({
			type: 'POST',
			url: '/www/games/modules/add_rating.php',
			data: 'rating='+rating+'&gameid='+gameid+'&mapping='+mapping,
			dataType: 'json',
			cache: false,
			success: function( response ) 
			{ 
				if( response.error )
				{
					AGA.messenger.summon( response.error );
				}
				else
				{
					$('#ratetext').html('<strong>This Game\'s Rating</strong><small>'+response.score+' rating from '+response.votes+' votes</small>');
					
					$('.rater a').remove();
					
					$('.rater .full').removeClass('full').addClass('empty');
					
					$('.rater span').each( function( i, n )
					{ 
						if( i < response.score )
						{
							$(n).removeClass('empty').addClass('full');
						}
					});
					
					AGA.messenger.summon( response.success, 'credits' );
					
					$("#ratebox").animate( { backgroundColor: '#ffffff' }, 'fast').animate( { backgroundColor: fadeColour }, 'slow');
					
				}
			}
		});
		return false;
	}
	
};

AGA.stf = {
	
	init: function()
	{
		$('#challengebtn').click( function()
		{
			var qs = $('#challengeform').serialize();
			
			$('#challengeform .loading').show();
			
			$.ajax({
				type: 'POST',
				url: $('#challengeform').attr('action'),
				data: qs,
				dataType: 'json',
				cache: false,
				success: function( response ) 
				{
					if( response.success )
					{
						AGA.messenger.summon( response.success ); 
						$('#challengeform input:text').val('');
					}
					else if( response.error )
					{
						AGA.messenger.summon( response.error, 'error'); 
					}
					$('#challengeform .loading').hide();
				}
			});
			return false;
		});
	}

};

	
	
