/**
 * @author Renaud
 */
var SAlert =
{
	DEFAULT_ALERT_TITLE: 'Alerte', // A remplacer quelque part par un sfConfig::get()
	DEFAULT_CONFIRM_TITLE: 'Confirmez',
	DEFAULT_INPUT_TITLE: 'Entrez une valeur',
	DEFAULT_WINDOW_WIDTH: 400,
	
	
	oAlertModal: null,
	pAlertCallbackOnHide: null,
	
	oConfirmModal: null,
	pConfirmCallbacks: null,
	
	oInputModal: null,
	pInputCallbacks: null,
	
	
	
	
	/**
	 * Affiche une alerte jqModal
	 * 
	 * @param {String} sMessage Le message
	 * @param {String} sTitle[optional] Le titre
	 * @param {Function} pCallback[optional] Une fonction à appeler lorsque la fenêtre est cachée
	 * @param {String} sUrlRedirect[optional] Une URL à appeler lorsqu'on ferme la fenêtre
	 */
	alert: function(sMessage, sTitle, pCallback, sUrlRedirect)
	{
		SAlert.init();
		SAlert.oAlertModal.find('.message').html(sMessage);
		SAlert.oAlertModal.find('h1').html(sTitle ? sTitle : SAlert.DEFAULT_ALERT_TITLE);
		SAlert.pAlertCallbackOnHide = (sUrlRedirect ? function(){document.location.href = sUrlRedirect} : (pCallback ? pCallback : null));
		SAlert.oAlertModal.jqmShow();
	},
	
	/**
	 * Appelée quand la fenêtre d'alerte est fermée
	 */
	onHideAlert: function()
	{
		if (SAlert.pAlertCallbackOnHide)
			SAlert.pAlertCallbackOnHide();
	},
	






	/**
	 * Affiche une fenêtre d'entrée
	 * 
	 * @param {String} sMessage
	 * @param {String} sTitle[optional] Le titre
	 * @param {Object} oCallbacks[optional] Les callbacks (onYes, onClose) (onClose = pareil pour cancel)
	 */
	input: function(sMessage, sTitle, oCallbacks, sDefaultValue)
	{
		SAlert.init();
		SAlert.oInputModal.find('.message p').html(sMessage);
		SAlert.oInputModal.find('.message input').val(sDefaultValue ? sDefaultValue : '');
		SAlert.oInputModal.find('h1').html(sTitle ? sTitle : SAlert.DEFAULT_INPUT_TITLE);
		SAlert.pInputCallbacks = $.extend({onYes: null, onClose: null}, oCallbacks);
		SAlert.oInputModal.jqmShow();
	},
	
	/**
	 * Appelée quand la fenêtre de confirmation est fermée
	 */
	onHideInput: function()
	{
		$('.message .warning').remove();
		if (SAlert.pInputCallbacks && SAlert.pInputCallbacks['onClose'])
			SAlert.pInputCallbacks['onClose']();
	},
	
	/**
	 * Appelée quand on choisit "yes" dans la fenêtre de confirmation
	 */
	onInputYes: function()
	{
		$('.message .warning').remove();
		if (SAlert.pInputCallbacks && SAlert.pInputCallbacks['onYes'])
		{
			var sError = SAlert.pInputCallbacks['onYes']($('.message input', SAlert.oInputModal).val());
			if (sError != '')
			{
				$('.message', SAlert.oInputModal).prepend('<div class="warning">' + sError + '</div>');
				$('.message input', SAlert.oInputModal).focus();
				return;
			}
			SAlert.pInputCallbacks['onClose'] = null;
		}
		SAlert.oInputModal.jqmHide();
	},





	
	
	/**
	 * Affiche une fenêtre de confirmation
	 * 
	 * @param {String} sMessage
	 * @param {String} sTitle[optional] Le titre
	 * @param {Object} oCallbacks[optional] Les callbacks (onYes, onNo, onClose) (onClose = pareil pour cancel)
	 */
	confirm: function(sMessage, sTitle, oCallbacks, bShowCancelButton)
	{
		SAlert.init();
		SAlert.oConfirmModal.find('.message').html(sMessage);
		SAlert.oConfirmModal.find('h1').html(sTitle ? sTitle : SAlert.DEFAULT_CONFIRM_TITLE);
		SAlert.pConfirmCallbacks = $.extend({onYes: null, onNo: null, onClose: null}, oCallbacks);
		$('#jqmConfirm_cancel', SAlert.oConfirmModal).toggle(bShowCancelButton == true);
		SAlert.oConfirmModal.jqmShow();
	},
	
	/**
	 * Appelée quand la fenêtre de confirmation est fermée
	 */
	onHideConfirm: function()
	{
		if (SAlert.pConfirmCallbacks && SAlert.pConfirmCallbacks['onClose'])
			SAlert.pConfirmCallbacks['onClose']();
	},
	
	/**
	 * Appelée quand on choisit "yes" dans la fenêtre de confirmation
	 */
	onConfirmYes: function()
	{
		if (SAlert.pConfirmCallbacks && SAlert.pConfirmCallbacks['onYes'])
		{
			SAlert.pConfirmCallbacks['onClose'] = null;
			SAlert.pConfirmCallbacks['onYes']();
		}
		SAlert.oConfirmModal.jqmHide();
	},
	
	/**
	 * Appelée quand on choisit "no" dans la fenêtre de confirmation
	 */
	onConfirmNo: function()
	{
		if (SAlert.pConfirmCallbacks && SAlert.pConfirmCallbacks['onNo'])
		{
			SAlert.pConfirmCallbacks['onClose'] = null;
			SAlert.pConfirmCallbacks['onNo']();
		}
		SAlert.oConfirmModal.jqmHide();
	},
	
	
	
	/**
	 * Initialise les fenêtres modales si ce n'est pas déjà fait
	 */
	init: function()
	{
		if (SAlert.oAlertModal != null)
			return;
		
		SAlert.oAlertModal = $('body').popin(
		{
			content: '	<div class="message"></div>\
						<div class="buttonsContainer adminButtonsCenter">\
							<button class="cheap_btn jqmClose">Ok</button>\
						</div>',
			title: SAlert.DEFAULT_ALERT_TITLE,
			width: SAlert.DEFAULT_WINDOW_WIDTH,
			onHide: SAlert.onHideAlert
		});
		
		SAlert.oConfirmModal = $('body').popin(
		{
			content: '	<div class="message"></div>\
						<div class="buttonsContainer adminButtonsCenter">\
							<button class="cheap_btn" onclick="SAlert.onConfirmYes();">Oui</button>\
							<button class="cheap_btn" onclick="SAlert.onConfirmNo();">Non</button>\
							<button class="cheap_btn jqmClose" id="jqmConfirm_cancel">Annuler</button>\
						</div>',
			title: SAlert.DEFAULT_CONFIRM_TITLE,
			width: SAlert.DEFAULT_WINDOW_WIDTH,
			onHide: SAlert.onHideConfirm
		});
		
		SAlert.oInputModal = $('body').popin(
		{
			content: '	<div class="message">\
							<p></p>\
							<input type="text" />\
						</div>\
						<div class="buttonsContainer">\
							<button class="cheap_btn" onclick="SAlert.onInputYes();">Ok</button>\
							<button class="cheap_btn jqmClose">Annuler</button>\
						</div>',
			title: SAlert.DEFAULT_CONFIRM_TITLE,
			width: SAlert.DEFAULT_WINDOW_WIDTH,
			onHide: SAlert.onHideInput
		});
	}
}

