/**
 * jQuery lapframe plugin
 * plugin pompé sur lightbox (http://leandrovieira.com/projects/jquery/lightbox/)
 * qui permet d'afficher une iframe à la place d'une image
 * 
 * fonctionnalités de bidule :
 * ça ouvre une frame sur un fond noir et ça permet de la fermer
 * pas de navigation
 *
 * utilise :
 * - jquery.jframe (http://garage.pimentech.net/scripts_doc_jquery_jframe/)
 * - jquery.form (http://malsup.com/jquery/form/)
 * - jquery.dimension (http://brandonaaron.net)
 */


(function($) {
	/**
	fermer de l'extérieur
	*/
	$.closeLapframe = function (){

		$('#jquery-lapframe').remove();
		$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
		// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
		$('embed, object, select').css({ 'visibility' : 'visible' });
		
	}
	
	/**
	 * $ is an alias to jQuery object
	 *
	 */
	$.lapframe = function(settings) {// Settings to configure the jQuery lapframe plugin how you like
		settings = jQuery.extend(
			{
				// Configuration related to overlay
				overlayBgColor: 		'#000',		// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
				overlayOpacity:			0.5,		// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
				// Configuration related to frames
				imageLoading:			'/images/lapframe-ico-loading.gif',		// (string) Path and the name of the loading icon
				imageBtnClose:			'/images/lapframe-btn-close.gif',		// (string) Path and the name of the close btn
				imageBlank:				'/images/lapframe-blank.gif',			// (string) Path and the name of a blank image (one pixel)
				// Configuration related to container frame box
				containerBorderSize:	10,			// (integer) If you adjust the padding in the CSS for the container, #lapframe-container-frame-box, you will need to update this value
				containerResizeSpeed:	400,		// (integer) Specify the resize duration of container image. These number are miliseconds. 400 is default.
				
				// url de la page à loader dans la frame
				url:					'about:blank',
				// dimensions de la frame
				width:					'',
				height:					''
			},
			settings
		);
		
		// Caching the jQuery object with all elements matched
		var jQueryMatchedObj = this; // This, in this context, refer to jQuery object
		
		
		/**
		 * Start the jQuery lapframe plugin
		 *
		 * @param object objClicked The object (link) whick the user have clicked
		 * @param object jQueryMatchedObj The jQuery object with all elements matched
		 */
		function _start(jQueryMatchedObj) {
			// Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'hidden' });
			// Call the function to create the markup structure; style some elements; assign events in some elements.
			_set_interface();
			// Call the function that prepares image exibition
			_set_frame_to_view();
		}
		
		
		/**
		 * Create the jQuery lapframe plugin interface
		 *
		 * The HTML markup will be like that:
			<div id="jquery-overlay"></div>
			<div id="jquery-lapframe">
				<div id="lapframe-container-frame-box">
					<div id="lapframe-container-frame">
						<div src="" id="lapframe-frame">
						</div>
						<div id="lapframe-loading">
							<a href="#" id="lapframe-loading-link">
								<img src="../images/lapframe-ico-loading.gif">
							</a>
						</div>
					</div>
				</div>
			</div>
		 *
		 */
		 

		function _set_interface() {
			var html 	 = '\n<div id="jquery-overlay">';
			html 		+= '\n</div>';
			html 		+= '\n<div id="jquery-lapframe">';
			html 		+= '\n	<div id="lapframe-container-frame-box">';
			html 		+= '\n		<div id="lapframe-container-frame">';
			html 		+= '\n			<div src="" id="lapframe-frame">';
			html 		+= '\n			</div>';
			html 		+= '\n			<div id="lapframe-loading">';
			html 		+= '\n				<a href="#" id="lapframe-loading-link">';
			html 		+= '\n					<img src="' + settings.imageLoading + '">';
			html 		+= '\n				</a>';
			html 		+= '\n			</div>';
			html 		+= '\n		</div>';
			html 		+= '\n	</div>';
			html		+= '\n</div>';
			// Apply the HTML markup into body tag
			$('body').append(html);	
			// Get page sizes
			var arrPageSizes = ___getPageSize();
			// Style overlay and show it
			$('#jquery-overlay').css({
				backgroundColor:	settings.overlayBgColor,
				opacity:			settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).hide().fadeIn();
			
			// Get page scroll
			var arrPageScroll = ___getPageScroll();
			// Calculate top and left offset for the jquery-lapframe div object and show it
			$('#jquery-lapframe').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
			// Assigning click events in elements to close overlay
			$('#jquery-overlay').click(function() {
				_finish();
			});
			// Assign the _finish function to lapframe-loading-link and lapframe-secNav-btnClose objects
			$('#lapframe-loading-link').click(function() {
				_finish();
				return false;
			});
			
			
			// If window was resized, calculate the new overlay dimensions
			$(window).resize(function() {
				// Get page sizes
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = ___getPageScroll();
				// Calculate top and left offset for the jquery-lapframe div object and show it
				$('#jquery-lapframe').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
		}
		
		/**
		 *	prépare l'affichage de la frame, on affiche le preloader en attendant la fin du chargement
		 *
		 */
		function _set_frame_to_view() { // show the loading
			// Show the loading
			$('#lapframe-loading').show();
			// on cache la frame
			var $frame = $('#lapframe-frame');
			//$frame.hide();
			
			// donne sa taille à la frame si elle est déterminée dans les params
			/***/
			if (settings.width!=''){
				$frame.width(settings.width);
			}
			if (settings.height!=''){
				$frame.height(settings.height);
			}
			/**/
			// et on lance le chargement
			$frame.loadJFrame (
				settings.url, 
				function(){
					// fin du chargement
					var $frame = $('#lapframe-frame');
					_resize_container_frame_box($frame.width(), $frame.height());
				}
			);
			
		};
		
		/**
		 * Perfomance an effect in the image container resizing it
		 *
		 * @param integer intImageWidth The image´s width that will be showed
		 * @param integer intImageHeight The image´s height that will be showed
		 */
		function _resize_container_frame_box(intImageWidth,intImageHeight) {
			// Get current width and height
			var intCurrentWidth = $('#lapframe-container-frame-box').width();
			var intCurrentHeight = $('#lapframe-container-frame-box').height();
			// Get the width and height of the selected image plus the padding
			var intWidth = (intImageWidth + (settings.containerBorderSize * 2)); // Plus the image´s width and the left and right padding value
			var intHeight = (intImageHeight + (settings.containerBorderSize * 2)); // Plus the image´s height and the left and right padding value
			// Diferences
			var intDiffW = intCurrentWidth - intWidth;
			var intDiffH = intCurrentHeight - intHeight;
			// Perfomance the effect
			$('#lapframe-container-frame-box').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_image(); });
			if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
				if ( $.browser.msie ) {
					___pause(250);
				} else {
					___pause(100);	
				}
			} 
		};
		
		/**
		 * affiche la frame
		 *
		 */
		function _show_image() {
			$('#lapframe-loading').hide();
			$('#lapframe-frame').fadeIn();
		};
		/**
		 * Remove jQuery lapframe plugin HTML markup
		 *
		 */
		function _finish() {
			$('#jquery-lapframe').remove();
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		/**
		 / THIRD FUNCTION
		 * getPageSize() uses jquery.dimension
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function ___getPageSize() {
			var $document = $(document);
			var $window = $(window);
			return [$document.width(), $document.height(), $window.width(), $window.height()];
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function ___getPageScroll() {
			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;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		 /**
		  * Stop the code execution from a escified time in milisecond
		  *
		  */
		 function ___pause(ms) {
			var date = new Date(); 
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 };
		 
		_start(jQueryMatchedObj);
		return false; // Avoid the browser following the link
		
		
	}
	
})(jQuery); // Call and execute the function immediately passing the jQuery object