var overlayInitialized = false;

function showPopup() {
	if (document.getElementById) {
		var popup = document.getElementById('popup');
		var overlay = document.getElementById('overlay');
		var dims = getPageDimensions();
		
		overlay.style.height = dims.pageHeight + "px";

		overlay.style.display = "block";
		popup.style.display = "block";
		
		centerPopup();
		
		window.onresize = centerPopup;
	}
	return false;
}

function centerPopup() {
	var popup = document.getElementById('popup');
	
	var dims = getPageDimensions();
	var offsets = getScrollOffsets();
	popup.style.left = '' + parseInt((dims.windowWidth / 2) - (popup.offsetWidth / 2) + offsets.x) + 'px';
	popup.style.top = '' + parseInt((dims. windowHeight / 2) - (popup.offsetHeight / 2) + offsets.y) + 'px';
}

function hidePopup() {
	if (document.getElementById) {
		var popup = document.getElementById('popup');
		var overlay = document.getElementById('overlay');

		popup.style.display = "none";
		popup.innerHTML = "";
		overlay.style.display = "none";
	}
	return false;
}

function getScrollOffsets() {	// quirksmode.org
	var offsets = new Object();
	if (self.pageYOffset) // all except Explorer
	{
		offsets.x = self.pageXOffset;
		offsets.y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		offsets.x = document.documentElement.scrollLeft;
		offsets.y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		offsets.x = document.body.scrollLeft;
		offsets.y = document.body.scrollTop;
	}
	
	return offsets;
}

function getPageDimensions() {
	var dims = new Object();

	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;
	}


	dims.windowWidth = windowWidth;
	dims.windowHeight = windowHeight;
	dims.pageWidth = pageWidth;
	dims.pageHeight = pageHeight;

	return dims;
}


/*
	Modified script from Lightbox by Lokesh Dhakar
	
	http://huddletogether.com/projects/lightbox/
*/
function initOverlay()
{

	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hidePopup();}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','popup');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
	// create inner div
	//var objLink = document.createElement("a");
	//objLightboxInner.setAttribute('id','popupInner');
	//objLightboxInner.style.display = 'none';
	//objLightbox.appendChild(objLightboxInner);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {return hidePopup(); return false;}
	objLightbox.appendChild(objLink);

	overlayInitialized = true;
	
	window.onscroll = function () { centerPopup(); };
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
	   	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}

}

addLoadEvent(initOverlay);