// JavaScript Document

(function($){
	$.fn.publicidadmodal = function(settings){
		settings = jQuery.extend({
			msegundos : 10000,
			fade : 1000,
			opacity : 0.75
		},settings);
		var jQueryMatchedObj = this; //hace referencia al objeto que por medio del selector
		
		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;
		};
		
		creaVentana = function(){
			var left = ($(document).width() - 500)/2;
			$("body").append('<div id="publicidad-overlay"></div><div id="publicidad-modal-ventana" class="hide"><div id="publicidad-modal-promos"></div><div id="btn-cerrar-ventana"><a href="#">X Cerrar</a></div><div id="contenedor-ventana">'+jQueryMatchedObj.html()+'</div></div>');
			
			//asigno eventos de cerrar al overlay y al viculo cerrar
			$('#publicidad-overlay,#btn-cerrar-ventana a').click(function() {
				cerrarVentana();	
				return false;							
			});
			
			var arrPageSizes = ___getPageSize();
			
			//$('embed, object, select').css({ 'visibility' : 'hidden' });
			$("#publicidad-modal-ventana").css({"left":left});
			$("#publicidad-overlay").css({"opacity":settings.opacity,"width":arrPageSizes[0],"height":arrPageSizes[1]}).fadeIn(settings.fade);
			$("#publicidad-modal-ventana").fadeIn(settings.fade);
			
			$(window).resize(function() {
				// Get page sizes
				var left = ($(document).width() - 500)/2;
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#publicidad-overlay').css({"width":arrPageSizes[0],"height":arrPageSizes[1]});
				$("#publicidad-modal-ventana").css({"left":left});
			});
		}
		
		cerrarVentana = function(){
			$("#publicidad-modal-ventana").fadeOut(settings.fade,function(){$('#publicidad-modal-ventana').remove();});
			$("#publicidad-overlay").fadeOut(settings.fade,function(){$("#publicidad-overlay").remove();});
			//$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		
		cerrarDespues = function(msegundos){
			setTimeout("cerrarVentana()",msegundos)
		}
		
		creaVentana();
		cerrarDespues(settings.msegundos);
	}
})(jQuery);
