/**
 * Fonctions pour les affichages des popup
 *
 * @copyright		Copyright 2005-2010, Eggboxstudio. (http://www.eggboxstudio.com)
 * @link			http://www.eggboxstudio.com
 * @package			webroot
 * @subpackage		webroot.js
 * @controller		exemple
 * @version			1.0
 */

// -----------------------------------------------------------------------------
//
//											Initialisation
//
// -----------------------------------------------------------------------------

function initPopupDelete(txt, id, url)
{
	var popup_name = '';
	$('popup.alert container content p.libelle').text(txt);
	$('popup.alert container content p.id').text(' id  : '+ id);
	$('popup.alert button.close').die().unbind().live('click', function() {
		popupClose('popup.alert');
	});
	$('popup.alert button.delete').die().unbind().live('click', function() {
		$('popup.alert button.delete').die().unbind();
		document.location = url;
	})
}

// -----------------------------------------------------------------------------
//
//											Loader
//
// -----------------------------------------------------------------------------

function showLoader()
{
	popupPosition($('loader').find('div').get(0), true, true);
	
	$('loader').fadeIn(
		300
	);
}

function hideLoader()
{
	$('loader').fadeOut(
		300
	);
}

// -----------------------------------------------------------------------------
//
//											Overlay
//
// -----------------------------------------------------------------------------

/**
 * Initialisation de l'overlay
 */
function initOverlay()
{

	$('#backstretch img').click(function(){
		popupClose('popup');
	});
}

/**
 * Affichage de l'overlay, avec possibilité de callback
 */
function showOverlay(type,callback)
{
	// on affiche
	switch(type) {
		case 'topup':
			$.backstretch("/img/bg_toperlay.png");
			initOverlay();
			break;
		case 'popup':
		default:
			$.backstretch("/img/bg_toperlay.png");
			initOverlay();
			break;
	}
	// callback
	if (typeof callback == 'function') {
		callback();
	}
}

/**
 * Cache l'overlay, avec possibilité d callback
 */
function hideOverlay(callback)
{

	$('#backstretch').fadeOut(
		200,
		function() {
			if (typeof callback == 'function') {
				callback();
			}
			$('#backstretch').remove();
		}
	);
}

// -----------------------------------------------------------------------------
//
//											Classique
//
// -----------------------------------------------------------------------------

/**
 * Ouvre une popup
 *
 * @param Mixed object		est l'element que l'on souhaite ouvrir
 * @param String class		determine le type de popup
 * @param Number width		est sa taille en pixel
 * @param String content	Content est du contenu html envoye directement dans la fonction
 */
function popupOpen(object_id, type, width, content, addBtn)
{
	// on crée la popup dans le dom
	$('body').append('<div id="popup" class="popup" open="false"><div id="popup_container"></div><div id="popup_footer"></div></div>');

	addBtn = (addBtn=='' && addBtn != false) ? true : false;

	type = (type=='') ? 'popup' : type;
	width = (width=='') ? 'auto' : width + 'px';

	// on enleve la css
	$('#'+object_id).removeClass().addClass(type);
	
	// on masque l'element, on dit qu'il s'ouvre et on met la largeur'
	$('#'+object_id).attr('open','true').hide().css({width:width});
	
	// on remplit
	if(content) {
		$('#'+object_id+' #popup_container').html(content);
		initUi('#'+object_id+' #popup_container');
	}	
	
	// on met a jour la position
	popupPosition(object_id,true,!(type=='topup'));

	// on ajoute le bouton de fermeture de la popup
	$('#'+object_id).find('#popup_footer').prepend('<button class="mini right" onclick="javascript: popupClose(\'popup\');">Fermer</button>');
	
	
	// on affiche
	switch(type) {
		case 'topup':
			var _object_height = $('#'+object_id).height() + 30;
			$('#'+object_id).css({top:'-'+_object_height+'px',display:'block'});
			showOverlay('topup',function(){
				$('#'+object_id).animate(
					{top:'-10px'},
					200
				);
			});
			break;

		case 'popup':
		default:
			showOverlay('popup',function(){
				$('#'+object_id).css({display:'block'});
			});
			break;
	}
}

/**
 * Fermeture d'une popup
 *
 * @param Mixed object
 */
function popupClose(object_id)
{
	var object = $('#'+object_id).get(0);
	// modifie l'attribut d'ouverture
	$(object).attr('open','false');
	var _object_class = $(object).attr('class');
	var _object_height = $(object).height() + 30;

	switch(_object_class) {

		case 'topup':
			$(object).animate(
				{top:'-'+_object_height+'px'},
				150,
				function(){
					hideOverlay(function() {
						$('#popup').remove();
					});
				}
			);
			break;

		case 'popup':
		default:
			$(object).fadeOut(200, function() {
				hideOverlay(function() {
					$('#popup').remove();
				});
			});
			break;
	}
}

// -----------------------------------------------------------------------------
//
//											Message Flash
//
// -----------------------------------------------------------------------------

function displayFlashMessage(type, message)
{
	var flash = $('body div.flashMessage').get(0);

	$(flash).attr('class', 'flashMessage');	
	$(flash).addClass('flashMessage_' + type);
	
	popupPosition($(flash), true, false);
	
	$(flash).html(message);
	$(flash).slideDown(300);

	var sec = 2500;
	if (type == 'alert') {
		sec = 5000;
	}

	setTimeout(function(){
		$(flash).slideUp(300);
	},sec);
	
}

// -----------------------------------------------------------------------------
//
//											Utils popup
//
// -----------------------------------------------------------------------------

/**
 * @param Mixed object est l'objet que le repositionne
 * @param Boolean x déterminent si on repositionne sur x
 * @param Boolean y déterminent si on repositionne sur y
 */
function popupPosition(object_id,x,y)
{
	var object = $('#'+object_id).get(0);

	var pageWidth	= parseInt($(window).width());
	var pageHeight	= parseInt($(window).height());
	var pageCenter	= parseInt(pageWidth/2);
	var pageMiddle	= parseInt(pageHeight/2);

	var objectW = parseInt($(object).outerWidth(true));
	var objectH = parseInt($(object).outerHeight(true));

	var objectX = parseInt(pageCenter-(objectW/2));
	var objectY = parseInt(pageMiddle-(objectH/2));

	if(x) {
		$(object).css({left:objectX+'px'});
	}

	if(y) {
		$(object).css({top:objectY+'px'});
	}else {
		$(object).css({top:'-10px'});
	}

}
// -----------------------------------------
// FONCTION AU REDIMENSIONEMENT DE LA PAGE
$(window).resize(function() {
	var _object_class = $('#popup').attr('class');
	popupPosition('popup',true,!(_object_class=='topup'))
});
