//
// 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;
}


//
// showOverlay()
// Preloads images. Pleaces new image in overlay then centers and displays.
//
function showOverlay(showText , a)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objOverlay = document.getElementById('overlay');
	var objCaption = document.getElementById('overlayCaption');
	var objImage = document.getElementById('overlayImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLoadingText = document.getElementById('loadingText');
	var objOverlayDetails = document.getElementById('overlayDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	if (showText) {
		objLoadingText.style.top = (arrayPageScroll[1] + ((arrayPageSize[3]) / 2) - a + 'px');

		objLoadingText.innerHTML = showText;
		objLoadingText.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

}

//
// hideOverlay()
//

function hideOverlay()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLoadingText = document.getElementById('loadingText');

	// hide overlay and overlay
	objOverlay.style.display = 'none';
	objLoadingText.style.display = 'none';
	document.getElementById('menu').style.display = 'block';
}




//
// initOverlay()
// Function runs on window load, going through link tags looking for rel="overlay".
// These links receive onclick events that enable the overlay display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//

function initOverlay()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "overlay")){
			anchor.onclick = function () {showOverlay(this); return false;}
		}
	}

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hideOverlay(); return false;"><img id="loadingImage" /></a>
	//	</div>
	// <div id="overlay">
	//		<a href="#" onclick="hideOverlay(); return false;" title="Click anywhere to close image">
	//			<img id="closeButton" />		
	//			<img id="overlayImage" />
	//		</a>
	//		<div id="overlayDetails">
	//			<div id="overlayCaption"></div>
	//			<div id="keyboardMsg"></div>
	//		</div>
	// </div>
	
	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 () {hideOverlay(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objOverlay.align = 'center';
	objBody.insertBefore(objOverlay, objBody.firstChild);


	var objLoadingText = document.createElement("div");
	objLoadingText.setAttribute('id','loadingText');
	objLoadingText.onclick = function () {hideOverlay(); return false;}
	objLoadingText.style.display = 'none';
	objLoadingText.style.position = 'absolute';
	objLoadingText.style.zIndex = '200';
	objLoadingText.style.top = '0';
	objLoadingText.style.left = '0';
	objLoadingText.style.width = '100%';
	objLoadingText.align = 'center';
	objBody.insertBefore(objLoadingText, objBody.firstChild);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initOverlay);	// run initOverlay onLoad