var ajaxPopupOptions = {
	width:600,
	flvPlayer : '../_js/jquery.ajaxPopup.flvplayer.swf',
	flvPlayerWidth : 480,
	flvPlayerHeight : 360
};

(function($) {
/* ***************** */
// plugin definition
// NB : ne doit être appliqué que sur des liens
jQuery.fn.ajaxPopup = function() 
{
	ajaxPopup.init();
	
	$(this).bind('click',function(url){
	
		$('#ajaxPopup-content').html('<p>Chargement en cours</p>');
		ajaxPopup.center();
		
		var url = $(this).attr('href');
		if(url.indexOf('?')>=0)
			url += '&ajax=1'
		else
			url += '?ajax=1'
		
		if(url.indexOf('.jpg')>=0 || url.indexOf('.gif')>=0 || url.indexOf('.png')>=0)
			$('#ajaxPopup-content').html('<img align="center" src="'+url+'" style="cursor:pointer;" onload="ajaxPopup.callback();ajaxPopup.resize(\'img\')" onclick="ajaxPopup.close();"/>');
		else if(url.indexOf('.flv')>=0)
		{
			// source à modifier pour avoir le bon chemin vers le fichier FLV et les dimensions souhaitées.
			$('#ajaxPopup-content').html('<embed src="'+ajaxPopupOptions.flvPlayer+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file='+$(this).attr('href')+'&displayheight='+ajaxPopupOptions.flvPlayerHeight+'&autostart=true" width="'+ajaxPopupOptions.flvPlayerWidth+'" height="'+ajaxPopupOptions.flvPlayerHeight+'">');
			ajaxPopup.callback();
			ajaxPopup.resize(ajaxPopupOptions.flvPlayerWidth+4);
		}
		else
			$('#ajaxPopup-content').load(url,ajaxPopup.callback);
			
		if($(this).attr('dim')!=undefined && $(this).attr('dim')>0)
		{	
			ajaxPopup.resize($(this).attr('dim'));
		}

		return false
	});
}
/* ***************** */
})(jQuery);

var ajaxPopup = {
	'init' : function()
	{
		if($('#ajaxPopup').length==0)
		{
			// il est possible de modifier la structure html du popup du moment qu'on conserve les ID et Class des éléments d'injection
			var html = '';
			html += '<div id="ajaxPopup">';
				html += '<div id="ajaxPopup-bg">&nbsp;</div>'; // arrière plan opacité réduite à 50%
				html += '<div id="ajaxPopup-main">'; // zone de superposition du popup
					html += '<div id="ajaxPopup-box">'; // boite du popup
						html += '<div id="ajaxPopup-top"><a href="#" class="ajaxPopup-close">Fermer</a></div>'; // barre supérieur
						html += '<div id="ajaxPopup-content">&nbsp;</div>'; // point d'injection du contenu
						html += '<div id="ajaxPopup-bottom"><a href="#" class="ajaxPopup-close">Fermer</a></div>'; // barre inférieur
					html += '</div>';
				html += '</div>'
			html += '</div>';
			$('body').append(html);
			
			$('#ajaxPopup-bg').css({opacity:0.5});
			ajaxPopup.close();
			
			$('#ajaxPopup .ajaxPopup-close').bind('click',function(){
				ajaxPopup.close();
				return false;
			});
		}
	},
	'close' : function()
	{
		$('#ajaxPopup').hide();
		return false;
	},
	'callback' : function(reponse, statut, request)
	{
		ajaxPopup.center();
	},
	'resize' : function(width)
	{
		if(width!=undefined)
			if(width>0)
				$('#ajaxPopup-box').css({'width':width+'px'});
			else
				$('#ajaxPopup-box').css({'width':$('#ajaxPopup-content '+width).eq(0).width()+4});
		else
			$('#ajaxPopup-box').css({'width':ajaxPopupOptions.width});
	},
	'center' : function()
	{
		$('#ajaxPopup').show();
		$('#ajaxPopup-content').css('height','auto');
		$('#ajaxPopup').width($(window).width());
		$('#ajaxPopup, #ajaxPopup-bg').height($(document).height());
	
		var marge = $(window).height()-$('#ajaxPopup-content').height();
		if(marge<150)
		{
			marge = 150;
			$('#ajaxPopup-content').height($(window).height()-150);
		}
		$('#ajaxPopup-box').css('margin-top',Math.floor(marge/2)+$(document).scrollTop()-40);
	}
}