// This code was inspired by Lightbox JS at http://www.huddletogether.com/projects/lightbox/
// but I've changed it so much that it's unrecognisable, except for the code he borrowed to begin with.
// -Rick-

// Set this to hold all the screenshots on the page.
var screenshots = [];

//
// 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;
}

function showLightboxNav(objAnchor)
{
	if(objAnchor.href.slice(-1) == '#')
	{
		return false;
	}

	var objImage = document.getElementById('lightboxImage');
	var objLoader = document.getElementById('loadDiv');
	
	var arrayPageScroll = getPageScroll();
	var loaderPos = arrayPageScroll[1] + objImage.height / 2;

	objLoader.style.top = (loaderPos + 'px');
	objLoader.style.display = 'block';

	return showLightbox(objAnchor);
}

function showLightboxExplicit(href, navless)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objImage = document.getElementById('lightboxImage');
	var objPrevAnchor = document.getElementById('prevAnchor');
	var objNextAnchor = document.getElementById('nextAnchor');

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	objLightbox.style.display = 'block';

	objLightbox.style.top = (arrayPageScroll[1] + 'px');

	objImage.onload=function()
	{
		var objLoader = document.getElementById('loadDiv');
		objLoader.style.display = 'none';
	}

	objImage.src = href;

	if(navless == null || navless == false)
	{
		var i = 0;
		for(; i < screenshots.length && screenshots[i] != href.slice(href.length - screenshots[i].length); i++)
		{
		}

		if(i > 0)
		{
			objPrevAnchor.href = screenshots[i-1];
			objPrevAnchor.removeAttribute('class');
			objPrevAnchor.removeAttribute('className');
		}
		else
		{
			objPrevAnchor.href = '#';
			objPrevAnchor.setAttribute('class', 'screenshotNavDisabled');
			objPrevAnchor.setAttribute('className', 'screenshotNavDisabled');
		}

		if(i < screenshots.length - 1)
		{
			objNextAnchor.href = screenshots[i+1];
			objNextAnchor.removeAttribute('class');
			objNextAnchor.removeAttribute('className');
		}
		else
		{
			objNextAnchor.href = '#';
			objNextAnchor.setAttribute('class', 'screenshotNavDisabled');
			objNextAnchor.setAttribute('className', 'screenshotNavDisabled');
		}
	}
	else
	{
		objPrevAnchor.setAttribute('class', 'hide');
		objPrevAnchor.setAttribute('className', 'hide');
		objNextAnchor.setAttribute('class', 'hide');
		objNextAnchor.setAttribute('className', 'hide');
	}

	return false;
}

function showLightbox(objAnchor)
{
	return showLightboxExplicit(objAnchor.href, false);
}

function showLightboxSingle(objAnchor)
{
	return showLightboxExplicit(objAnchor.href, true);
}

function hideLightbox()
{
	// get objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objImage = document.getElementById('lightboxImage');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// Reset the image
	objImage.src = '/loading.gif';

	return false;
}
