// JavaScript Document
// Slideshow jQuery plugin by Chris Bewick
// http://chrisbewick.com/blog/jquery/jquery-slideshow-plugin/ 

// Set up a slideshow object to contain all the data needed by this plugin
// `Set the default values for fade time and show time to this object
var slideshow = {
	'defaultFadeTime' : 800,
	'defaultShowTime' : 6000
};

(function($){
	$.fn.slideshow = function(userFadeTime, userShowTime){
		// Make an array of all images in the gallery
		images = this.children('img');
		
		// Hide all the images in the gallery except the first
		images.hide().eq(0).show();

		// Get the ID of the div containing the gallery
		galleryID = this[0].id;
		
		// If it has been set by the user get the custom fade time
		fadeTime = userFadeTime ? userFadeTime : slideshow['defaultFadeTime'];
		
		// If it has been set by the user get the custom show time
		showTime = userShowTime ? userShowTime : slideshow['defaultShowTime'];
		
		// Calculate how long it takes to fade an image in, display it and then fade it out
		delayTime = showTime+(2*fadeTime);

		// Get the number of images in this gallery
		gallerySize = images.length-1;
		
		// Set up a new object within our slideshow object to record all 
		// the images in this gallery and keep track of which image is 
		// currently displayed
		slideshow[galleryID] = {'images' : images, 'count' : 0}
		
		//Call the rotate function every 'delayTime' seconds for this gallery
		setInterval('newRotate("'+galleryID+'",'+gallerySize+','+fadeTime+')',delayTime)
	}
})(jQuery);

function newRotate(galleryID,gallerySize,fadeTime){
	// Fade out the current image then update the count variable for this gallery and fade in the next image
	$(slideshow[galleryID]['images'][slideshow[galleryID]['count']]).fadeOut(fadeTime, function(){
		if(slideshow[galleryID]['count']<gallerySize){slideshow[galleryID]['count']++;}else{slideshow[galleryID]['count']=0;}
		$(slideshow[galleryID]['images'][slideshow[galleryID]['count']]).fadeIn(fadeTime);																
	});
}

