// -----------------------------------------------------------------------------------
//
//	Lightbox v2.03
//	by Lokesh Dhakar - http://www.huddletogether.com
//	4/9/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	Lightbox Class Declaration
	- initialize()
	- start()
	- changeImage()
	- resizeImageContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadNeighborImages()
	- end()

	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- showFlash()
	- hideFlash()
	- pause()
	- initLightbox()

	Function Calls
	- addLoadEvent(initLightbox)

*/
// -----------------------------------------------------------------------------------

//
//	Configuration
//
var fileLoadingImage = "../lightbox/images/loading.gif";
var filetopNavCloseImage = "../lightbox/images/close.gif";
var fileshowCircleImage = "../lightbox/images/show.gif";

var animate = true;	// toggles resizing animations

var resizeSpeed = 10;	// controls the speed of the image resizing (1=slowest and 10=fastest)

var borderSizeVertical = 0;	//if you adjust the padding in the CSS, you will need to update this variable

var borderSizeHorizontal = 0;	//if you adjust the padding in the CSS, you will need to update this variable

var bottomHeightDecrease = 0;	// corrects showing of the vertical scrollbar

var delayShowTime = 2;	// time in secs for showing the rest of the content if mouse is over one of the navigational arrows

var timerHandle = 0; // used to set the delay trigger only once, when new image is loaded

var linkDisplayActive = 0; // used to detect if mouse got on the navigation arrows while controls were disabled

var oldMouseX = null;
var oldMouseY = null;

// -----------------------------------------------------------------------------------

//
//	Global Variables
//
var imageArray = new Array;
var activeImage;

