var CLightBox = Class.create({

	getPageScroll: function () {	
		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: 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){ // 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;
	},
	
	pause: function (numberMillis) {
		var now = new Date();
		var exitTime = now.getTime() + numberMillis;
		while (true) {
			now = new Date();
			if (now.getTime() > exitTime)
				return;
		}
	},

	show: function (content) {
	
		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();
		
		if (!$('overlay')) return;
		
		$('overlay').setStyle({height: arrayPageSize[1] + 'px'});	
		$('overlay').show();
		
		$('lightboxContent').update(content);	
		this.pause(250);			
	
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - $('lightbox').getHeight()) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - $('lightbox').getWidth()) / 2);
			
		$('lightbox').setStyle({top: (lightboxTop < 0) ? '0px' : lightboxTop + 'px',
								left: (lightboxLeft < 0) ? '0px' : lightboxLeft + 'px'});
	
		new Hover('#lightbox div.button', 'button-hover');
		$('lightbox').show();
	
		arrayPageSize = this.getPageSize();
		$('overlay').setStyle({height: arrayPageSize[1] + 'px'});
	},

	hide: function () {
		$('overlay').hide();
		$('lightbox').hide();
	},

	init: function () {	
		var objOverlay = new Element('div', { id:'overlay' });
		objOverlay.setStyle({ display:'none', position:'absolute',
				     top:'0',left:'0',zIndex:'90',width:'100%' });
		
		var objLightbox = new Element('div', { id:'lightbox' }).setStyle({ display:'none', position:'absolute', zIndex:'100' });

		objLightbox.appendChild(new Element('a', {href:'javascript:;', id: 'closeButton', title: 'Click to close'}).setStyle({position:'absolute',zIndex:200}).observe('click', function () { $('overlay').hide(); $('lightbox').hide(); return false;}).update('close'));	
		objLightbox.appendChild(new Element('div', {id: 'lightboxCaption'}));
		objLightbox.appendChild(new Element('div', {id: 'lightboxContent'}));
			
		var objBody = document.getElementsByTagName("body").item(0);
		objBody.insertBefore(objOverlay, objBody.firstChild);		
		objBody.insertBefore(objLightbox, objOverlay.nextSibling);					
	}
});

var LightBox = new CLightBox();
document.observe('dom:loaded', function(){
	LightBox.init();
});