
var pauseTime = 3000; //time between slides
var fadeTime = 800; //length of the fade between slides

var currentIndex = 0;
var animating = false;
var nextAnimation = false;
var slideShowPlaying = false;
var slideShowTimeout = false;
var loaded = [];

function nextImage(slideShow){
	if(!slideShow){
			pauseSlideShow();
		}
	
	if(!animating){
		var next = currentIndex + 1;
		if(next == images.length) next = 0;
		animate(next);
	}else{
		nextAnimation = function(){
			nextImage();
		};
	}
}

function showCaption(caption, link){
	if(link){
		$("#slideshow_caption").html("<a href='" + link + "'>"  +  caption +  "</a>")
	}else{
		$("#slideshow_caption").html(caption)
	}
	
	$("#slideshow_caption").animate({opacity: 1.0}, 800);
}

function hideCaption(){
	$("#slideshow_caption").animate({opacity: 0.0}, 100);
}

function prevImage(){
	pauseSlideShow();
	
	if(!animating){
		var next = currentIndex - 1;
		if(next == -1) next = images.length - 1;;
		animate(next);
	}else{
		nextAnimation = function(){
			prevImage();
		};
	}
}

function jumpTo(index){
	if(!animating){
                animate(index);
        }else{
                nextAnimation = function(){
                        jumpTo(index);
                };
        }
}

function animate(nextIndex){
	nextIndex = parseInt(nextIndex);
	
	//console.log("going from " + currentIndex + " to " + nextIndex);
	
	hideCaption();
	
	$("#big_image_wrapper").css({background: "url(" + images[nextIndex].src + ")"});
	currentIndex = nextIndex;
	animating = true;
	
	
	$("#big_image").animate({
		opacity: 0
	}, fadeTime, function(){
		indexLabel = currentIndex + 1;
		$("#gallery_count").html(indexLabel + " of " + images.length);
		
		$("#big_image").attr('src', images[currentIndex].src);
		$("#big_image").css({opacity: 1.0});
		
		if(images[currentIndex].caption){
			showCaption(images[currentIndex].caption, images[currentIndex].link);
		}
		


		animating = false;
		if(nextAnimation){
			nextAnimation();
			nextAnimation = false;
		}else{
			if(slideShowPlaying) slideShowTimeout = setTimeout(function(){ nextImage(true); }, pauseTime);
		}
		
	});	
	
}

function playSlideShow(){
	slideShowPlaying = true;
	$("#play_button").hide();
	$("#pause_button").show();
	slideShowTimeout = setTimeout(function(){ nextImage(true); }, 3000);
}

function pauseSlideShow(){
        slideShowPlaying = false;
	if(slideShowTimeout) clearTimeout(slideShowTimeout);
	slideShowTimeout = false;
	$("#play_button").show();
	$("#pause_button").hide();
}

$(function () {
var index = 0;

	$.each(images, function(){
		$("#preload_wrapper").append("<img id='preload_" + index +  "' src='" + this.src + "'>");
        
		$("#preload_" + index).bind('load', function(){
			//id = this.id.replace(/^preload_/, '');
			//$("#thumb_"+id).animate({opacity: 1}, 500);
			//$("#thumb_" + id).css({cursor: 'pointer'});
			//$("#thumb_" + id).click(function(){
			//	jumpTo(this.id.replace(/^thumb_/, ''));
			//});
		
			loaded.push(this);
			if(loaded.length == images.length && currentIndex == 0){
				playSlideShow();
				//alert('loaded');
				if(images[0].caption){
					showCaption(images[0].caption, images[0].link);
				}
			
			}

		});     
		index++;
	});
	
});





