/*
 *
 *	@titre: popup.
 *	@description: javascript popup - Le Veau.
 *	@auteur: neov - http://www.neov.net.
 *	@creation: 20090506.
 *	@modification: -.
 *
*/

var fadeTime = 300;
var lastOpen = null;

function afficherMasque()
{
	var w = $('body').width();
	var mh = $('body').height();
	var ih = $(document).height();

	if ( mh < ih ) mh = ih;		

	$('#masque')
		.css({width: w + 'px', height: mh +'px', opacity: 0.5, filter:'Alpha(Opacity=50)'})
		.fadeIn(fadeTime);

}

// affichage popup

$.fn.showPop = function(el, f)

{
	
	$(this).each(
		function()
		{
			$(this).click(
				function()
				{
					if (f) f.call();
					
					$(el).showElem();
					
					return false;
				}
			);
		}
	);
	
}


$.fn.showPopListeMoe = function(el, f)

{
	
	$(this).each(
		function()
		{
			$(this).click(
				function()
				{
					if (f) f.call();
					
					$(el).showElemListeMoe();
					
					return false;
				}
			);
		}
	);
	
}

// affichage des elements du popup

$.fn.showElem = function()
{ 
	var extraPos = 0;
	if( $(window).height() < $(this).height() ) extraPos = 150;
	var tPos = ( $(window).height() - $(this).height() )/2 + $(window).scrollTop() + extraPos;
	var lPos = ( $(window).width() - $(this).width() )/2;
	if (lastOpen != null) $(lastOpen).fadeOut(fadeTime);
	$(this)
		.fadeIn(fadeTime)
		.css({ top: tPos + 'px', left: lPos + 'px' });
		//.css({ top: '350px', left: lPos + 'px' });
		
	afficherMasque();
	
	lastOpen = this;

	return false;
}

$.fn.showElemListeMoe = function()
{ 
	var extraPos = 0;
	if( $(window).height() < $(this).height() ) extraPos = 150;
	var tPos = ( $(window).height() - $(this).height() )/2 + $(window).scrollTop() + extraPos;
	var lPos = ( $(window).width() - $(this).width() )/2;
	if (lastOpen != null) $(lastOpen).fadeOut(fadeTime);
	$(this)
		.fadeIn(fadeTime)
		//.css({ top: tPos + 'px', left: lPos + 'px' });
		.css({ top: '350px', left: lPos + 'px' });
		
	afficherMasque();
	
	lastOpen = this;

	return false;
}

// fermeture popup

$.fn.hidePop = function()
{
	$(this).each(
		function()
		{
			$(this).click(
				function()
				{
					$('#masque').fadeOut(fadeTime);
					$(lastOpen).fadeOut(fadeTime);
					
					return false;
				}
			);
		}
	);
}


$(function() {

	// ouverture popup
	$('.envoi-mail').showPop('#envoi-mail');
	$('.pop-condition-cgu').showPop('#cgu');
	//$('#rs .valider').showPop('#list-rs');

	// fermeture popup
	$('.btn-pop a').hidePop();
	$('#list-rs .rs-link a').hidePop();
		
});
