$(document).ready(function() {		
	
	//Execute the slideShow, set at 4 seconds for each images initially
	slideShow(6000);

});

function slideShow(speed) {

	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-option-2" class="caption"><div class="slideshow-caption-1"><p></p></div><div class="slideshow-caption-2"><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//show both of the captions since they inherit opacity of 0
	$('ul.slideshow #slideshow-option-2').css({opacity: 1.0});
	
	//Get the captions of the first image from TITLE and ALT attributes and display it
	$('.slideshow-caption-1 p').html($('ul.slideshow a:first').find('img').attr('title'));
	$('.slideshow-caption-2 p').html($('ul.slideshow a:first').find('img').attr('alt'));
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}

function gallery() {
	
	//you can set animation times here
	var slidOutSpeed = 300; 	// the speed at which the captions slide out
	var slidInSpeed  = 500; 	// the speed at which the captions slide in
	

	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow (the last <li> which is not an image), 
	//then rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-option-2')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image captions
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
		
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	//Hide the captions first, and then set and display the new captions
	//.slideshow-caption-1 = title
	//.slideshow-caption-2 = alt
	$('.slideshow-caption-1').animate({left:0}, slidOutSpeed);
	$('.slideshow-caption-2').animate({right:-200}, slidOutSpeed, function () {
			//Display the new content for next frame
			$('.slideshow-caption-1 p').html(title);
			$('.slideshow-caption-2 p').html(desc);				
			$('.slideshow-caption-1').animate({left:200}, slidInSpeed);	
			$('.slideshow-caption-2').animate({right:50}, slidInSpeed);
	});		

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}


