// JavaScript Document

// create 2 arrays, 1 for imgPath, and 1 for imgCaption
var imgPathArray = new Array();
var imgCaptionArray = new Array();
var thumbHeight;
var currIndex;

// for each thumbnail, go ahead and get:
// a) the mainImage's src, and
// b) the thumbnail's caption for alt text.

// Then puts each variable into the associating arrays.
// Which value to fetch depends on the intIndex value.
$(document).ready(function(){
	//$(".galleryModule//ul//li//a//img").hide();
	//$(".galleryModule//ul//li//a//img").load(function(){$(this).fadeIn('fast')});
	
							   
	$(".galleryModule//ul//li").each(function(intIndex){
		var imgPath = $("a",this).attr("href");
		var imgCaption = $("img",this).attr("alt");
		
		imgPathArray[intIndex] = imgPath;
		imgCaptionArray[intIndex] = imgCaption;
	});	

	
	$('.galleryModule//li//a').click(function(){
		var getImgPath = $(this).attr("href");
		var getImgCaption = $("img",this).attr("alt");
		galleryEnlarge(getImgPath, getImgCaption);
		return false;
	});	
	
	$('.next').click(function(){
		goNext();
		return false;
	});	
	
	$('.back').click(function(){
		goBack();
		return false;
	});	
	
	$('.backToThumbs').click(function(){
		galleryReset();
		return false;
	});	
	
	thumbHeight = $(".galleryModule").height();
	
});


// imgPath = mainImage's SRC
// imgCaption = from thumbnail
function galleryEnlarge(imgPath, imgCaption){
	
	$('.loading').show();
	$('.galleryModule//ul').css({opacity: ".5"});
	$('.galleryEnlargedImg').css({opacity: ".5"});
		
	// sets the 2 img Paths to the mainImage.
	$('.loadingImg').attr({src: "" + imgPath + "" });
	
	var count;
	$(".loadingImg").load(function(){
		if(!count) {
			
			$('.galleryEnlargedImg').attr({ src: "" + imgPath + "", alt: "" + imgCaption + "" });

			
			// set the height of the gallery from the size of Image being shown
			var height = $(this).height();
			$(".galleryModule").css({height: ""+height+"px"});
			
			$('.galleryModule//ul').hide();
			$('.loading').hide();
			
			// once loaded, show the main Image
			$('.galleryEnlargedImg').css({opacity: "1"}).fadeIn('slow');
			
			// once its loaded, show the controls
			$(".galleryButtons").show();
			
			count = 1;
		}
		
	
	});
		
	// but if reached to end of gallery, reset.
	if($(".loadingImg").attr("src") == 'undefined'){
		galleryReset();
	}
	return true;
};

function galleryReset() {
	$(".galleryModule").css({height: ""}); //resets the height.
	$(".loading").hide();
	$(".galleryEnlargedImg").hide(); // fade?
	$(".galleryButtons").hide();
	$(".galleryModule//ul").css({opacity: "1"}).fadeIn('slow');
};

function goNext(){
	getGalleryIndex();
	var nextMainImagePath = imgPathArray[currIndex + 1];
	var nextMainImageCaption = imgCaptionArray[currIndex + 1];
	galleryEnlarge(nextMainImagePath, nextMainImageCaption);
}

function goBack(){
	getGalleryIndex();
	var prevMainImagePath = imgPathArray[currIndex - 1];
	var prevMainImageCaption = imgCaptionArray[currIndex - 1];
	galleryEnlarge(prevMainImagePath, prevMainImageCaption);
}

function getGalleryIndex(){
	var currMainImageSRC = $(".galleryEnlargedImg").attr("src");
	
	// loop through imgPathArray and find the array Index that matches currMainImageSRC
	for(i=0;i<imgPathArray.length;i++){
		if(imgPathArray[i] == currMainImageSRC){
			currIndex = i;
		}
	}
	return true;
}