var slideShowRunning = true;
var slideShowTimer = 0;


function startSlideShow() {
//	console.log('starting slide show, delay: %d', slideShowInterval);

	//Set the opacity of all images to 0
	jQuery('.slide_show_image').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	jQuery('.slide_show_image:first').css({opacity: 1.0});
	
	//Call the gallery function to run the slideshow
	if (slideShowRunning) {
		slideShowTimer = setTimeout('slideShowNext()', slideShowInterval);
	}
	
}

function slideShowNext() {
	if (typeof(pauseAnimations) != "undefined" && pauseAnimations == true) {
		// do nowt 
	} else {
		var current, next;
		
		//if no IMGs have the show class, grab the first image
	//	var current = (jQuery('#slide_show_images img.show')?  jQuery('#slide_show_images img.show') : jQuery('.slide_show_image:first'));
		if (jQuery('.slide_show_image.show').length > 0) {
			current = jQuery('.slide_show_image.show');
		} else {
			current = jQuery('.slide_show_image:first');
		}
	
	//	console.log('next slide show element. current:');
	//	console.log(current);
	
		//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	//	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? jQuery('#gallery a:first') :current.next()) : jQuery('#gallery a:first'));
		
		if (current.next().length > 0) {
			next = current.next();
		} else {
			next = jQuery('.slide_show_image:first');
		}
	//	console.log('next slide show element. next:');
	//	console.log(next);
		
		//Set the fade in effect for the next image, show class has higher z-index
		next.css({opacity: 0.0});
		next.addClass('show');
		next.animate({opacity: 1.0}, 1000);
	
		//Hide the current image
		current.animate({opacity: 0.0}, 1000);
		current.removeClass('show');
	}
	
	//Call the gallery function to run the slideshow
	if (slideShowRunning) {
		slideShowTimer = setTimeout('slideShowNext()', slideShowInterval);
	}
	
}



function toggleSlideShow() {
	if (slideShowRunning) {
		clearTimeout(slideShowTimer);
		slideShowRunning = false;
		jQuery('#slide_show_button img').attr('src', '/i/shared/slide_show_play_button.png');
		jQuery('#slide_show_button img').attr('alt', 'Play');
	} else {
		slideShowRunning = true;
		jQuery('#slide_show_button img').attr('src', '/i/shared/slide_show_pause_button.png');
		jQuery('#slide_show_button img').attr('alt', 'Pause');
		slideShowTimer = setTimeout('slideShowNext()', 0);		
	}
	
}


