// JavaScript Document

/*
	Author : Nick Cyrus Lemus Duque
	16 FEB 2010
*/

(function ($) {
	
	var $lightboxUni,
	$overlay,
	$iframe,
	$window = $(window),
	$body   = $('body');
	
	defaults = {
		iframeheight: false,
		iframewidth: false,
		overlayClose : true,
		ajustarTop : 0,
		ajustarBotton : 0,
		initialWidth: "800",
		initialHeight: "600",
		opacity: 0.5,
		url : '',
		preloading: true,
		next: "next",
		callback : ''
	};
	
	
	
	$.fn.ligtboxpulpo = function (options, callback) {
			
			var property = $.extend({}, defaults, options);		
			//$(this).click(function(e){
				var href;
				
				(property.url) ? href=property.url  : href=this.href;
				
				$('*').css('z-index','0');
				
				if (href){
						
						$overlay  = '<div id="overlayUni"></div>';
						
						var height = (property.iframeheight) ? property.iframeheight : property.initialHeight;
						var width = (property.iframewidth) ? property.iframewidth : property.initialWidth;
						
						
						height  = setSize(height , 'y');
						width = setSize(width,'x');	
						
						
						$iframe  = '<div id="Icontent" style="height:'+height+'px;width:'+width+'px;"><div id="iclose"></div><iframe allowtransparency="yes" frameborder="0" height="100%" width="100%" scrolling="auto" id="iFrameUniform" name="iFrameUniform" src="'+href+'" style="display:none"></iframe><br style="clear:both;" /></div>';
												
						
						$('body').prepend($overlay);
						
						$('#overlayUni').after($iframe)
						
						$('body').css({overflow : 'hidden'})
						
						var Size = getSizeBody();
						var SizeWindow = getSizeWindow();
						
						// alert($window.scrollTop())
						
						$('#overlayUni').css({
											 	width    : SizeWindow[0],
												height   : SizeWindow[1]+$window.scrollTop(),
												position : 'absolute',
												top		 : property.ajustarTop+'px',
												left     : '0px',
												opacity  : property.opacity,
												'z-index': 1
											})
						
						
						$('#iFrameUniform').load(function(){
							$('#iFrameUniform').fadeIn('Fast');
							if (property.callback)
									property.callback();
						})
						
						
						posTop = ((SizeWindow[1]/2)-(height/2))+$window.scrollTop(),
						posLeft = ((SizeWindow[0]/2)-(width/2))+$window.scrollLeft();
		
						
						
						$('#Icontent').css({
												position : 'absolute',
												top 	 : posTop,
												left     : posLeft,
												'z-index': 100
											 })
						
						
						
						/***************/	
						//'window'
						$(window).resize(function(){
							
							var Size = getSizeBody();
							var SizeWindow = getSizeWindow();
							
							
							
							$('#overlayUni').css({
											 	width    : Size[0],
												height   : Size[1]+$window.scrollTop()+200,
												position : 'absolute',
												top		 : '0px',
												left     : '0px',
												opacity  : property.opacity
							})
							
							
							
						
							posTop = ((SizeWindow[1]/2)-(height/2))+$window.scrollTop(),
							posLeft = ((SizeWindow[0]/2)-(width/2))+$window.scrollLeft();
							$('#Icontent').css({
												top 	 : posTop,
												left     : posLeft
												
							 },500)
							
							
						})
						/*
							Close overlay
						*/
						if (property.overlayClose){
							$('#overlayUni').click($.fn.close);
						}
						
						$('#iclose').click($.fn.close);
						
						
				
				 // End href
				}

			//})
	
	// End Función
	}
	
	$.fn.close = function(){
					
					var URL = $('#iFrameUniform').attr('src');
					
					URL = (URL) ? URL : 0;
					
					$('#iFrameUniform').fadeOut(1000, function(){
													$('#overlayUni').fadeOut(500, function(){
															$('#overlayUni,#iFrameUniform,#Icontent').remove()
													})
													
													$('#Icontent, #overlayUni').remove();
					
						$('body').css({overflow : 'auto'})
					
					})
			 e.preventDefault();
	}
	
	$.fn.simpleclose = function(e){
					$('#iFrameUniform').fadeOut(1000, function(){
													$('#overlayUni').fadeOut(500, function(){
															$('#overlayUni,#iFrameUniform,#Icontent').remove();
													})
								$('#Icontent').remove();
								
								$('body').css({overflow : 'auto'})
					})
			// e.preventDefault();
	}
	$.fn.closeNocallback = function(e){
					$('#iFrameUniform').fadeOut(1000, function(){
													$('#overlayUni').fadeOut(500, function(){
															$('#overlayUni,#iFrameUniform,#Icontent').remove();
													})
							
								$('body').css({overflow : 'auto'})
							
								$('#Icontent').remove();
					})
			// e.preventDefault();
	}
	
	// Convert % values to pixels
	function setSize(size, dimension) {
		dimension = dimension === 'x' ? $window.width() : $window.height();//document.documentElement.clientWidth : document.documentElement.clientHeight;
		return (typeof size === 'string') ? Math.round((size.match(/%/) ? (dimension / 100) * parseInt(size, 10) : parseInt(size, 10))) : size;
	}

	getSizeWindow=function(){
		
		width = $(window).width();
		height = $(window).height();
		var Size = new Array(width,height);
		return Size;
	}
	
	getSizeBody=function(){
		
		width = $('body').width();
		height = $('body').height();
		var Size = new Array(width,height);
		return Size;
	}
	
	
}(jQuery));


