
jQuery(document).ready(function() {		

	// Generate slideshow links if number of slides > 1
	if (jQuery('#gallery div.slide').length > 1) {
		jQuery('#gallery div.slide').each(function(i, item) {
	 		jQuery('#slidelinks').append('<a href="" id="slidelink' + i + '">' + (i+1) + '</a> ');
		});	
		jQuery('#playerbuttons').show();
	}
	
	//Execute the slideShow
	var slideshow = new slideShow();
	
	// Attach events
	jQuery('#slidelinks a').click(
		function() {
			var idx = jQuery(this).parent().children().index(this);
			slideshow.jumpToSlide(idx);
			return false;
		}
	);
});

function slideShow() {

	// Slideshow interval
	var interval = 5000;

	//Set the opacity of all images to 0
	jQuery('#gallery div.slide').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	jQuery('#gallery div.slide:first').css({opacity: 1.0});
	
	//Get the caption of the first image 
	jQuery('#gallery .caption').html( jQuery('#gallery div.slide:first').find('.slidecaption').html() );
	
	var isPlaying = true;
	var currentSlide = 1; // start slideshow by moving to the second slide (index = 1)

	jQuery('#slidelinks a:first').addClass('on');	
	jQuery('#doplay').addClass('on'); // show 	
	// start slideshow
	var iTimerID = setInterval(function(){showSlide()},interval);
		
	
	var play = function() 
	{
		// show next image right away
		showSlide(); 
		// restart the interval, otherwise if the function is called at the end of the interval two images will appear one after another.
		clearInterval(iTimerID);
		// Start playing images with an interval. If slideshow was paused then don't play, in pause mode the user manually controls next/prev.
		if (isPlaying) iTimerID = setInterval(function(){showSlide()},interval);
	}
	

	
	jQuery('#doplay').click(function() 
	{
			isPlaying = true;
			jQuery(this).addClass('on');
			isPlaying = true;
			jQuery(this).hide(); 
			jQuery('#dopause').show(); 
			play();
	});
	
	jQuery('#dopause').click(function() 
	{
		isPlaying = false;
		clearInterval(iTimerID); // stop playing the slideshow.
		jQuery(this).hide(); 
		jQuery('#doplay').show(); 
	});
	 
	
	// Show a speific slide by index
	this.jumpToSlide = function(idx)
	{
		if (isPlaying) jQuery('#dopause').trigger('click');
		currentSlide = idx;
		showSlide();
	}


	function showSlide() {
		// Check if next slide is within the array
		if (currentSlide >= jQuery('#gallery div.slide').length)
			currentSlide = 0;

		// if no show class, grab the first div	
		var current = (jQuery('#gallery div.show') ?  jQuery('#gallery div.show') : jQuery('#gallery div.slide:first'));
		
		// if current link is clicked again do nothing
		var currentIndex = current.parent().children().index(current);
		
		if (currentIndex == currentSlide)
			return;
		
		var next = jQuery('#gallery div.slide:eq(' + currentSlide +')');
		
		// Get next image caption
		var caption = next.find('.slidecaption').html();	
	
		//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}, 700);
	
		//Hide the current image
		current.animate({opacity: 0.0}, 700).removeClass('show');
		
		jQuery('#gallery .caption').html(caption);
				
		jQuery('#slidelinks a').removeClass('on');
		jQuery('#slidelinks a:eq(' + currentSlide + ')').addClass('on');
		
		currentSlide++;
	}	
	
}

