//handles the AJAX requests - this method is compatible with all versions of IE
var objfavoritesxml;
var gamerequest = true;

var domainpath = '';

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  objfavoritesxml=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  objfavoritesxml=new ActiveXObject("Microsoft.XMLHTTP");
}

function saveGame(gameid) {
	
	objfavoritesxml.open("POST", ''+domainpath+'/external/save_game.php?gameid='+ gameid +'',true);
		
	objfavoritesxml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var form = $('#favorites_form');
	objfavoritesxml.send(form.serialize());
	objfavoritesxml.onreadystatechange=favoritesHandler;
}

//Handle the callbacks from the script
favoritesHandler=function(data) {
	
	if (objfavoritesxml.readyState==4 && objfavoritesxml.status==200) {
		
		if(objfavoritesxml.responseText == 'Game is already saved to your favorites.'){
			
			gamerequest = false;
			$('.favorites_status').html('Game is already saved.');
			$('.favorites_status').fadeOut(4000, function() {
			
				$(this).html('');
				$(this).show();
				
			});
			
			
		} else {
			
			$('.favorites_status').html(objfavoritesxml.responseText);
			$('.favorites_status').fadeOut(4000, function() {
			
				$(this).html('');
				$(this).show();
				
			});
			
		}	
		
		
	}
	
}


jQuery(document).ready(function() {
	
	$('.favorites_btn').click(function() {
		
		if(gamerequest !== false){
			var game_id = $(this).attr('id');
			saveGame(game_id);
		} else {
		
			$('.favorites_status').html('Game is already saved.');
			$('.favorites_status').fadeOut(4000, function() {
			
				$(this).html('');
				$(this).show();
				
			});
			
		}
		
		
	});
	
});

