

// ************************
// START: Gallery Functions 
// ************************
// Current IMG Outer Div ID   = "galleryImageCurrent"
// Thumbnail IMG Outer Div ID = "galleryImageThumbnails"
// Current IMG Caption Div ID = "galleryImageCurrentCaption"
// Gallery Navigation DIV ID  = "galleryImageNav"
// Current IMG ID             = "currentImage"
// Thumbnail IMG ID           = "imgID-<IMAGE ID>"
// Thumbnail IMG LINK ID <a>  = "linkID-<IMAGE ID>"

var currentImageIndex   = 0;
var imageArray			= [];
var descArray			= [];
	
$(function(){

	$( '#galleryImagePicNavPrev' ).click( function() { imagePrev(); } );
	$( '#galleryImagePicNavNext' ).click( function() { imageNext(); } );			
			
	setTimeout( function() {
		imageHeight = $( 'img#currentImage' ).height();
		$( '#galleryImagePicNavPrev' ).height( imageHeight );
		$( '#galleryImagePicNavNext' ).height( imageHeight );
	}, 1100);

	// On page load get all the href locations for each thumbnail anchor.  This will be what 
	//   is used to advance pictures forward and backwards.
	$("#galleryImageThumbnails li a").each(
		function( loopCount ){
			imageArray[ loopCount ] = $(this).attr('href');
			descArray[ loopCount ] = $(this).attr('alt');
		}
	);

	// When we click on a thumbnail, this is the code that switches that thumbnail into main 
	//   image div.
	$("#galleryImageThumbnails li a").click(function(){
			hideThumbnails();
		
			var linkID          = $(this).attr('id');
			var imagePubsysID   = linkID.substr(7);
			var linkSRC 	    = $("#linkID-" + imagePubsysID).attr('href');
			var linkDescription = $("#linkID-" + imagePubsysID).attr('alt'); 
			
			$("#currentImage").attr('src',linkSRC);
			$("#galleryImageCurrentCaption p").html( linkDescription );
	
			return( false );
			//window.setTimeout( showCaption, 750 );
			//window.setTimeout( hideCaption, 3000 );
	});
	
	// This code toggles the hide and show of the caption
	$("#galleryInfo").toggle(
		function(e) {
			hideCaption();
		}, 
		function(e) {
			showCaption();
		}
	);


});

// Function that gets called when clicking the 'back' arrow on galleries
function imagePrev() {
	if( currentImageIndex <= 0 ) {
		currentImageIndex = imageArray.length - 1;
	} else {
		currentImageIndex--;
	}		
		
	$("#currentImage").attr('src', imageArray[currentImageIndex]);
	$("#galleryImageCurrentCaption p").html( descArray[currentImageIndex] );
	
	setTimeout( function() {
		imageHeight = $( 'img#currentImage' ).height();
		$( '#galleryImagePicNavPrev' ).height( imageHeight );
		$( '#galleryImagePicNavNext' ).height( imageHeight );
	}, 1100 );
		
}
// Function that gets called when clicking the 'forward' arrow on galleries
function imageNext() {
	currentImageIndex++;
	if( currentImageIndex >= imageArray.length )
		currentImageIndex = 0;
		
	$("#currentImage").attr('src', imageArray[currentImageIndex]);
	$("#galleryImageCurrentCaption p").html( descArray[currentImageIndex] );
	
	setTimeout( function() {
		imageHeight = $( 'img#currentImage' ).height();
		$( '#galleryImagePicNavPrev' ).height( imageHeight );
		$( '#galleryImagePicNavNext' ).height( imageHeight );
	}, 1100 );
}


function showThumbnails() {
	$("#galleryImageThumbnails").css( 'display', 'block' );
}
function hideThumbnails() {
	$("#galleryImageThumbnails").css( 'display', 'none' );
}
function showCaption() {
	$("#galleryImageCurrentCaption").slideDown(600);
}
function hideCaption() {
	$("#galleryImageCurrentCaption").slideUp(600);
}



// END: Gallery Functions
// **********************
