/**
 * jQuery popup plugin
 * This jQuery plugin was inspired and based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 * and adapted to me for use like a plugin from jQuery.
 * @name jquery-popup.js
 * @author George Desmyter 
 * @version 0.1
 * @date Novembre 10, 2008
 * @category jQuery plugin
 * @copyright (c) 2008 George Desmyter 
 */

(function($){  
	$.jq_popup = function(url, options) {   
		var settings = {  
			overlayBgColor: 		'#000',
			overlayOpacity:			0.8,
			imageLoading:			'loading.gif',	
			containerBorderSize:	10,			
			containerResizeSpeed:	400,
			width: 300,
			height: 300,
			internalLink: 'internal_jq_popup', // classe
			closeButton : '#close'
		};  
		var options = $.extend(settings, options);
		
		var jQueryMatchedObj = this;
		
		function _initialize() {
			_start(this,jQueryMatchedObj); 
			return false; 
		}
		
		function _start(objClicked,jQueryMatchedObj) {	
			$('select').css({ 'visibility' : 'hidden' });
			_set_interface();
			_set_page_to_view();
		}
		
		function _set_interface() {
			$('body').append('<div id="jquery-overlay"></div><div id="jquery-popup-container"><div id="jquery-popup-container-load"><div id="popup-loading"><img src="' + settings.imageLoading + '"></div></div></div>');	
			var arrPageSizes = ___getPageSize();
			$('#jquery-overlay').css({
				backgroundColor:	settings.overlayBgColor,
				opacity:			settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
			var arrPageScroll = ___getPageScroll();
			$('#jquery-popup-container').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
			$('#jquery-popup-container-load').css({
				backgroundColor:	settings.overlayBgColor
			});
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				var arrPageScroll = ___getPageScroll();
				$('#jquery-popup-container').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
		}
		
		function _set_page_to_view() {
			$('#popup-loading').show();
			_resize_container_image_box();
		};
		
		function _resize_container_image_box() {
			var intCurrentWidth = $('#jquery-popup-container-load').width();
			var intCurrentHeight = $('#jquery-popup-container-load').height();
			intWidth = settings.width;
			intHeight = settings.height; 
			if(settings.width == 'auto' || settings.width == 'auto') {	
				$('body').append('<table style="position: absolute; top: -99999px; left: -99999px;" id="preload"><tr><td id="content_preload"></td></tr></table>');
				$("#content_preload").load(url, function(){	
					intWidth = settings.width == 'auto' ? $("#content_preload").width() : settings.width;
					intHeight = settings.height == 'auto' ? $("#content_preload").height() : settings.height;
					var intDiffW = intCurrentWidth - intWidth;
					var intDiffH = intCurrentHeight - intHeight;
					$('#jquery-popup-container-load').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_page(true); });
					if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
						if ( $.browser.msie ) {
							___pause(250);
						} else {
							___pause(100);	
						}
					} 	
				});			
			}
			else {
				var intDiffW = intCurrentWidth - intWidth;
				var intDiffH = intCurrentHeight - intHeight;
				$('#jquery-popup-container-load').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_page(); });
				if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
					if ( $.browser.msie ) {
						___pause(250);
					} else {
						___pause(100);	
					}
				} 				
			}
		};
		
		function _show_page(allreadyLoaded) {
			if(allreadyLoaded) {
				$('#jquery-popup-container-load').html($("#content_preload").html());
				$("#preload").hide().remove();				
				$('#popup-loading').hide();				
				if(settings.closeButton) {					
					$(settings.closeButton).click(function() {
						_finish();
						return false;
					});
				}
			}
			else {
				$('#jquery-popup-container-load').load(url,function() {
					$('#popup-loading').hide();				
					if(settings.closeButton) {
						$(settings.closeButton).click(function() {
							_finish();
							return false;
						});
					}
				});				
			}
			var testContent = false;
			$('#jquery-popup-container-load, #jquery-overlay, #jquery-popup-container').click(function() {
				if($(this).attr('id')=='jquery-popup-container-load') 
					testContent = true;
				if(!testContent)
					_finish();
				else if($(this).attr('id')!='jquery-popup-container-load')	
					testContent = false;			
			});
			$('a.'+settings.internalLink).click(function(){
				var url = $(this).attr('href');
				$('#jquery-popup-container-load').load(url, function(){
					_loadInternalLinks($(this));
				});
				return false;
			});
		};
		
		function _loadInternalLinks(e) {
			$(e).find('a.'+settings.internalLink).click(function(){
				var url = $(this).attr('href');
				$('#jquery-popup-container-load').load(url, function(){
					_loadInternalLinks($(this));
					if(settings.closeButton) {
						$(settings.closeButton).click(function() {
							_finish();
							return false;
						});
					}
				});
				return false;
			});
			if(settings.closeButton) {
				$(settings.closeButton).click(function() {
					_finish();
					return false;
				});
			}
		}
		
		function _finish() {
			$('#jquery-popup-container').remove();
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else {
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) {
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		
		function ___pause(ms) {
			var date = new Date(); 
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 };
		_initialize();
		return false
	};  
})(jQuery);  
