var Overlay = Class.create();

Overlay.prototype = {
	initialize: function(base, options) {
		
		this.options = {
			width: '',
			height: '',
			backgroundColor: ''
			/* you can set these in the instantiation or the CSS */
		}
		
		this.options = Object.extend(this.options, options || {});
	
		var overlayShowTargets = $(base).getElementsByClassName(this.options.overlayShowClass);
		var overlayCloseTargets = $(base).getElementsByClassName(this.options.overlayCloseClass);
		
		var overlays = $(base).getElementsByClassName(this.options.overlayClass);
		
		for(var x=0; x<overlayShowTargets.length; x++)
			Event.observe(overlayShowTargets[x], 'click', this.activate.bindAsEventListener(this), false);
			
		for(var x=0; x<overlayCloseTargets.length; x++)
			Event.observe(overlayCloseTargets[x], 'click', this.deactivate.bindAsEventListener(this), false);	
			
			for(var x=0; x<overlays.length; x++) {
				overlays[x].style.width = this.options.width || overlays[x].style.width;
				overlays[x].style.height = this.options.height || overlays[x].style.height;
				
				overlays[x].style.background =  this.options.backgroundColor || $(overlays[x]).getStyle('background-color');
			
				overlays[x].style.margin = '0 auto';
				overlays[x].style.display = 'none'; 
				overlays[x].style.zIndex = '-10';
		}
	
		Event.observe(document, 'keypress', this.setKeyPress.bindAsEventListener(this));
		Event.observe('overlay', 'click', this.deactivate.bind(this));
		
		$('overlay').style.display = 'none';
		
	},
	
	setKeyPress : function(e) {
	
		if (e.keyCode == Event.KEY_ESC) {
			this.hideLightbox();
		}
		
	},
	
	activate: function(e) {
	
		var idToOpen = Event.element(e).getAttribute('rel') ?
		Event.element(e).getAttribute('rel') :
		Event.element(e).parentNode.getAttribute('rel');
	
		
		// hack for innovation page //
		if (typeof this.options.beforeDisplay == 'function') {
			this.options.beforeDisplay.apply(this, arguments);
		}
		
		
		this.displayLightbox(idToOpen);
		if(e) Event.stop(e);
	
	},
	
	deactivate: function(e) {
		this.hideLightbox();
		if(e) Event.stop(e);
	},
	

	displayLightbox: function(id){
	
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		
		// show the gray overlay //
		$('overlay').style.height = arrayPageSize[1] + 'px';
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.5 });
		
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
		$('lightbox').style.display = 'block';
		$('lightbox').style.top = lightboxTop + 'px';
		
		var idToShow = $(id + '-overlay');
		idToShow.style.display = 'block';
		idToShow.style.zIndex = '99999'; /* bring the sucker to the very tippy top */
	},
	
	hideLightbox: function() {
		
		var overlays = document.getElementsByClassName(this.options.overlayClass);
		
		for (var x=0; x<overlays.length; x++) {
			overlays[x].style.display = 'none';
			overlays[x].style.zIndex = '-10';
		}
		
		new Effect.Fade('overlay', { duration: 0.2, from: 0.5, to: 0 });
		$('lightbox').style.display = 'none';
	
	}
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	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){ // 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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

