window.onload = function() { 
	initLightbox();
};

var t = 0;
var tmr;
var closeButton = 'images/close.gif';	
var fadeObj;

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

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


function waitFor(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x') { hideLightbox(); }
}

function listenKey () {	document.onkeypress = getKey; }

function fadeIn() {
	if(t<=100) {	
		t+=50;
		ramt = (t == 100)?99.999:t;
		fadeObj.style.filter = "alpha(opacity:"+ramt+")";
		fadeObj.style.KHTMLOpacity = ramt/100;
		fadeObj.style.MozOpacity = ramt/100;
		fadeObj.style.opacity = ramt/100;
	} else {
		clearInterval(tmr);
		t = 0;
	}	
}
	
function showLightbox(objLink) {
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objContent = document.getElementById(objLink.getAttribute('title'));
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	// center lightbox and make sure that the top and left values are not negative
	// and the content placed outside the viewport
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 480) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 640) / 2);
		
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	// A small pause between the image loading and displaying is required with IE,
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		waitFor(250);
	}
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}

	objOverlay.style.display = 'block';
	objContent.style.display = 'block';
	objLightbox.style.display = 'block';
	
	fadeObj = objLightbox;
	tmr = setInterval("fadeIn()",1);

	// Check for 'x' keypress
	listenKey();		
}

function hideLightbox() {
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	objList = new Array('location_info','contact_info','tshirt_info');
	
	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// hide list of info divs
	for(i = 0; i != objList.length; i++) {
		var obj = document.getElementById(objList[i]);
		obj.style.display = 'none';
	}
	
	// reset opacity on lightbox
	objLightbox.style.filter = "alpha(opacity:0)";
	objLightbox.style.KHTMLOpacity = 0;
	objLightbox.style.MozOpacity = 0;
	objLightbox.style.opacity = 0;

	// make select boxes visible
	selects = document.getElementsByTagName("select");
  	for(i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}

function initLightbox() {
	var anchors = document.getElementsByTagName("a");
	var ol = document.getElementById("overlay");

	// loop through all anchor tags
	for(var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () { showLightbox(this); return false; }
		}
	}
	
	ol.onclick = function() {hideLightbox()}	
}