if(animate == true){
	overlayDuration = 0.2;	// shadow fade in/out duration
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else {
	overlayDuration = 0;
	resizeDuration = 0;
}

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth;
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src;
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------

//
//	Lightbox Class Declaration
//	- initialize()
//	- start()
//	- changeImage()
//	- resizeImageContainer()
//	- showImage()
//	- updateDetails()
//	- updateNav()
//	- enableKeyboardNav()
//	- disableKeyboardNav()
//	- keyboardNavAction()
//	- preloadNeighborImages()
//	- end()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var Lightbox = Class.create();

Lightbox.prototype = {

	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for
	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow
	// overlay and the image container.
	//
	initialize: function() {
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		var areas = document.getElementsByTagName('area');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];

			var relAttribute = String(anchor.getAttribute('rel'));

			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				anchor.onclick = function () {myLightbox.start(this); return false;}
			}
		}

		// loop through all area tags
		// todo: combine anchor & area tag loops
		for (var i=0; i< areas.length; i++){
			var area = areas[i];

			var relAttribute = String(area.getAttribute('rel'));

			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (area.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				area.onclick = function () {myLightbox.start(this); return false;}
			}
		}

		// The rest of this code inserts html at the bottom of the page that looks similar to this:
		//
		//	<div id="overlay"></div>
		//	<div id="lightbox">
		//		<div id="imageTitleContainer">
		//			<span id="title1"></span><span id="title2"></span>
		//		</div>
		//		<div id="topNav">
		//			<a href="#" id="topNavClose">
		//				<img src="images/close.gif">
		//			</a>
		//		</div>
		//		<div id="outerImageContainer">
		//			<div id="imageContainer">
		//				<img id="lightboxImage">
		//				<div id="loading">
		//					<a href="#" id="loadingLink">
		//						<img src="images/loading.gif">
		//					</a>
		//				</div>
		//			</div>
		//		</div>
		//		<div id="imageDataContainer">
		//			<div id="imageData">
		//				<div id="imageDetails">
		//					<span id="caption">
		//						<span id="caption1"></span><span id="caption2"></span><span id="caption3"></span>
		//					</span>
		//					<div id="displayNavOuter">
		//						<div id="displayNav">
		//							<span id="navPhotos"></span>
		//							<a href="#" id="prevLink"></a>
		//							<a href="#" id="nextLink"></a>
		//						</div>
		//					</div>
		//				</div>
		//			</div>
		//		</div>
		//	</div>

		objBody = document.getElementsByTagName('body').item(0);

		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);

		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.style.display = 'none';
		objBody.appendChild(objLightbox);

		var objImageTitleContainer = document.createElement("div");
		objImageTitleContainer.setAttribute('id','imageTitleContainer');
		objLightbox.appendChild(objImageTitleContainer);

		objtopNav = document.createElement("div");
		objtopNav.setAttribute('id','topNav');
		objLightbox.appendChild(objtopNav);

		objtopNavCloseLink = document.createElement("a");
		objtopNavCloseLink.setAttribute('id','topNavClose');
		objtopNavCloseLink.setAttribute('href','#');
		objtopNav.appendChild(objtopNavCloseLink);

		var objtopNavCloseImage = document.createElement("img");
		objtopNavCloseImage.setAttribute('src', filetopNavCloseImage);
		objtopNavCloseLink.appendChild(objtopNavCloseImage);

		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','outerImageContainer');
		objLightbox.appendChild(objOuterImageContainer);

		// When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
		// If animations are turned off, it will be hidden as to prevent a flicker of a
		// white 250 by 250 box.
		if(animate){
			Element.setWidth('outerImageContainer', 250);
			Element.setHeight('outerImageContainer', 250);
		} else {
			Element.setWidth('outerImageContainer', 1);
			Element.setHeight('outerImageContainer', 1);
		}

		var objImageContainer = document.createElement("div");
		objImageContainer.setAttribute('id','imageContainer');
		objOuterImageContainer.appendChild(objImageContainer);

		var objLightboxImage = document.createElement("img");
		objLightboxImage.setAttribute('id','lightboxImage');
		objImageContainer.appendChild(objLightboxImage);

		var objLoading = document.createElement("div");
		objLoading.setAttribute('id','loading');
		objImageContainer.appendChild(objLoading);

		var objLoadingLink = document.createElement("a");
		objLoadingLink.setAttribute('id','loadingLink');
		objLoadingLink.setAttribute('href','#');
		objLoadingLink.onclick = function() { myLightbox.end(); return false; }
		objLoading.appendChild(objLoadingLink);

		var objLoadingImage = document.createElement("img");
		objLoadingImage.setAttribute('src', fileLoadingImage);
		objLoadingLink.appendChild(objLoadingImage);

		objImageDataContainer = document.createElement("div");
		objImageDataContainer.setAttribute('id','imageDataContainer');
		objImageDataContainer.className = 'clearfix';
		objLightbox.appendChild(objImageDataContainer);

		var objImageData = document.createElement("div");
		objImageData.setAttribute('id','imageData');
		objImageDataContainer.appendChild(objImageData);

		var objImageDetails = document.createElement("div");
		objImageDetails.setAttribute('id','imageDetails');
		objImageData.appendChild(objImageDetails);

		var objCaption = document.createElement("span");
		objCaption.setAttribute('id','caption');
		objImageDetails.appendChild(objCaption);

		var objdisplayNavOuter = document.createElement("span");
		objdisplayNavOuter.setAttribute('id','displayNavOuter');
		objImageDetails.appendChild(objdisplayNavOuter);

		var objdisplayNav = document.createElement("span");
		objdisplayNav.setAttribute('id','displayNav');
		objdisplayNavOuter.appendChild(objdisplayNav);

		var objnavPhotos = document.createElement("span");
		objnavPhotos.setAttribute('id','navPhotos');
		objdisplayNav.appendChild(objnavPhotos);

		objPrevLink = document.createElement("a");
		objPrevLink.setAttribute('id','prevLink');
		objPrevLink.setAttribute('href','#');
		objdisplayNav.appendChild(objPrevLink);

		objNextLink = document.createElement("a");
		objNextLink.setAttribute('id','nextLink');
		objNextLink.setAttribute('href','#');
		objdisplayNav.appendChild(objNextLink);

	},

	//
	//	start()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function(imageLink) {

		hideSelectBoxes();
		hideFlash();
		var objBody = document.getElementsByTagName('body').item(0);
		objBody.style.overflow = 'hidden';
		// hide some of the div elements
		Element.setStyle('imageDataContainer', {'visibility': 'hidden'});
		Element.setStyle('navPhotos', {'visibility': 'hidden'});
		Element.setStyle('prevLink', {'visibility': 'visible'});
		Element.setStyle('nextLink', {'visibility': 'visible'});
		Element.setStyle('displayNav', {'visibility': 'visible'});
		Element.hide('topNavClose');

		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();

		var objHTML = document.getElementsByTagName('html').item(0);
		//alert(objHTML.scrollHeight+' '+arrayPageSize[1]+' '+arrayPageSize[3]);

		Element.setHeight('overlay', arrayPageSize[3]-bottomHeightDecrease);
		Element.setHeight('lightbox', arrayPageSize[3]-bottomHeightDecrease);

		//alert(objHTML.scrollHeight+' '+arrayPageSize[1]+' '+arrayPageSize[3]);

		new Effect.Appear('overlay', { duration: overlayDuration, from: 0.0, to: 1.0 });

		imageArray = [];
		imageNum = 0;

		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// if image is NOT part of a set..
		if((imageLink.getAttribute('rel') == 'lightbox')){
			// add single image to imageArray
			imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('alt'), anchor.getAttribute('title')));
		} else {
		// if image is part of a set..

			// loop through anchors, find other images in set, and add them to imageArray
			for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];
				if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
					imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('alt'), anchor.getAttribute('title')));
				}
			}
			imageArray.removeDuplicates();
			while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}

		Element.show('lightbox');

		this.changeImage(imageNum);

	},

	//
	//	changeImage()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	changeImage: function(imageNum) {
		// disable and hide the data and nav controls
		disableControls();

		activeImage = imageNum;	// update global var

		// hide elements during transition
		if(animate){ Element.show('loading');}
		Element.hide('lightboxImage');
		Element.hide('displayNav');
		Element.hide('imageDataContainer');
		Element.hide('navPhotos');
		Element.hide('topNavClose');

		imgPreloader = new Image();

		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			Element.setSrc('lightboxImage', imageArray[activeImage][0]);
			myLightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
		}
		imgPreloader.src = imageArray[activeImage][0];

		disableEnableControls();

	},

	//
	//	resizeImageContainer()
	//
	resizeImageContainer: function( imgWidth, imgHeight) {

		// get curren width and height
		this.widthCurrent = Element.getWidth('outerImageContainer');
		this.heightCurrent = Element.getHeight('outerImageContainer');

		// get new width and height
		var widthNew = (imgWidth  + (borderSizeHorizontal * 2));
		var heightNew = (imgHeight  + (borderSizeVertical * 2));

		// scalars based on change from old to new
		this.xScale = ( widthNew / this.widthCurrent) * 100;
		this.yScale = ( heightNew / this.heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		wDiff = this.widthCurrent - widthNew;
		hDiff = this.heightCurrent - heightNew;

		if(!( hDiff == 0)){ new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
		if(!( wDiff == 0)){ new Effect.Scale('outerImageContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		// if new and old image are same size and no scaling transition is necessary,
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
		}

		// Element.setHeight('prevLink', imgHeight);
		// Element.setHeight('nextLink', imgHeight);
		//Element.setWidth( 'imageDataContainer', widthNew);

		var arrayPageSize = getPageSize();
		imgActive = new Image();
		imgActive.src = imageArray[activeImage][0];
		var arrayPageScroll = getPageScroll();
		var outerImageContainerTop = Math.floor(((arrayPageSize[3])-imgActive.height)/2);
		Element.setTop('outerImageContainer', outerImageContainerTop);
		Element.setHeight('topNav', arrayPageSize[3]);
		Element.show('overlay'); // needed to get the width right in the code below
		captionLeftRightMarginPadding = 0;
		captionLeftRightMarginPaddingExtra = Element.getStyle('caption', 'margin-right');
		captionLeftRightMarginPadding += eval(captionLeftRightMarginPaddingExtra.substring(0,captionLeftRightMarginPaddingExtra.length-2));
		captionLeftRightMarginPaddingExtra = Element.getStyle('caption', 'margin-left');
		captionLeftRightMarginPadding += eval(captionLeftRightMarginPaddingExtra.substring(0,captionLeftRightMarginPaddingExtra.length-2));
		captionLeftRightMarginPaddingExtra = Element.getStyle('caption', 'padding-left');
		captionLeftRightMarginPadding += eval(captionLeftRightMarginPaddingExtra.substring(0,captionLeftRightMarginPaddingExtra.length-2));
		captionLeftRightMarginPaddingExtra = Element.getStyle('caption', 'padding-right');
		captionLeftRightMarginPadding += eval(captionLeftRightMarginPaddingExtra.substring(0,captionLeftRightMarginPaddingExtra.length-2));
		var captionWidth = Math.floor(((Element.getWidth('overlay')-imgActive.width-captionLeftRightMarginPadding)/2));
		Element.setWidth('caption', captionWidth);

		this.showImage();

	},

	//
	//	showImage()
	//	Display image and begin preloading neighbors.
	//
	showImage: function(){
		Element.hide('loading');
		new Effect.Appear('lightboxImage', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myLightbox.updateDetails(); } });
		this.preloadNeighborImages();
	},

	//
	//	updateDetails()
	//	Display caption, image number, and bottom nav.
	//
	updateDetails: function() {

		// Set the proper title from title which contains captions separated by :
		title_caption = imageArray[activeImage][2];
		// Get the first part of the title
		title1_start = 0;
		title1_end = title_caption.indexOf(":", title1_start);
		title1_txt = title_caption.slice(title1_start,title1_end);
		// Get the second part of the title
		title2_start = title1_end + 1 ;
		title2_end = title_caption.length;
		title2_txt = title_caption.slice(title2_start,title2_end);
		//create the title inner HTML
		title_caption ='<span id="title1">'+title1_txt+':</span>'+'<span id="title2">'+title2_txt+'</span>';
		Element.show('imageTitleContainer');
		Element.setInnerHTML( 'imageTitleContainer', title_caption);

		// Set the proper caption from alt which contains captions separated by |
		alt_caption = imageArray[activeImage][1];
		// Get the first part of the caption
		caption1_start = 0;
		caption1_end = alt_caption.indexOf("|", caption1_start);
		caption1_txt = alt_caption.slice(caption1_start,caption1_end);
		// Get the second part of the caption
		caption2_start = caption1_end + 1 ;
		caption2_end = alt_caption.indexOf("|", caption2_start);
		caption2_txt = alt_caption.slice(caption2_start,caption2_end);
		// Get the third part of the caption
		caption3_start = caption2_end + 1 ;
		caption3_end = alt_caption.length;
		caption3_txt = alt_caption.slice(caption3_start,caption3_end);
		//create the caption inner HTML
		alt_caption ='<span id="caption1">'+caption1_txt+'</span><br/>'+'<span id="caption2">'+caption2_txt+'</span><br/>'+'<span id="caption3">'+caption3_txt+'</span>'
		Element.show('caption');
		Element.setInnerHTML( 'caption', alt_caption);

		// Create navigation bar if image is part of set
		if(imageArray.length > 1){
			Element.show('navPhotos');
			navPhotosTxt = '';
			for (i = 0; i < imageArray.length; i++)
			{
				if ( i == activeImage ) {
					navPhotosTxt += ' ' + '<span id="currentImage">'+eval(i+1)+'</span>';
				} else {
					navPhotosTxt += ' ' + '<a href="javascript: myLightbox.changeImage('+i+');window.scrollTo(0,0);">'+eval(i+1)+'</a>';
				}
			}
			Element.setInnerHTML( 'navPhotos', navPhotosTxt);
		}

		new Effect.Parallel(
			[ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: resizeDuration, from: 0.0, to: 1.0 }),
			  new Effect.Appear('imageDataContainer', { sync: true, duration: resizeDuration }) ],
			{ duration: resizeDuration, afterFinish: function() {
				// update overlay size and update nav
				var arrayPageSize = getPageSize();
				Element.setHeight('overlay', arrayPageSize[3]-bottomHeightDecrease);
				myLightbox.updateNav();
				}
			}
		);
	},

	//
	//	updateNav()
	//	Display appropriate previous and next hover navigation.
	//
	updateNav: function() {

		Element.show('displayNav');
		Element.setStyle('displayNav', {'visibility': 'visible'});
		Element.setStyle('prevLink', {'visibility': 'hidden'});
		Element.setStyle('nextLink', {'visibility': 'hidden'});

		// if not first image in set, display prev image button
		if(activeImage != 0){
			if (Element.getStyle('displayNav', 'visibility') == 'visible') {
				Element.setStyle('prevLink', {'visibility': 'visible'});
				document.getElementById('prevLink').onclick = function() {
					myLightbox.changeImage(activeImage - 1); return false;
				}
			}
		}

		// if not last image in set, display next image button
		if(activeImage != (imageArray.length - 1)){
			if (Element.getStyle('displayNav', 'visibility') == 'visible') {
				Element.setStyle('nextLink', {'visibility': 'visible'});
				document.getElementById('nextLink').onclick = function() {
					myLightbox.changeImage(activeImage + 1); return false;
				}
			}
		}

		this.enableKeyboardNav();
	},

	//
	//	enableKeyboardNav()
	//
	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction;
	},

	//
	//	disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	//
	//	keyboardAction()
	//
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}

		key = String.fromCharCode(keycode).toLowerCase();

		if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){	// close lightbox
			myLightbox.end();
		} else if((key == 'p') || (keycode == 37)){	// display previous image
			if(activeImage != 0){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage - 1);
			}
		} else if((key == 'n') || (keycode == 39)){	// display next image
			if(activeImage != (imageArray.length - 1)){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage + 1);
			}
		}

	},

	//
	//	preloadNeighborImages()
	//	Preload previous and next images.
	//
	preloadNeighborImages: function(){

		if((imageArray.length - 1) > activeImage){
			preloadNextImage = new Image();
			preloadNextImage.src = imageArray[activeImage + 1][0];
		}
		if(activeImage > 0){
			preloadPrevImage = new Image();
			preloadPrevImage.src = imageArray[activeImage - 1][0];
		}

	},

	//
	//	end()
	//
	end: function() {
		this.disableKeyboardNav();
		Element.hide('lightbox');
		new Effect.Fade('overlay', { duration: overlayDuration});
		showSelectBoxes();
		showFlash();
		objBody.style.overflow = 'visible';
		Element.setInnerHTML( 'imageTitleContainer', '');
	}
}

