//JQuery function that runs AFTER DOM loads, will attach functions
// to buttons on html page.
$(function() {

 });
 
var galleryPhotos=new Array();
var galleryCaptions=new Array();
var arrayCount = 0;
//If this == 0 then show is paused, == 1 then show is playing. 
var playPause = 0;
var slideShowInterval;
var currentPicture="";
var currentPictureCaption="";

//Function takes one parameter "myOperation" which does the following based on value:
// myOperation = "playPause" toggles start/stop of the slide show
// myOperation = "next" displays next photo in set
// myOperation = "back" displays previous photo in set
function detailGallery(myOperation) {

	//Evaluate what the user is trying to do
	switch (myOperation) {
		case "playPause": {
			if (playPause == 0) {
				slideShowInterval = setInterval(nextPicture,7000);
				playPause = 1;
			} else {
				clearInterval(slideShowInterval);
				playPause = 0;
			}
			break;
		}
		case "next": {
			clearInterval(slideShowInterval);
			playPause = 0;
			nextPicture();
			break;
		}
		case "back": {
			clearInterval(slideShowInterval);
			playPause = 0;
			backPicture();
			break;
		}
	}
	
	//Transitions to next picture
	function nextPicture() {
		arrayCount++;
		if ( arrayCount > (galleryPhotos.length-1) )
			arrayCount = 0;
		currentPicture = galleryPhotos[arrayCount];
		currentPictureCaption = galleryCaptions[arrayCount];

		$("img.galleryImagePreLoad").attr("src",galleryPhotos[arrayCount+1] );
					
		//Swap Image's src in a fadeout / fadein style
		$("img.galleryImage").fadeOut("slow",function(){
			$("img.galleryImage").attr( "src",  currentPicture );
			$("img.galleryImage").fadeIn("slow");
		});
                $("p.detailGalleryCaption").html( currentPictureCaption );
	} 

	//Transitions back a picture
	function backPicture() {
		arrayCount--;
		if (arrayCount < 0)
			arrayCount = (galleryPhotos.length-1);
		
		currentPicture = galleryPhotos[arrayCount];
		
		$("img.galleryImage").fadeOut("slow",function(){
			$("img.galleryImage").attr( "src",  currentPicture );
			$("img.galleryImage").fadeIn("slow");
		}); 
	}
}
