

var resizeSpeed = 8; /* 1 = slowest, 10 = fastest */
var borderSize = 10; /* Update this f you adjust the padding in the CSS */

/* Globals */
var imageGroups = new Array;
var imageArray;
var activeImage;

if (resizeSpeed > 10) { resizeSpeed = 10; }
if ( resizeSpeed < 1 ) { resizeSpeed = 1; }
var resizeDuration = (11 - resizeSpeed) * 0.15;

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

/* A lightbox object */
function lbobj() {
	var thumbURL;
	var fullURL;
	var title;
	var description;
}

/* Push a new lightbox object into a certain group */
function LightboxPush(group,thumbURL,fullURL,title,description) {
	if ( !imageGroups[group] ) { imageGroups[group] = new Array; }
	var a = imageGroups[group];
	var l = new lbobj();
	l.thumbURL = thumbURL;
	l.fullURL = fullURL;
	l.title = title;
	l.description = description;
	a.push(l);
}

/* Push a new lightbox object into a certain group */
function LightboxPush2(group,thumbURL,fullURL,origURL,title,description) {
	if ( !imageGroups[group] ) { imageGroups[group] = new Array; }
	var a = imageGroups[group];
	var l = new lbobj();
	l.thumbURL = thumbURL;
	l.fullURL = fullURL;
	l.origURL = origURL;
	l.title = title;
	l.description = description;
	a.push(l);
}

var Lightbox = Class.create();

