
(function($) {
	$.fn.oLightBox = function (settings) {
		settings = jQuery.extend({
			poptop	: 100,
			template: 'template_1',
			callClose : null								 	
		},settings);
		
    	var $popupContent = $(this);
		var tSize = _getPageSize(); 
		var contenTop = ( tSize[3] - $popupContent.height() )/2 + $(window).scrollTop();
		
		var template_1 = '<div id="lightbox-overlay"></div>'
						+ 	'<div id="lightbox-outter">'
						+ 		'<div id="lightbox-inner">'
						+			'<div class="lightbox-close" title="Fermer"></div>'						
						+		'</div>'
						+	'</div>';
						//+ '</div>' ;
		
		/*var template_2 = '<div id="lightbox-overlay"></div>'
						+ 	'<div id="lightbox-outter">'
						+ 		'<div id="lightbox-inner">'
						+			'<div class="lightbox-close orange"></div>'						
						+		'</div>'
						+	'</div>'
						+ '</div>' ;

		var template_3 = '<div id="lightbox-overlay"></div>'
						+ 	'<div id="lightbox-outter">'
						+ 		'<div id="lightbox-inner">'
						+			'<div class="lightbox-close orange popcgu png"></div>'						
						+		'</div>'
						+	'</div>'
						+ '</div>' ;*/
		
		var template = eval(settings.template);		   
    
		function _showOverlay () {
			var bodyWitdh = $('body').width();
			var bodyHeight = $(window).height() < $('body').height() ? $('body').height() : $(window).height() ;			
			var tSize = _getPageSize();
			$('#lightbox-overlay').css({width: tSize[0] + 'px', height: tSize[1] +'px', opacity: 0.6, filter:'Alpha(Opacity=60)'})
		};
		
		function _showLightBox () {
			// Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			//$('embed, object, select', '.page').css({ 'visibility' : 'hidden' });
			$('select', '.page').css({ 'visibility' : 'hidden' });
			$('body').append(template);
			
			_showOverlay ();
			
			var contentWidth = $popupContent.innerWidth();
			var contentHeight = $popupContent.innerHeight();
			
			$popupContent
				.appendTo('#lightbox-inner')
				//.css ({ position:'static', top:'auto', left:'auto' });
				.removeClass('popup');
			
			$('#lightbox-inner').css ({ width: contentWidth + 'px', height: contentHeight + 'px'  });
			$('#lightbox-outter').css({ top: contenTop + 'px' });
			
			$('#lightbox-inner	.lightbox-close').click ( function () {               
				_closeLightBox ();
        if (settings.callClose)
          settings.callClose.call (this);             			
			});
			
		}
		$popupContent[0]._closeLightBox = _closeLightBox;
				
		function _closeLightBox () {
			//$('#lightbox-inner').find('#' + $popupContent.attr('id'))			
			//.css ({ position:'absolute', left:'-1000px', top:'0px' })
			$popupContent
				.addClass('popup')
				.appendTo('body');
				
			$('#lightbox-outter').remove();
			//$('#lightbox-overlay').fadeOut ( function() { $('#lightbox-overlay').remove(); });
			$('#lightbox-overlay').remove();
			$('embed, object, select', '.page').css({ 'visibility' : 'visible' });
			if (settings.callClose)
          		settings.callClose.call (this);
		}

		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 { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth;
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){
				pageWidth = xScroll;
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		
		_showLightBox ();
	}
})(jQuery); // Call and execute the function immediately passing the jQuery object


