// JavaScript Document
/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com
*/

var loadingImage = 'loading.gif';		
var closeButton = 'close.gif';		

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


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(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 showLightbox(chosenState)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var arrayPageSize = getPageSize();	
	var arrayPageScroll = getPageScroll();
	
	var objForm = document.getElementById('popForm');
	var objTitle = document.getElementById('popUpTitle');
	var objTopAnchors = document.getElementById('popUpTop').getElementsByTagName('A');
	var objFootAnchors = document.getElementById('popUpFoot').getElementsByTagName('A');
	
	var selCategory = document.getElementById('popCategory');
	var selLocation = document.getElementById('popLocation');
	var selType = document.getElementById('popType');
	
	if(chosenState == 'do'){
		objForm.action = '/visitors/';		
		objTitle.className = 'doTitle';
		setClass(objTopAnchors, 'lightboxNav', 'lightboxNav linkDo', '/visitors/');	
		setClass(objFootAnchors, 'lightboxNav', 'lightboxNav linkDo', '/visitors/');		
		selCategory.className='';
		selLocation.className='hidden';
		selType.className='hidden';
	} else if(chosenState == 'eat'){
		objForm.action = '/visitors/dining/';
		selCategory.value = '';
		objTitle.className = 'eatTitle';
		setClass(objTopAnchors, 'lightboxNav', 'lightboxNav linkEat', '/visitors/dining/');	
		setClass(objFootAnchors, 'lightboxNav', 'lightboxNav linkEat', '/visitors/dining/');	
		selCategory.className='hidden';
		selLocation.className='';
		selType.className='hidden';
	} else if(chosenState == 'stay'){
		objForm.action = '/visitors/accommodations/';
		selCategory.value = '';
		objTitle.className = 'stayTitle';
		setClass(objTopAnchors, 'lightboxNav', 'lightboxNav linkStay', '/visitors/accommodations/');	
		setClass(objFootAnchors, 'lightboxNav', 'lightboxNav linkStay', '/visitors/accommodations/');	
		selCategory.className='hidden';
		selLocation.className='';
		selType.className='';
	}
	

	objLightbox.style.left = (((arrayPageSize[0] - 352) / 2) + 'px');
	objLightbox.style.display = 'block';
	
	objLightbox.className = '';
	objLightbox.style.zIndex = '1000';	

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}

function setClass(anchorList, searchClass, setClass, linkLocation){
	var anchorLength = anchorList.length;
	for(var i=0; i<anchorLength; i++){
		if(anchorList[i].className.indexOf(searchClass) >= 0){
			anchorList[i].className = setClass;
			anchorList[i].href = linkLocation;
		}
	}
}

function hideLightbox()
{
	
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	objLightbox.className = 'hidden';	
	
	// disable keypress listener
	document.onkeypress = '';
}

function initLightbox()
{
	
	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") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}
	
	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 () {hideLightbox(); 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%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

}

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

}

addLoadEvent(initLightbox);	// run initLightbox onLoad