/*
 *
 *	@titre: jquery.pop.
 *	@description: javascript layer.
 *	@auteur: neov - http://www.neov.net.
 *	@creation: 20090506.
 *	@modification: 20091005.
 *
*/

(
	function($)
	{
		var fadeTime = 300 ;
		var lastOpen = null ;

		// affichage masque.
		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})
				.fadeIn(fadeTime) ;
		} ;

		// affichage popup.
		$.pop = function(el, o)
		{
			var base = this ;
			base.$el = $(el) ;
			base.el = el ;
			
			base.$el.data('pop', base) ;
			
			// initialisation.
			base.init = function()
			{
				// options.
				base.o = $.extend(
					{
						overlay:		true,
						popUp:			null,
						beforeShow:		null,
						afterShow:		null
					}, o || {}
				) ;

				// return collection.
				if(base) {
					$(base.$el).each(
						function()
						{
							// evenement au clic de l'element.
							$(this).click(
								function()
								{

									// action avant affichage.
									if(base.o.beforeShow) base.o.beforeShow.call() ;

									// affichage.
									if(base.o.overlay) {
										$(o.popUp).showElem() ;
									} else {
									
									}

									// action apres affichage.
									if(base.o.afterShow) base.o.afterShow.call() ;
									
									return false ;
								}
							) ;
						}
					) ;
				}
			}
			
			base.init() ;
		} ;

		$.fn.pop = function(o)
		{
			return this.each(
				function()
				{
					(new $.pop(this, o)) ;
				}
			) ;
		} ;

		// 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' }) ;
			afficherMasque() ;
			
			lastOpen = this ;

			return false ;
		} ;

		// fermeture popup.
		$.fn.hidePop = function()
		{
			$(this).each(
				function()
				{
					$(this).click(
						function()
						{
							$('#masque').fadeOut(fadeTime) ;
							$(lastOpen).fadeOut(fadeTime) ;
							
							return false ;
						}
					) ;
				}
			) ;
		} ;
		
		$.popup = function (el, o)
		{
			var pbase = this ;
			pbase.$el = $(el) ;
			pbase.el = el ;
			
			pbase.$el.data('popup', pbase) ;
			
			pbase.init = function()
			{
				// options.
				pbase.o = $.extend(
					{
						width:		400 ,
						height:		400 ,
						top:		0 ,
						left:		0 ,
						resize:		'no' ,
						scrollbars:	'no' ,
						name:		'popup'
					}, o || {}
				) ;

				// return collection.
				if(pbase) {
					$(pbase.$el).each(
						function()
						{
							$(this).click(
								function()
								{
									var url = $(this).attr('href') ;
									
									window.open(url, pbase.o.name, 'width=' + pbase.o.width + ', height=' + pbase.o.height + ', top=' + pbase.o.top + ', left=' + pbase.o.left + ', resizable=' + pbase.o.resize + ', scrollbars=' + pbase.o.scrollbars );
									
									return false;
								}
							) ;
						}
					) ;
				}
			}
			
			pbase.init() ;
		}
		
		$.fn.popup = function(o)
		{
			return this.each(
				function()
				{
					(new $.popup(this, o)) ;
				}
			) ;
		} ;
	}
)(jQuery) ;
