if(typeof Effect == 'undefined')
  throw("lightVentana requires including script.aculo.us' effects.js library!");

var lightVentana = Class.create();	
lightVentana.prototype = {
		dimensions : {
			viewport : {
				height : null,
				width : null,
				offsetTop : null,
				offsetLeft : null
			}
		},
		pagePosition : {
			x : 0,
			y : 0
		},
		pageDimensions : {
			width : null,
			height : null
		},
		posicionesFinales : {
			top : null,
			left : null
		},
		//
		//	Initialize the lightwindow.
		//
		initialize : function(options) {
			this.options = Object.extend({
			}, options || {});
			this._getPageDimensions();
			this._browserDimensions();
		},
		// 
		//  Get the actual page size
		//
		_getPageDimensions : function() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = document.body.scrollWidth;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ 
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { 
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}

			var windowWidth, windowHeight;
			if (self.innerHeight) {	
				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) { 
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	

			if(yScroll < windowHeight){
				this.pageDimensions.height = windowHeight;
			} else { 
				this.pageDimensions.height = yScroll;
			}

			if(xScroll < windowWidth){	
				this.pageDimensions.width = windowWidth;
			} else {
				this.pageDimensions.width = xScroll;
			}
		},
		//
		//  Get the Browser Viewport Dimensions
		//
		_browserDimensions : function() {
			if (Prototype.Browser.IE) {
	            this.dimensions.viewport.height = document.documentElement.clientHeight;
	            this.dimensions.viewport.width = document.documentElement.clientWidth;   
	        } else {
	            this.dimensions.viewport.height = window.innerHeight;
	            this.dimensions.viewport.width = document.width || document.body.offsetWidth;
	        }
		},
		calculaPos: function (imgWidth, imgHeight) {
			this._getPageDimensions();
			this._browserDimensions();
			//this._setupDimensions();
			originalHeight = imgHeight + 75; //se añaden márgenes, bordes y texto en la ventana
			originalWidth = imgWidth + 39; //se añaden márgenes y bordes
			
			var offsetHeight = this.dimensions.viewport.height/2+this.pagePosition.y;
			var offsetWidth = this.dimensions.viewport.width/2+this.pagePosition.x;
			
			this.posicionesFinales.top = offsetHeight - originalHeight/2+'px';
			this.posicionesFinales.left = offsetWidth - originalWidth/2 +'px'
			/*elemento.setStyle({
				position: 'absolute',
				top: offsetHeight - originalHeight/2+'px',
				left: offsetWidth - originalWidth/2 +'px'
			});*/
			
			//new Effect.Appear(elemento, { duration: 1, from: 0.0, to: 0.8 });
		}
}

	Event.observe(window, 'load', function() {
        var imgPreloader = new Image();
        imgPreloader.onload = (function(){
        	var myLightWindow = new lightVentana();
    		myLightWindow.calculaPos(imgPreloader.width, imgPreloader.height);
    		$('pop').setStyle({
    			height:myLightWindow.dimensions.viewport.height + 'px',
    			width:myLightWindow.dimensions.viewport.width + 'px'
    		});
    		$('inner').setStyle({
    			left:myLightWindow.posicionesFinales.left,
    			top:myLightWindow.posicionesFinales.top
    		});
    		new Effect.Appear('ocultar', { duration: 1, from: 0.0, to: 1 });
        });
        imgPreloader.src = $('imagenbanner').getAttribute('src');
	});

	function escondeAnuncio() {
		new Effect.Fade('ocultar', { duration: 1, from: 1, to: 0.0 });
	}