// -----------------------------------------------------------------------------------

//
// 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;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();

	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }

// ---------------------------------------------------

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}


// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Help from Ran Bar-On [ran2103@gmail.com]
//

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}
/*
function pause(numberMillis) {
	var curently = new Date().getTime() + sender;
	while (new Date().getTime();
}
*/
// ---------------------------------------------------

function disableEnableControls() {
	if (timerHandle == 0) {
		disableControls();
		timerHandle=setTimeout("disableEnableControls()", delayShowTime*1000);
	} else {
		enableControls();
	}
}

function disableControls() {
	clearTimeout(timerHandle);
	timerHandle = 0;
	hideDataNavElements ();
	objtopNav.onmouseout = function() {};
	objtopNav.onmouseover = function() {};
	objtopNavCloseLink.onclick = function() {};
	objImageDataContainer.onmouseout = function() {};
	objImageDataContainer.onmouseover = function() {};
	objPrevLink.onmouseover = function() { linkDisplayActive = 1;};
	objPrevLink.onmouseout = function() {};
	objNextLink.onmouseover = function() { linkDisplayActive = 1;};
	objNextLink.onmouseout = function() {};
}

function enableControls() {
	// clear timer
	clearTimeout(timerHandle);
	timerHandle = 0;
	// set functions for control of display
	objtopNav.onmouseout = function() {
		hideDataNavElements ();
		}
	objtopNav.onmouseover = function() {
		Element.show('topNavClose');
		}
	objtopNavCloseLink.onclick = function() {
		navCloseClick ();
		}
	objImageDataContainer.onmouseout = function() {
		hideDataNavElements ();
		}
	objImageDataContainer.onmouseover = function() {
		showDataNavElements ();
		}
	objPrevLink.onmouseover = function() {
		showDataNavElements ();
		}
	objNextLink.onmouseover = function() {
		showDataNavElements ();
		}
	// activate display if there was movement while controls were disabled
	if ( linkDisplayActive == 1 ) {
		showDataNavElements ();
	}
}

function showDataNavElements () {
		Element.setStyle('displayNav', {'visibility': 'visible'});
		Element.setStyle('imageDataContainer', {'visibility': 'visible'});
		Element.setStyle('navPhotos', {'visibility': 'visible'});
		Element.show('topNavClose');
}

function hideDataNavElements () {
		Element.setStyle('displayNav', {'visibility': 'hidden'});
		Element.setStyle('imageDataContainer', {'visibility': 'hidden'});
		Element.setStyle('navPhotos', {'visibility': 'hidden'});
		Element.hide('topNavClose');
}

function navCloseClick () {
		objBody.style.overflow = 'visible';
		Element.setInnerHTML( 'imageTitleContainer', '');
		myLightbox.end(); return false;
}

function initLightbox() { myLightbox = new Lightbox(); }
Event.observe(window, 'load', initLightbox, false);

