(function($){
	$.fn.cerrarCover = function(){
		/*$('.cover').animate(
			{'opacity': 0 },
			{duration : 800, queue:false, complete:function(){
				//alert('a');
				 $(".cover").css({'display':'none'});
				 $("body").css({'overflow':'auto'});
			}}
		);*/
		$("#cover").remove();
		//$("body").css({'overflow':'auto'});
	},
	$.fn.modalKillLoader = function() {
		$('.loading-modal').remove(); 
		//$('.cover').css({'display':'none'}); 
	},

	$.fn.modal =  function(opciones) {

			var opciones = $.extend({
				accion: '', // evento para el bind, de estar vacio, se evita el bind y se llama directamente.  
				titulo: 'Ventana',
				tipo: 'modal',
				cover: false,
				timer: 0,
				claseCss: '', // Se ignora con tipo:'ventana'
				contenido: '.',
				ajax:false,
				mWidth: 500,
				mHeight: 500,
				mOverflow: 'auto',
				zIndex: 997,
				url: '', // Se utiliza solo con ajax:true
				siCallback: function (){  }, // Se utiliza solo con tipo:'confirmar'
				botonSiHTML: '<img src="/js/modal/accept.gif" class="manito" border="0"><span>Aceptar&nbsp;</span>',
				noCallback: function (){  }, // Se utiliza solo con tipo:'confirmar'
				botonNoHTML: '<img src="/js/modal/cancel.gif" class="manito" border="0"><span>Cancelar&nbsp;</span>',
				cerrarCallback: function(){} 
			}, opciones);
			
			if(opciones.accion!=''){

				//alert('a');
				$(this).bind(opciones.accion, function(){new $.unModal(this, opciones);})
			}else{
				return new $.unModal(this, opciones);
			}
			
	},
		
	$.fn.confirmar =  function(opciones) {
			var opciones = $.extend({				
				accion: 'click',
				titulo:'Confirme la operaci&oacute;n', 
				cover:true, 
				contenido: 'Error de par&aacute;metros', 
				tipo: 'confirmar',
				claseCss: 'infomsg',
				zIndex: 997,
				mWidth: 500,
				timer: 0,
				botonSiHTML: '<img src="/js/modal/accept.gif" class="manito" border="0"><span>Aceptar&nbsp;</span>',
				botonNoHTML: '<img src="/js/modal/cancel.gif" class="manito" border="0"><span>Cancelar&nbsp;</span>',		
				noCallback: function(){ }
				
			}, opciones);
			
			return new $.unModal(this, opciones);
	},
		
	$.fn.galeria =  function(opciones) {
			var opciones = $.extend({				
				accion: 'click',
				triggerClass: 'galeria', 
				titulo:'REVOLUCION!', 
				cover:true, 
				contenido: 'Error de par&aacute;metros', 
				tipo: 'galeria',
				claseCss: 'infomsg',
				timer: 0,						
				botonSiHTML: '<img src="js/modal/next.gif" border="0"><span>Pr&oacute;xima&nbsp;</span>',
				botonNoHTML: '<img src="js/modal/prev.gif" border="0"><span>Anterior&nbsp;</span>',		
				siCallback: function(){ alert("proxima"); },
				noCallback: function(){ alert("anterior"); }
				
			}, opciones);
			if(opciones.accion!=''){

				$(this).bind(opciones.accion, function(){new $.unModal(this, opciones);})
			}else{
				
				return new $.unModal(this, opciones);
			}
			
	},
		
	$.unModal = function(element, opciones){
				/** Fix scope problems */
				var self = this;
	
				/** A unique id so we can find our elements later */
				var id = new Date().getTime().toString().substr(8);
				
				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;
					}

					var 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){  // 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;
					}

					var arrayPageScroll = new Array(xScroll,yScroll);
					return arrayPageScroll;
				};
			
				var covered=false;
				
				var mWidth = opciones.mWidth;
				var mHeight = opciones.mHeight;
				var mOverflow = opciones.mOverflow;
				// calculate top and left offset for the lightbox
				var arrayPageScroll = getPageScroll();
				var arrayPageSize = getPageSize();
				// If window was resized, calculate the new overlay dimensions
				
				$(window).scroll(function() {
					// Get page scroll
					var arrPageSizes = getPageSize();
					var arrPageScroll = getPageScroll();
					// Calculate top and left offset for the jquery-lightbox div object and show it
					//alert(arrPageScroll[1]);
					$('#cover').css({
						top: '0px',
						height: parseInt( ( arrPageSizes[1] ) )+'px'
					});
					
					$(modal).css({
						top: parseInt( ( arrPageScroll[1] + 10 ) )+'px'
					});
					
				});
				$(window).resize(function() {
					// Get page sizes
					var arrPageSizes = getPageSize();
					// Style overlay and show it
					$('#cover').css({
						top: '0px',
						width:		arrPageSizes[2],
						height:		arrPageSizes[3]
					});
					
					// Calculate top and left offset for the jquery-lightbox div object and show it
					var mWidth = $(modal).width();
					$(modal).css({
						left: parseInt( ( arrPageSizes[0]-mWidth)/2 )+'px'
					});
				});
				
				//var timer = 3600; // milisegundos, 0 = sin tiempo.
				if(opciones.cover && !covered){
				    
				    
				    
		arrPageSizes = getPageSize();
		arrPageScroll = getPageScroll();
				    
// 					$("body").css({'overflow':'hidden'});
					
 					$('body').prepend('<div id="cover" style="position:absolute;"></div>');
// 					$('#bodybg').prepend('<div style=" z-index:1000"><div id="cover"></div></div>');
//					$('#bodybg').prepend('<div id="cover"></div>');
					covered=true;
					$("#cover").css({  'height':arrayPageSize[1]+'px', 'width':arrayPageSize[2]+20+'px', 'display':'block'}).animate({opacity:'0.8', duration:'400'}).bind('click', 
						function(){ 
							if(opciones.timer<1){ 
								covered=false; 
								//$(".cover").animate({opacity:'1', duration:'10'});
								$.fn.cerrarCover();
								$('.modal').remove();
							} 
						}
					); 
					//$("body").css({'overflow':'hidden'});
				}
				//alert( parseInt( ( arrayPageSize[0]-imgWidth)/2 ) );
				//$("#modal>span").css({'width': imgWifalsedth+'px'});
				var modalID = id+'-modal';
				//alert(arrayPageScroll[1]);
				var topOffset = 10;
				if(opciones.tipo=='loading'){topOffset=60;}
				
				
				var modal = $('<div></div>').addClass('modal').attr({ 'id' : modalID}).css({'top':parseInt( ( arrayPageScroll[1] + topOffset ) )+'px'}).hide();
				
				$('body').prepend(modal);
				$('#contGral').prepend(modal);

				
				if(opciones.tipo=='mensaje'){
					
					// MENSAJE
					$(modal).css({'width': mWidth+'px', 'z-index':opciones.zIndex, 'left':parseInt( ( arrayPageSize[0]-mWidth)/2 )+'px'}).addClass(opciones.claseCss);
					$(modal).html('<span>'+opciones.contenido+'</span>');
					
				}else if(opciones.tipo=='loading'){
									
					// LOADING

					$(modal).css({'width': '200px', 'z-index':'1200', 'textAlign':'center', 'left':parseInt( ( arrayPageSize[0]-200)/2 )+'px'}).addClass('modalLuka').addClass('loading-modal');
					$(modal).html('<div>'+opciones.contenido+'</div><img src="/js/modal/loading_icon_medium.gif" border="0" alt="Cargando">');
					
				}else if(opciones.tipo=='galeria'){

					// GALERIA
					
					var imagenID = $(''+opciones.contenido).attr('id');
					var img = new Image();
					img.onload = function(){ 
						$(modal).append('<div id="div-'+imagenID+'"></div>');
						$('#div-'+imagenID).append(img);
						mWidth = img.width;
						mHeight = img.height;
						maxHeight = arrayPageSize[3]-80;
						maxWidth = arrayPageSize[2]-40;
						//alert(maxHeight);
						//alert(mHeight);
						var scrollY = 'none';
						var scrollX = 'none';
						if(maxWidth<mWidth){
							mWidth = maxWidth;
							scrollX = 'auto';
						}
						if(maxHeight<mHeight){
							mHeight = maxHeight;
							scrollY = 'auto';
						}
						
						$(modal).css({'width': mWidth+'px', 'z-index':opciones.zIndex, 'left':parseInt( ( arrayPageSize[0]-mWidth)/2 )+'px'}).addClass('modalLuka');
// 						$(modal).css({'top': '100px'});// PARCHE POR BANNER EN FLASH QUE OCULTA PARTE DE LAS FOTOS
						$('#div-'+imagenID).css({'height': mHeight+30+'px', 'overflow-x':scrollX, 'overflow-y':scrollY});
						$(modal).show();						
					};
					img.src = $(''+opciones.contenido).attr('src');
					
					//$(''+opciones.contenido).remove();

					
					
				}else if(opciones.tipo=='ventana'){

					// VENTANA

					$(modal).css({'width': mWidth+'px', 'height': mHeight+'px', 'z-index':opciones.zIndex, 'overflow-y': mOverflow, 'left':parseInt( ( arrayPageSize[0]-mWidth)/2 )+'px'}).addClass('modalLuka');
					$(modal).html(opciones.contenido);
				}else if(opciones.tipo=='topslide'){

					// slide superior
					
					$(modal).css({'width':'100%','height': '0px', 'overflow-y': mOverflow, 'top':0}).removeClass('modal').addClass('carro');
					$(modal).animate({height:'150px'}, {
						duration:700, 
						queue:false
					}
					);
					$(modal).html(opciones.contenido);
					
				}else if(opciones.tipo=='confirmar'){
					var botonSi = $('<div class="botonSi">'+opciones.botonSiHTML+'</div>').bind('click', opciones.siCallback);
					var botonNo = $('<div class="botonNo">'+opciones.botonNoHTML+'</div>').bind('click', opciones.noCallback);
					$(botonSi).bind('click', function(){ $('#'+modalID).remove(); if(opciones.cover){$.fn.cerrarCover();} });
					$(botonNo).bind('click', function(){ $('#'+modalID).remove(); if(opciones.cover){$.fn.cerrarCover();} });
					$(modal).css({'width': mWidth+'px', 'left':parseInt( ( arrayPageSize[0]-mWidth)/2 )+'px', 'z-index':opciones.zIndex}).addClass('modalLuka');
					$(modal).html('<h1>'+opciones.titulo+'</h1><span>'+opciones.contenido+'</span>');
					
					$(modal).append('<div class="modal-confirmar"></div>').append(botonSi, botonNo);
				}else if(opciones.tipo=='prompt'){
					
					// PROMPT

				}else if(opciones.tipo=='ajax'){
					
					// AJAX?

				}
				$(modal).css({'display':'block'});	
					//alert(modalID);	
				if(opciones.timer>0){
					$(modal).prepend('<div class="countdownDiv" style="width:'+mWidth+'px; height:5px; background-color:#FFF;"></div>');
					//$('#'+modalID).remove(); $('.cover').css({'display':'none'}); 
//					alert("borrar"+$('#'+modalID).html());
					$('#'+modalID+'>.countdownDiv').animate({width:'1px'}, {
						duration:opciones.timer, 
						queue:true, 
						complete:function(){ 
							$('#'+modalID).remove();
							if(opciones.cover){$.fn.cerrarCover();}
						} 
					});
			
				}else if(opciones.tipo!='confirmar' && opciones.tipo!='loading'){
					$(modal).prepend('<img class="botonCerrar manito" src="/js/modal/cancel.gif" style="float:right; margin:5px;" border="0">');
					$('#'+modalID+'>.botonCerrar').bind("click",opciones.cerrarCallback);					
					$('#'+modalID+'>.botonCerrar').bind("click",function (){ 
						//alert(modalID);
						$(modal).remove(); 
						if(opciones.cover){$.fn.cerrarCover();} 
					});
				}
				
		}
	
})(jQuery);
	
$(document).ready(function(){
	//$('body').prepend('<div style="position:absolute;"><div id="cover"></div></div>');
});
	/*
	
	$('#cabecera').modal({
		accion: 'click',
		titulo:'Mensaje', 
		cover:true, 
		contenido: 'Mensaje independiente', 
		tipo: 'mensaje',
		claseCss: 'infomsg',
		timer: 0
	});
	
	
*/
