//======================================================================//
//== About maqbox.js
//==--------------------------------------------------------------------//
//== This file is part of rough project.
//== Copyright (c) 2009-2010 - Erwan LE LOSTEC
//== Licensed under the GPL version 2.0 license.
//== See LICENSE file or
//== http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
//==--------------------------------------------------------------------//
//== Contributor : Erwan LE LOSTEC
//== Contributor : Clement DEGANDT
//==--------------------------------------------------------------------//
//== @requires jQuery v1.2 or later
//== 
//== Usage: 
//== 
//==  	jQuery(document).ready(function() 
//==  	{
//==  		jQuery('a[rel*=maqbox]').maqbox()
//==  	})
//== 
//== 	<a href="#terms" rel="maqbox">Terms</a> 		//-- Loads the #terms div in the box
//== 	<a href="terms.html" rel="maqbox">Terms</a> 	//-- Loads the terms.html page in the box
//== 	<a href="terms.png" rel="maqbox">Terms</a> 		//-- Loads the terms.png image in the box
//== 
//== For a diaporama:
//==	
//==  	jQuery(document).ready(function() 
//==  	{
//== 		jQuery('a[rel=maqbox_diaporama]').maqbox();
//== 		$('a[rel=maqbox_diaporama] > img:first').show();
//== 		-- or -- $('a[rel=maqbox_diaporama]').each(function() { $(this).children('img:first').show(); }); //-- When more than 1 diaporama on the page
//==  	})
//==	
//==	<a id="diaporama_14263" rel="maqbox_diaporama">
//==		<img style="display:none;" title="Title 1" alt="image 1" src="my_image_1.jpg" />
//==		<img style="display:none;" title="Title 2" alt="image 2" src="my_image_2.jpg" />
//==		<img style="display:none;" title="Title 3" alt="image 3" src="my_image_3.jpg" />
//==	</a>
//== 
//== 
//== You can also use it programmatically:
//== 
//== 	jQuery.maqbox('some html');						//-- The above will open a maqbox with "some html" as the content.
//== 	jQuery.maqbox(function(jQuery) 
//==	{
//==		jQuery.get('blah.html', function(data) { jQuery.maqbox(data) })	//-- The above will show a loading screen before the passed function is called, allowing for a better ajaxy experience.
//==	})
//==
//==  The maqbox function can also display an ajax page or image:
//== 	
//== 	jQuery.maqbox({ ajax: 'remote.html' })
//== 	jQuery.maqbox({ image: 'dude.jpg' })
//== 	
//== Want to close the maqbox?  Trigger the 'close.maqbox' document event:
//== 	
//== 	jQuery(document).trigger('close.maqbox')
//== 	
//== maqbox also has a bunch of other hooks:
//== 	
//== 	loading.maqbox
//== 	beforeReveal.maqbox
//== 	reveal.maqbox (aliased as 'afterReveal.maqbox')
//== 	init.maqbox
//== 	
//== Simply bind a function to any of these hooks:
//== 	
//== 	 jQuery(document).bind('reveal.maqbox', function() { ...stuff to do after the maqbox and contents are revealed... })
//======================================================================///*
(function(jQuery) 
{
	jQuery.maqbox = function(data, klass) 
	{
		jQuery.maqbox.loading();
		
		if(data.ajax) 						{ fillmaqboxFromAjax(data.ajax);					}
		else if(data.image) 				{ fillmaqboxFromImage(data.image);					}
		else if(data.div) 					{ fillmaqboxFromHref(data.div);						}
		else if(data.diaporama) 			{ fillmaqboxFromDiaporama(data.diaporama);			}
		else if(jQuery.isFunction(data)) 	{ data.call(jQuery);								}
		else 								{ jQuery.maqbox.reveal(data, klass);				};
	}
	jQuery.extend(jQuery.maqbox, 
	{
		settings: 
		{
			confirmImage		: '',
			confirmText 		: '',
			confirmDirection 	: '',
			headerText 			: '&nbsp;',
			opacity      		: 0.5,
			overlay				: true,
			loadingImage 		: '/design/style_1/images/maqbox/loading.gif',
			closeImage   		: '/design/style_1/images/maqbox/closelabel.gif',
			closeText	   		: '',
			closeDirection 		: '',
			imageTypes   		: ['png', 'jpg', 'jpeg', 'gif'],
			maqboxHtml  		: '<div id="maqbox" class="popup" style="display:none;">\
								    <div class="body">\
								     <ul class="shadows">\
								      <li class="top-left">&nbsp;</li>\
								      <li class="top">&nbsp;</li>\
								      <li class="top-right">&nbsp;</li>\
								      <li class="left">&nbsp;</li>\
								      <li class="right">&nbsp;</li>\
								      <li class="bottom-left">&nbsp;</li>\
								      <li class="bottom">&nbsp;</li>\
								      <li class="bottom-right">&nbsp;</li>\
								     </ul>\
								     <div class="headerText">&nbsp;</div>\
								     <div class="content">&nbsp;</div>\
								     <div class="footer"><a href="#" class="close"></a><span class="spacer">&nbsp;</span></div>\
								    </div>\
								   </div>'
		},
		loading: function() 
		{
			init();
			if(jQuery('#maqbox .loading').length == 1) { return true };
			showOverlay();
			jQuery('#maqbox .content').empty();
			jQuery('#maqbox .body').children().hide().end();
			jQuery('#maqbox .body').append('<div class="loading"><img src="'+jQuery.maqbox.settings.loadingImage+'"/></div>');
			//jQuery('#maqbox').css( { top:getPageScroll()[1] + (getPageHeight() / 10), left:'50%' }).css('display','inline');
			jQuery('#maqbox').css( { top:(getPageHeight() / 10), left:'50%' }).css('display','inline');
			jQuery(document).bind('keydown.maqbox', function(e) { if(e.keyCode == 27) { jQuery.maqbox.close() }; return true; });
			jQuery(document).trigger('loading.maqbox');
		},
		reveal: function(data, klass) 
		{
			jQuery(document).trigger('beforeReveal.maqbox');
			if(klass) { jQuery('#maqbox .content').addClass(klass); };
			jQuery('#maqbox .content').append(data);
			jQuery('#maqbox .loading').remove();
			jQuery('#maqbox .body').children().fadeIn('normal');
			jQuery('#maqbox').css('left', (jQuery(window).width() / 2) - (jQuery('#maqbox').width() / 2));
			jQuery(document).trigger('reveal.maqbox').trigger('afterReveal.maqbox');
			/////////// rectificatif IE 7 et 6 ///////////////
			if (navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.substr(22,1) == "6" || navigator.appVersion.substr(22,1) == "7" ))
			{
				jQuery('#maqbox .body').css('width', jQuery('#maqbox .content').width() + 'px');
			}
		},		
		close: function() 
		{
			jQuery(document).trigger('close.maqbox');
			return false;
		},		
		confirm: function() 
		{
			jQuery(document).trigger('confirm.maqbox');
			return false;
		}
	})

	jQuery.fn.maqbox = function(settings) 
	{
		init(settings);
	
		function clickHandler() 
		{
	
			jQuery.maqbox.loading(true);
			//-- support for rel="maqbox.inline_popup" syntax, to add a class, also supports deprecated "maqbox[.inline_popup]" syntax
			//var klass 		= this.rel.match(/maqbox\[?\.(\w+)\]?/);
			var klass 			= this.rel.match(/maqbox\[?\_(\w+)\]?/);
			if(klass) 			{ klass = klass[1] };
			fillmaqboxFromHref(this.href, klass, this.id);
			return false;
		}
		return this.click(clickHandler);
	}

  
	function init(settings) 
	{/** @function init()
	  * called one time to setup maqbox on this page
	  *
	  * @param		settings	<object>	the maqpbox settings.
	  *
	  * @return	nothing
	  **/
		jQuery.maqbox.settings.inited 				= true;
		
		jQuery(document).trigger('init.maqbox');
		makeCompatible();
		
		var imageTypes 								= jQuery.maqbox.settings.imageTypes.join('|');
		jQuery.maqbox.settings.imageTypesRegexp		= new RegExp('\.' + imageTypes + 'jQuery', 'i');
		
		if(settings) { jQuery.extend(jQuery.maqbox.settings, settings); };
		jQuery('body').append(jQuery.maqbox.settings.maqboxHtml);
		
		var preload 								= [ new Image(), new Image() ];
		preload[0].src 								= jQuery.maqbox.settings.closeImage;
		preload[1].src 								= jQuery.maqbox.settings.loadingImage;
		
		jQuery('#maqbox').find('.b:first, .bl, .br, .tl, .tr').each(function() 
		{
			preload.push(new Image());
			preload.slice(-1).src 					= jQuery(this).css('background-image').replace(/url\((.+)\)/, 'jQuery1');

		});
		jQuery.maqbox
		jQuery('#maqbox .close').html(jQuery.maqbox.settings.closeText);
		jQuery('#maqbox .headerText').html(jQuery.maqbox.settings.headerText);
		jQuery('#maqbox').addClass(jQuery.maqbox.settings.closeDirection);
		jQuery('#maqbox .close').click(jQuery.maqbox.close);
		jQuery('#maqbox .close_image').attr('src', jQuery.maqbox.settings.closeImage);
		
		if(jQuery.maqbox.settings.confirmText.length > 0) 
		{
			jQuery('#maqbox .footer .confirm').remove();
			jQuery('#maqbox .footer').append('<a class="confirm"></a>');
			jQuery('#maqbox .confirm').html(jQuery.maqbox.settings.confirmText);
			jQuery('#maqbox').addClass(jQuery.maqbox.settings.confirmDirection);
			if(jQuery.maqbox.settings.confirmImage !== '') 
			{
				jQuery('#maqbox .confirm').css('background-image', 'url('+jQuery.maqbox.settings.confirmImage+')');
			}
		}
	}
	function getPageScroll() 
	{/** @function getPageScroll()
	  * get the page scroll position
	  *
	  * @return	nothing
	  **/
		var xScroll, yScroll;
		
		if (self.pageYOffset) 
		{
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} 
		else if(document.documentElement && document.documentElement.scrollTop) 
		{//-- Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} 
		else if(document.body) 
		{//-- all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}
		return new Array(xScroll,yScroll);
	}
	function getPageHeight() 
	{/** @function getPageHeight()
	  * get the page height adapted from getPageSize()
	  *
	  * @return	nothing
	  **/
		var windowHeight;
		
		if (self.innerHeight)
		{//-- all except Explorer
			windowHeight = self.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientHeight)
		{//-- Explorer 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		}
		else if(document.body)
		{//-- other Explorers
			windowHeight = document.body.clientHeight;
		}
		
		return windowHeight;
	}
	function makeCompatible() 
	{/** @function makeCompatible()
	  * Backwards compatibility
	  *
	  * @return	nothing
	  **/
		var jQuerys 				= jQuery.maqbox.settings

		jQuerys.loadingImage 		= jQuerys.loading_image 	|| jQuerys.loadingImage;
		jQuerys.closeImage 			= jQuerys.close_image 		|| jQuerys.closeImage;
		jQuerys.closeText 			= jQuerys.close_text 		|| jQuerys.closeText;
		jQuerys.headerText 			= jQuerys.header_text 		|| jQuerys.headerText;
		jQuerys.closeDirection 		= jQuerys.close_direction 	|| jQuerys.closeDirection;
		jQuerys.confirmImage 		= jQuerys.confirm_image 	|| jQuerys.confirmImage;
		jQuerys.confirmText 		= jQuerys.confirm_text 		|| jQuerys.confirmText;
		jQuerys.confirmDirection 	= jQuerys.confirm_direction || jQuerys.confirmDirection;
		jQuerys.imageTypes 			= jQuerys.image_types 		|| jQuerys.imageTypes;
		jQuerys.maqboxHtml 			= jQuerys.maqbox_html 		|| jQuerys.maqboxHtml;
	}
	function fillmaqboxFromHref(href, klass, id) 
	{/** @function fillmaqboxFromHref()
	  * Figures out what you want to display and displays it, formats are: 'div: #id', 'image: blah.extension', 'ajax: anything else'
	  *
	  * @param		href	<object>	the maqpbox settings.
	  * @param		klass	<object>	the maqpbox settings.
	  * @param		id		<object>	the maqpbox settings.
	  *
	  * @return	nothing
	  **/    
		if (href.match(/#/)) 
		{//-- div
			var url    = window.location.href.split('#')[0];
			var target = href.replace(url, '');
			jQuery.maqbox.reveal(jQuery(target).clone().show(), klass);
		}
		else if(klass.match(/diaporama/))
		{//-- image
			fillmaqboxFromDiaporama(href, klass, id);
		}
		else if(href.match(jQuery.maqbox.settings.imageTypesRegexp))
		{//-- image
			fillmaqboxFromImage(href, klass);
		}
		else
		{//-- ajax
			fillmaqboxFromAjax(href, klass);
		}
	}	
	function fillmaqboxFromImage(href, klass) 
	{
		var image 		= new Image();
		image.onload = function() 
		{
			jQuery.maqbox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass);
		}
		image.src 		= href;
	}	
	function fillmaqboxFromDiaporama(href, klass, id) 
	{
		var nb_elem				= jQuery("#"+ id +" img").length;		
		var first_image			= jQuery("#"+ id +" > img:first");
		first_image.addClass('diapo_active');
		var contents			= jQuery("#"+ id +"").html();
		var title				= "";
		if(first_image.attr("title") != "" && first_image.attr("title") != "undefined") 			
		{ 
			title 				= first_image.attr("title").replace(/\n/gi, "<br />"); 
		}
		var text				= "<div class='diaporama_txt'>"+ title +"</div>";
		var num_bar				= "";
		var prev				= "";
		var next				= "";
		if(nb_elem > 1)
		{
			num_bar				= "<ul class='diaporama_numbar'>";
			var plus			= "";
			for(i = 1 ; i <= nb_elem ; i++)
			{
				if(i == 1) 		{ plus = " active_bar"; } else { plus = "" }
				num_bar			+= "<li class='numbar"+ plus +"' id='numbar_"+ i +"'onclick='diaporamaNavigation(\""+ i +"\" , \""+ nb_elem +"\")'>"+ i +"</li>";
			}
			num_bar				+= "</ul>";
			prev				= "<div class='diaporama_prev' onclick='diaporamaNavigation(\"prev\" , \""+ nb_elem +"\")'>&nbsp;</div>";
			next				= "<div class='diaporama_next' onclick='diaporamaNavigation(\"next\" , \""+ nb_elem +"\")'>&nbsp;</div>";
		}
		jQuery('#maqbox .headerText').html(num_bar);
		jQuery.maqbox.reveal(prev + '<div class="diaporama">'+ contents +'</div>' + next + text , klass);
		jQuery('#maqbox .diaporama').each(function() { $(this).children('img').hide(); $(this).children('img.diapo_active').show(); });
		/////////// retouches pour la barre des numeros //////////
		jQuery('#maqbox .diaporama_numbar').css('width' , (jQuery('#maqbox .diaporama').width() - 80) + 'px');
		/////////// rectificatif IE 8 ///////////////
		if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.substr(22,1) == "8")
	   	{
		   jQuery('#maqbox .diaporama_prev, #maqbox .diaporama_next').css('visibility' , 'hidden');
	   	}
	}
	function fillmaqboxFromAjax(href, klass) 
	{
		jQuery.get(href, function(data) 			{ jQuery.maqbox.reveal(data, klass) });
	}
	function skipOverlay() 
	{
		return jQuery.maqbox.settings.overlay == false || jQuery.maqbox.settings.opacity === null;
	}
	function showOverlay() 
	{
		if(skipOverlay()) { return; };
		
		if(jQuery('maqbox_overlay').length == 0) 	{ jQuery("body").append('<div id="maqbox_overlay" class="maqbox_hide"></div>') };
		
		jQuery('#maqbox_overlay').hide().addClass("maqbox_overlayBG")
							.css('opacity', jQuery.maqbox.settings.opacity)
							.click(function() { jQuery(document).trigger('close.maqbox') })
							.fadeIn(900);
		
		return false;
	}
	function hideOverlay() 
	{
		if(skipOverlay()) { return; };
		
		jQuery('#maqbox_overlay').fadeOut(900, function() { jQuery("#maqbox_overlay").removeClass("maqbox_overlayBG"); jQuery("#maqbox_overlay").addClass("maqbox_hide"); jQuery("#maqbox_overlay").remove(); })
		
		return false;
	}

	jQuery(document).bind('close.maqbox', function() 
	{
		jQuery(document).unbind('keydown.maqbox');
		jQuery('#confirm').remove();
		
		jQuery('#maqbox').fadeOut(function() 
		{
			jQuery('#maqbox .content').removeClass().addClass('content');
			hideOverlay();
			jQuery('#maqbox .loading').remove();
			jQuery(document).unbind('init.maqbox');
		});
		
		jQuery("#maqbox").remove();
		jQuery("#maqbox_overlay").remove();
	});	
	jQuery(document).bind('confirm.maqbox', function() 
	{
		var direction = jQuery('#confirm').attr('class');
		jQuery('#confirm').remove();
		jQuery(document).unbind('keydown.maqbox');
		
		jQuery('#maqbox').fadeOut(function() 
		{
			jQuery('#maqbox .content').removeClass().addClass('content');
			hideOverlay();
			jQuery('#maqbox .loading').remove();
		});
		if(direction.length > 0) 
		{
			window.location = direction;
		}
	});
})(jQuery);
/*//////////// ?????  pas moyen de la mettre ailleurs  ????? //////////*/
function diaporamaNavigation(action , nb_elem)
{
	var num_actif				= jQuery(".diapo_active").index();
	if(action == "prev") 		{ var new_num 	= num_actif - 1; 	}
	else if(action == "next") 	{ var new_num 	= num_actif + 1; 	}
	if(new_num < 0) 			{ new_num		= nb_elem - 1;		}
	if(new_num > nb_elem - 1) 	{ new_num		= 0;				}
	if(action != "prev" && action != "next")						
								{ var new_num 	= action - 1;			}
	var new_image				= jQuery("#maqbox .diaporama img").eq(new_num);
	jQuery("#maqbox .diapo_active").removeClass("diapo_active").hide();
	jQuery("#maqbox .active_bar").removeClass("active_bar");
	jQuery("#maqbox .diaporama_numbar li").eq(new_num).addClass("active_bar");
	new_image.addClass('diapo_active').show();
	if(new_image.attr("title") != "" && new_image.attr("title") != "undefined") 			
	{ 
		jQuery(".diaporama_txt").html(new_image.attr("title")); 
	}
}
