var runslideshow = true;
var slideshowimages = {};
var slideshowdelay = 5000; //in milliseconds
var slideshowfadespeed = "fast"; //"slow", "normal", "fast", or number in milliseconds

//$(window).load(function(){
$(document).ready(function(){
	//preload the arrow images used for scrolling thumbnails
	var arrowLeft = new Image();
	arrowLeft.src = '../images/jquery.hoverscroll/arrow-left-small.png';
	var arrowRight = new Image();
	arrowRight.src = '../images/jquery.hoverscroll/arrow-right-small.png';
	//start the slideshow after a delay
	window.setTimeout("slideshow()", slideshowdelay);
	//set up the thumbnail click event
	$("#portrait_thumbnails a").click(function(){
		//stop the slideshow
		runslideshow = false;
		//show the slide just clicked on
		showslide(this);
		return false;
	});
	$("#portrait_thumbnails a img[original]").each(function(i){
		var img_src = $(this).attr("original");
		slideshowimages[img_src] = new Image();
		slideshowimages[img_src].src = img_src;
	});
	//setup the thumbnail scrolling (only if there's too many thumbnails to display)
	if(parseInt($('#portrait_thumbnails').width()) >= parseInt($('#portrait_thumbnails_container').width())) {
		$('#portrait_thumbnails')
			.css('position', 'relative') //change position to relative to get the scrolling to work
			.hoverscroll({
				vertical:false,
				width:$('div#portrait_thumbnails_container').width(),
				height:35,
				arrowsOpacity:0.8
			});
	}
});

//show the slide for the passed in <a>
function showslide(a){
	//check to make sure a has a value
	if(a == null || a === null || a.length == 0){
		return false;
	}
	//get the source for the large image to show
	var img_src = $(a).children("img").attr("original");
	//change the large image to the new image
	$("#portrait").attr("src", img_src);
	//de-select the old thumbnail
	$("#portrait_thumbnails li.selected").removeClass("selected");
	//select the new thumbnail
	$(a).parent("li").addClass("selected");
	/*//fade out the old image at specified speed
	$("#portrait").fadeOut(slideshowfadespeed, function(){
		//when it's done fading, change the large image to the new image and fade it in
		$(this).attr("src", slideshowimages[img_src].src).fadeIn(slideshowfadespeed);
		//$(this).attr("src", img_src).fadeIn(slideshowfadespeed);
		//de-select the old thumbnail
		$("#portrait_thumbnails li.selected").removeClass("selected");
		//select the new thumbnail
		$(a).parent("li").addClass("selected");
	});*/
	return true;
}

//auto slideshow function
function slideshow(){
	//check to see if the slideshow should be running or not
	if(runslideshow) {
		//try to show the next slide in the list
		if(showslide(getnextslide())){
			//the next slide has been shown, wait for a delay before showing next slide automatically
			window.setTimeout("slideshow()", slideshowdelay);
		}
	}
}

//get the next slide in the list
function getnextslide(){
	//try to find the next slide
	var a = $("#portrait_thumbnails li.selected").next("li").children("a");
	//check to see if there is no next one (means we were on the last in the list)
	if(a.length == 0) {
		//go back to the first slide
		a = $("#portrait_thumbnails li:first a");
	}
	//return the next slide
	return a;
}

