/* -----------------------------------------------------------------------
   Fuel Industries - All Girl Arcade
   General Javascript
----------------------------------------------------------------------- */

if ( typeof AGA === "undefined" ) { var AGA = {} };

AGA.mypage = {

	msg_pending: function()
	{
		AGA.messenger.summon('Our team will approve your <strong>Username</strong> within the next few days.<br /> Why don\'t you <strong>Play Games</strong> and earn <strong>Gems</strong> while you wait!');
		return false;
	},
	
	bracelets_open: function( userid )
	{
		AGA.mall.overlay( "/www/mypage/modules/bracelets_viewer.php", "userid="+userid, AGA.mypage.bracelets_success, 0 );
		$('.checkout img:first').hide(); 
	},
	
	bracelets_success: function( result, final_id )
	{
		$('.checkout .loading').hide(); 
		$('.checkout .ajaxinhere').html(result).show();
	}
	
}

/* friends ================================ */

AGA.friends = {
   
   init: function()
   {
      $('.addfriend').click( function(){ AGA.friends.addfriend( this ); return false; } );
      
		$('.acceptfriend').click( function(){ AGA.friends.acceptfriend( this ); return false; } );
      $('.denyfriend').click( function(){ AGA.friends.denyfriend_button( this ); return false; } );
      $('.denyfriendsure .yes').click( function(){ AGA.friends.denyfriend( this ); return false; } );
		$('.cancelfriend').click( function(){ AGA.friends.cancelfriend( this ); return false; } );
		
      $('.removefriend').click( function(){ AGA.friends.removefriend_button( this ); return false; } );
      $('.removesure .yes').click( function(){ AGA.friends.removefriend( this ); return false; } );
		
      $('.customname').click( function(){ AGA.friends.customname_button( this ); return false; } );
      $('.renamebox .submit').click( function(){ AGA.friends.customname( this ); return false; } );
		
      $('.overbox .no, .overbox .cancel').click( function(){ AGA.friends.overbox_close( this ); return false; } );
      
      $('.filterfriends').click( function(){ AGA.friends.show_filter_options(); return false; } );
      
      $(document).click( function(){ setTimeout("AGA.friends.hide_filter_options()",200); } );
   },
   
   check: function()
   {
      //console.debug($('.listlayout .row:visible').length);
      
      if( $('.listlayout .row:visible').length == 0 )
      {
         location.reload(true);
      }
   },
   
   removefriend_button: function( that )
   {
      AGA.friends.overbox_close( that );
      $(that).parent().parent().parent().parent().find('.removesure').show();
   },
   
   removefriend: function( that )
   {
      var destination = $(that).parent().parent().parent().find('.removefriend').attr('href')+'&ajax=1';
      var parent = $(that).parent().parent().parent();
      var wait = $(that).parent().parent().parent().find('.overbox.wait');
      
      AGA.friends.overbox_close( that );
      $(wait).show();
      
      $.ajax({
         type: "POST", url: destination, cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
            $(that).fadeOut('fast');
            $(parent).animate( { backgroundColor: '#ffb3b3' }, 'fast').hide();
            AGA.messenger.summon( response.m, response.t );
            
            AGA.friends.check();
         }
      });
      
   },
   
   addfriend: function( that )
   {
      var destination = $(that).attr('href')+'&ajax=1';
      var parent = $(that).parent().parent().parent().parent();
      var wait = $(that).parent().parent().parent().parent().find('.overbox.wait');
      
      $(wait).show();
      
      $.ajax(
		{
         type: "POST", url: destination, cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
				$(that).parent().hide();
				$(parent).find('.waitingrequest').show();
				$(parent).find('.cancelfriend').parent().show();
				AGA.messenger.summon( response.m, response.t );
				
				AGA.friends.check();
         }
      });
   },
   
   acceptfriend: function( that )
   {
      var destination = $(that).attr('href')+'&ajax=1';
      var parent = $(that).parent().parent().parent().parent();
      var wait = $(that).parent().parent().parent().parent().find('.overbox.wait');
      
      $(wait).show();
      
      $.ajax({
         type: "POST", url: destination, cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
				$(parent).animate( { backgroundColor: '#d4f161' }, 'fast').fadeOut('slow');
				AGA.messenger.summon( response.m, response.t );
				
				AGA.friends.check();
         }
      });
   },
	
   denyfriend_button: function( that )
   {
      AGA.friends.overbox_close( that );
      $(that).parent().parent().parent().parent().find('.denyfriendsure').show();
   },
   
   denyfriend: function( that )
   {
      var destination = $(that).parent().parent().parent().find('.denyfriend').attr('href')+'&ajax=1';
      var parent = $(that).parent().parent().parent();
      var wait = $(that).parent().parent().parent().find('.overbox.wait');
      
      AGA.friends.overbox_close( that );
      $(wait).show();
      
      $.ajax({
         type: "POST", url: destination, cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
            $(that).fadeOut('fast');
            $(parent).animate( { backgroundColor: '#ffb3b3' }, 'fast').hide();
            AGA.messenger.summon( response.m, response.t );
            
            AGA.friends.check();
         }
      });
   },
   
   cancelfriend: function( that )
   {
      var destination = $(that).attr('href')+'&ajax=1';
      var parent = $(that).parent().parent().parent().parent();
      var wait = $(that).parent().parent().parent().parent().find('.overbox.wait');
      
      $(wait).show();
      
      $.ajax({
         type: "POST", url: destination, cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
				$(that).parent().hide();
				$(parent).find('.waitingrequest').hide();
				$(parent).find('.addfriend').parent().show();
				AGA.messenger.summon( response.m, response.t );
         }
      });
   },
	
   customname_button: function( that )
   {
      AGA.friends.overbox_close( that );
      $(that).parent().parent().parent().parent().find('.field').val('Create a Nickname for this user.').addClass('unfocused');
      $(that).parent().parent().parent().parent().find('.field').focus( function(){ $(this).val('').addClass('focused').removeClass('unfocused'); } );
      $(that).parent().parent().parent().parent().find('.renamebox').show();
   },
   
   customname: function( that )
   {
      var parent = $(that).parent().parent().parent().parent();
      var field = $(that).parent().parent().parent().parent().find('.field');
      var wait = $(that).parent().parent().parent().parent().find('.overbox.wait');
      var value = $(field).val();
      
      $(wait).show();
      $(that).parent().parent().parent().parent().find('.renamebox').hide();
      
      if( value == 'Create a Nickname for this user.' || value == '' )
      {
         $.ajax({
            type: "POST", 
            url: $(parent).find('.customname').attr('rel')+'&customname='+''+'&ajax=1', 
            cache: false, dataType: "json",
            success: function( response )
            {
               $(wait).hide();
               $(parent).find('.name a').text( $(parent).find('.name .oldname').attr('rel') );
               $(parent).find('.name .oldname').text('');
            }
         });
         
         return false;
      }
      
      $.ajax({
         type: "POST", 
         url: $(parent).find('.customname').attr('rel')+'&customname='+value+'&ajax=1', 
         cache: false, dataType: "json",
         success: function( response )
         {
            $(wait).hide();
            $(parent).find('.name .oldname').text( $(parent).find('.name .oldname').attr('rel') );
            $(parent).find('.name a').text(value);
            AGA.messenger.summon( response.m, response.t );
         }
      });
   },
   
   overbox_close: function( that )
   {
      $('.overbox').hide();
   },
   
   show_filter_options: function()
   {
      $('.filterfriends').addClass('on').unbind('click').click( function(){ AGA.friends.hide_filter_options(); return false; } );
      $('.filteroptions').slideDown('fast');
   },
   
   hide_filter_options: function()
   {
      $('.filterfriends').removeClass('on').unbind('click').click( function(){ AGA.friends.show_filter_options(); return false; } );
      $('.filteroptions').slideUp('fast');
   }
   
}


