// Show new image
function showImage(imagePath) {
	$('highlightPhoto').onload = showImageWorker;
	$('highlightPhoto').src = imagePath;
	
	if (getPreviousImage()) {
		$('previous').removeClassName('hidden');
	} else {
		$('previous').addClassName('hidden');
	}

	if (getNextImage()) {
		$('next').removeClassName('hidden');
	} else {
		$('next').addClassName('hidden');
	}

}

function showImageWorker() {
		Effect.Appear('highlightPhoto',{duration:.25});
}

// Show new caption
function showCaption(caption) {
	$('caption').innerHTML = caption;
	setTimeout("Effect.Appear('caption',{duration:.25})",500);
}

// Hides the current photo and shows the new one
function showHighlight(imagePath,caption) {
	// Hides the current photo
	Effect.Fade('highlightPhoto',{duration: .25});

	// Hide current caption
	Effect.Fade('caption',{duration: .25});

	// Call function to show photo in .25 seconds
	setTimeout("showImage('"+imagePath+"')",250);


	// If there is a caption, call function to show it in .25 seconds
	if (caption.trim() != '') setTimeout("showCaption('"+caption+"')",250);
}

// Get the image before the one that is currently highlighted
function getPreviousImage() {
	newImage = '';
	var tmpImage = '';
	galleryPhotos.each(
		function(photoLink) {
			if (photoLink == $('highlightPhoto').src) {
				newImage = tmpImage;
			}
			tmpImage = photoLink;
		});

	return newImage;
}

// Get the image after the one that is currently highlighted
function getNextImage() {
	newImage = '';
	var getNext = false;
	galleryPhotos.each(
		function(photoLink) {
			if (getNext) {
				newImage = photoLink;
				getNext = false;
			}
			if (photoLink == $('highlightPhoto').src) {
				getNext = true;
			}
		});

	return newImage;
}

// Get the caption from a given identifier
function getCaption(href) {
	galleryPhotos.find(
		function(photoLink,index) {
			if (photoLink == href) imageId = index + 1;
		});

	return($('image'+imageId).title);
}

// Moves to a new highlighted image
function changeHighlight(direction) {

	// Find the previous image
	if (direction == 'previous') {
		newImage = getPreviousImage();
	// .. or find the next image
	} else if (direction == 'next') {
		newImage = getNextImage();
	}

	// Get the caption
	tmpCaption = getCaption(newImage);

	// Show the new image
	showHighlight(newImage,tmpCaption);

	// Highlight the new image thumbnail
	selectMe(newImage);
}

// Deselects all currently selected thumbs, then sets this thumb to selected
function selectMe(e) {
	$('galleryPhotos').select('[class~=selected]').each(
		function(photoLink) {
			photoLink.removeClassName('selected');
		});
	$(e).addClassName('selected');
}

function imagesLoaded() {
	alert('images loaded');
}

preloader = new Array();
function preloadImages() {

	//preloader.onLoad = imagesLoaded();
	galleryPhotos.each(
		function(photoLink) {
			foo = new Image();
			foo.src = photoLink.href;
			preloader.push(foo);
		});

	checkPreload();
}

function checkPreload() {
	var msg = "";
	var ready = true;
	var totalImages = preloader.length;
	var imagesReady = 0;
	preloader.each(
		function(img) {
			if (img.complete) { imagesReady++; }
		});

	if (imagesReady == totalImages) { $('debug').innerHTML = $('debug').innerHTML + "<br/>READY!"; }
	else {
		$('debug').innerHTML = "Loading image " + imagesReady + " of " + totalImages;
		setTimeout(checkPreload,50);
	}
}

// Stores photo thumbnail links for easy access
var galleryPhotos = new Array();
function getGalleryPhotos() {
	$('galleryPhotos').select('a:not([class~=foo])').each(
		function(photoLink) {
			galleryPhotos.push(photoLink);
		});
}

function loadGallery() {
	getGalleryPhotos();
	//preloadImages();
}

addLoadEvent(loadGallery);