Lightbox.prototype = {

	initialize: function() {
	},

	/*
	 * Starts the lightbox.
	 */
	start: function(groupName,index) {	
		$$('object').each(
			function(node) { node.style.visibility = 'hidden' }
		);
		$$('embed').each(
			function(node) { node.style.visibility = 'hidden' }
		);
		$$('select').each(
			function(node) { node.style.visibility = 'hidden' }
		);
		imageArray = imageGroups[groupName]; /* SETS THE GLOBAL imageArray */
		if ( !index ) { index = 0; } /* Start at first in group by default. */
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		Element.setHeight('lightbox_overlay', arrayPageSize[1]);
		new Effect.Appear('lightbox_overlay', { duration: 0.2, from: 0.0, to: 0.8 });
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
		Element.setTop('lightbox', lightboxTop);
		Element.show('lightbox');
		this.changeImage(index);
	},

	nextImage: function() {
		if ( activeImage + 1 >= imageArray.length ) {
			lightbox.changeImage(0);
		}
		else {
			lightbox.changeImage(activeImage+1);
		}
	},

	prevImage: function() {
		if ( activeImage == 0 ) {
			lightbox.changeImage(imageArray.length-1);
		}
		else {
			lightbox.changeImage(activeImage-1);
		}
	},

	/*
	 * Stuff to do when the image changes.
	 */
	changeImage: function(index) {	
		activeImage = index; // update global var
		// hide elements during transition
		Element.show('lightbox_loading');
		Element.hide('lightbox_lightboxImage');
		Element.hide('lightbox_imageDataContainer');
		Element.hide('lightbox_controls');		
		preloader = new Image();
		// once image is preloaded, resize image container
		preloader.onload = function() {
			Element.setSrc('lightbox_lightboxImage',(imageArray[activeImage]).fullURL);
			lightbox.resizeImageContainer(preloader.width,preloader.height);
		}
		preloader.src = (imageArray[activeImage]).fullURL;
	},

	//	resizeImageContainer()
	resizeImageContainer: function(imgWidth,imgHeight) {
		// get current height and width
		this.wCur = Element.getWidth('lightbox_outerImageContainer');
		this.hCur = Element.getHeight('lightbox_outerImageContainer');
		// scalars based on change from old to new
		this.xScale = ((imgWidth  + (borderSize * 2)) / this.wCur) * 100;
		this.yScale = ((imgHeight  + (borderSize * 2)) / this.hCur) * 100;
		// calculate size diff between new and old image; resize if necessary
		wDiff = (this.wCur - borderSize * 2) - imgWidth;
		hDiff = (this.hCur - borderSize * 2) - imgHeight;
		if ( hDiff != 0 ) {
			new Effect.Scale(
				'lightbox_outerImageContainer',
				this.yScale, {
					scaleX: false, 
					duration: resizeDuration, 
					queue: 'front'
				}
			);
		}
		if ( wDiff != 0 ) {
			new Effect.Scale(
				'lightbox_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.setWidth( 'lightbox_imageDataContainer', imgWidth + (borderSize * 2));
		this.showImage();
	},


	//	showImage()
	//	Display image and begin preloading neighbors.
	showImage: function() {
		Element.hide('lightbox_loading');
		new Effect.Appear(
			'lightbox_lightboxImage', 
			{
				duration: 0.5,
				queue: 'end',
				afterFinish: function() {
					lightbox.updateDetails();
				}
			}
		);
		this.preloadNeighborImages();
	},


	slideShowStart: function() {
		if ( this.slideShowOn ) { return; }
		this.slideShowOn = true;
		lightbox.slideShowNext();
	},

	slideShowStop: function() {
		$('lightbox_slideshow_stop').style.display = 'none';
		$('lightbox_std_buttons').style.display = 'inline';
		this.slideShowOn = false;
		clearTimeout(this.slideShowTimer);
	},

	slideShowToggle: function() {
		if ( this.slideShowOn  ) { lightbox.slideShowStop(); }
		else { lightbox.slideShowStart(); }
	},

	slideShowNext: function() {
		lightbox.nextImage();
		this.slideShowTimer = setTimeout('lightbox.slideShowNext()',5000);
	},

	//	updateDetails()
	//	Display caption, image number, and bottom nav.
	updateDetails: function() {
		Element.show('lightbox_caption');
		var captionHTML = '';
		if ( (imageArray[activeImage]).title ) {
			captionHTML += '<div id="lightbox_caption_title">';
			captionHTML += (imageArray[activeImage]).title;
			captionHTML += '</div>';
		}
		if ( (imageArray[activeImage]).description ) {
			captionHTML += '<div id="lightbox_caption_description">';
			captionHTML += (imageArray[activeImage]).description;
			captionHTML += '</div>';
		}
		captionHTML = captionHTML.replace(/&lt;/g,'<');
		captionHTML = captionHTML.replace(/&gt;/g,'>');
		captionHTML = captionHTML.replace(/&#34;/g,'"');
		captionHTML = captionHTML.replace(/&#39;/g,"'");
		captionHTML = captionHTML.replace(/&amp;/g,'&');
		Element.setInnerHTML('lightbox_caption',captionHTML);
		
		// if image is part of set display 'Image x of x' 
		var t = '';
		if ( imageArray.length > 1 ) {
			t += "<span id='lightbox_number_count'>";
			t += eval(activeImage + 1);
			t += "&nbsp;of&nbsp;" + imageArray.length;
			t += "</span>\n";
			if ( this.slideShowOn ) {
				t += "<span id='lightbox_std_buttons' style='display:none'>";
			}
			t += "<a href='javascript:void(0)' ";
			t += "onclick='lightbox.prevImage();";
			t += "return false;'>";
			t += "<span class='lightbox_keycode'>P</span>rev";
			t += "</a>\n";
			t += "<a href='javascript:void(0)' ";
			t += "onclick='lightbox.nextImage();";
			t += "return false;'>";
			t += "<span class='lightbox_keycode'>N</span>ext";
			t += "</a>\n";
			t += "<a href='javascript:void(0)' ";
			t += "id='lightbox_slideshow_start' ";
			t += "onclick='lightbox.slideShowStart();";
			t += "return false;'>";
			t += "<span class='lightbox_keycode'>S</span>lideshow";
			t += "</a>\n";
			if ( this.slideShowOn ) {
				t += "</span>\n"; // closes #lightbox_std_buttons above
				t += "<a href='javascript:void(0)' ";
				t += "id='lightbox_slideshow_stop' ";
				t += "onclick='lightbox.slideShowStop();";
				t += "return false;'>";
				t += "<span class='lightbox_keycode'>S</span>top";
				t += "</a>\n";
			}
		}
		var thisImg = imageArray[activeImage];
		if ( !this.slideShowOn && thisImg.origURL ) {
			t += "<a href='" + thisImg.origURL + "' target='_blank'>";
			t += "HiRes</a>\n";
		}
		t += "<a href='javascript:void(0)' ";
		t += "onclick='javascript:lightbox.end();'>";
		t += "<span class='lightbox_keycode'>C</span>lose";
		t += "</a>\n";
		Element.show('lightbox_controls');
		Element.setInnerHTML('lightbox_controls',t);

		new Effect.Parallel(
			[
				new Effect.SlideDown(
					'lightbox_imageDataContainer',
					{
						sync: true,
						duration: resizeDuration + 0.25,
						from: 0.0,
						to: 1.0 
					}
				), 
				new Effect.Appear(
					'lightbox_imageDataContainer',
					{
						sync: true,
						duration: 1.0
					}
				)
			], 
			{
				duration: 0.65,
				afterFinish: function() {
					lightbox.updateNav();
				}
			} 
		);
	},


	//	updateNav()
	//	Display appropriate previous and next hover navigation.
	updateNav: function() {
		this.enableKeyboardNav();
	},

	/* Enable keyboard navigation */
	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction; 
	},

	/* Disable keyboard navigation */
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	/* Keyboard action */
	keyboardAction: function(e) {
		if (e == null) { keycode = event.keyCode; } // IE
		else { keycode = e.which; } // Gecko

		key = String.fromCharCode(keycode).toLowerCase();
		
		/* close lightbox */
		if ( key == 'c' ) {
			lightbox.end();
		}
		/* display previous image */
		else if ( key == 'p' && !this.slideShowOn ) {
			lightbox.disableKeyboardNav();
			lightbox.prevImage();
		}
		/* display next image */
		else if ( key == 'n' && !this.slideShowOn ) {
			lightbox.disableKeyboardNav();
			lightbox.nextImage();
		}
		else if ( key == 's' ) {
			lightbox.slideShowToggle();
		}
	},


	// Preload previous and next images.
	preloadNeighborImages: function(){
		var preloadNextImage = new Image();
		if ((imageArray.length - 1) > activeImage) {
			preloadNextImage.src = (imageArray[activeImage+1]).fullURL;
		}
		else {
			preloadNextImage.src = (imageArray[0]).fullURL;
		}
		var preloadPrevImage = new Image();
		if (activeImage > 0) {
			preloadPrevImage.src = (imageArray[activeImage-1]).fullURL;
		}
		else {
			preloadPrevImage.src = (imageArray[imageArray.length-1]).fullURL;
		}
	},

	// End lightbox
	end: function() {
		if ( this.slideShowOn ) { this.slideShowStop(); }
		this.disableKeyboardNav();
		Element.hide('lightbox');
		new Effect.Fade('lightbox_overlay', { duration: 0.2});
		$$('object').each(
			function(node) { node.style.visibility = 'visible' }
		);
		$$('embed').each(
			function(node) { node.style.visibility = 'visible' }
		);
		$$('select').each(
			function(node) { node.style.visibility = 'visible' }
		);
	}
}



// Returns array with x,y page scroll values.
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	}
	/* Explorer 6 Strict */
	else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	}
	/* All other Explorers */
	else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	return new Array('',yScroll) 
}





/* Returns array with page width, height and window width, height. */
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) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	// Explorer Mac, also Explorer 6 Strict, Mozilla and Safari...
	else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	/* Explorer 6 Strict Mode */
	else if (document.documentElement && document.documentElement.clientHeight){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	/* Other Explorers */
	else if (document.body) {
		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; }
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
}


// Pauses code execution for specified time. Uses busy code, not good.
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

var lightbox = new Lightbox();