/* awards ================================ */

AGA.awards = {

   display: function( src, name, description, title, mapping )
   {
      $('.awardslist').css( 'width', '313px');
      $('.awardsdisplay').css( 'display', 'block');
      $('.awardsdisplay .avatar img').attr( 'src', src );
      $('.awardsdisplay .description').text( description );
      $('.awardsdisplay .playnow a').attr( 'href', mapping );
   }

};

/* gifts ================================ */

AGA.gifts = {
   
   open: function( that, id, image, text, username, opened, baseurl )
   {
	  if( opened == 0 )
	  {
		  $("a[rel='gift-"+id+"'] .image").attr('src', image);
		  $("a[rel='gift-"+id+"']").attr('title', text+ ' from ' +username);
		  
		  $.ajax({
			 type: "POST",
			 url: "/www/mypage/modules/open_gift.php",
			 data: 'id='+id,
			 cache: false,
			 success: function( msg ) 
			 {
				AGA.gifts.callmsg( text, username, baseurl );
			 }
		  });
	  }
	  else
	  {
		  AGA.gifts.callmsg( text, username, baseurl );
	  }
   },
   
   callmsg: function( text, username, baseurl )
   {
	   AGA.messenger.summon( 'It\'s a gift from <a href="'+baseurl+'users/'+username+'">'+username+'</a>!!!<br /> You can buy gifts for your friends in <a href="'+baseurl+'themall/gifts/">The Mall</a>.', 'credits');
   }
   
};
