var loginAskLock = false;
var defaultModalTimeout = 1800;

$(document).ready(function() {
	if ($("#inProgress").size() > 0) {
		$("#inProgress").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#showGame").size() > 0) {
		$("#showGame").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'overlayShow'		: true,
			'hideOnContentClick'	: true,	
			'centerOnScroll'      : true,
			'showCloseButton' : false,
			'onStart'			: function() { 
				window.setTimeout('$.fancybox.close();',defaultModalTimeout);
			}
		});
	}
	if ($("#showGamePreview").size() > 0) {
		$("#showGamePreview").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#signUpGame").size() > 0) {
		$("#signUpGame").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#alreadyQueued").size() > 0) {
		$("#alreadyQueued").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#tooMuchGamesInQueue").size() > 0) {
		$("#tooMuchGamesInQueue").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#upgrade").size() > 0) {
		$("#upgrade").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'overlayShow'		: true,
			scrollable          :   true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	if ($("#choicesBeforeRent").size() > 0) {
		$("#choicesBeforeRent").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'overlayShow'		: true,
			hideOnContentClick	: false,	
			centerOnScroll      : true
		});
	}
	$('#addInQueue').click(function() {
		// get the selected information to add game in queue
		var selectedGame = $("#gameRentingContainer").find(".selected");
		if (selectedGame.size() == 1) {
			var gameId = $(selectedGame).find(".gameId").val();
			var gameName = $(selectedGame).find(".gameName").val();
			gameName = $.url.decode(gameName);
		}
		insertInQueue(gameId, gameName);
		return false;
	});
	$('#dontAddInQueue').click(function() {
		$.fancybox.close();
		return false;
	});
	$('#continueRenting').click(function() {
		$.fancybox.close();
		showFancy("choicesBeforeRent");
		return false;
	});
});

function showFancy(fancyToDisplay) {
	$.fancybox.close();
	$("#"+fancyToDisplay).click();
	$.fancybox.resize();
	
}

function disabledRentingImg(gameId) {
	var rentItLink = $("#rentItLink_"+gameId).get(0);
	displayLink(rentItLink, false);
}

function handleGetData(str, gameId, gameName) {
	if (str == 'showGame' || str == 'showGamePreview' ) {
		// get the game name by the information link
		$('#'+ str +'Div .gameInfo').html(gameName);
		// update the image status
		disabledRentingImg(gameId);
	} 
	if (str == 'alreadyQueued') { // to be sure the process will work properly  
		disabledRentingImg(gameId);
	}
	showFancy(str);
}

function setSelected(wrapperGame) {
	// remove the last selected game if exist
	if ($('#gameRentingContainer').find(".selected").size()==1) {
		$('#gameRentingContainer').find(".selected").removeClass("selected");
	}
	// this game is flaged like selected with a css class
	$(wrapperGame).addClass('selected');
}

function choicesBeforeRent(gameElement) {
	var wrapper = $(gameElement).parent(); 
	setSelected(wrapper);
	var cusId = $(wrapper).find(".customerId").val(); 
	
	if (!isLinkDisplayed( $(wrapper))) {
		showFancy("alreadyQueued");
	} else {
		$("#choicesBeforeRent").click();
	}
}

function insertInQueue(gameId, gameName) {
	showFancy("inProgress");
	
	queueInsert.insertInQueue(gameId,{
		  callback:function(str) { handleGetData(str, gameId, gameName); },
		  errorHandler:function(message) { 
			  		//alert("ajax request failed : " + message);
			  		$.fancybox.close();
			  	}
		});
}

