/* Plugin by Rein  Van Oyen - January 2011 */
(function($){
	
	$.fn.tntSlideshow = function(method){

		/* var scope			= {playInterval: false} */
		var elementStack 	= this; /* Object returning the element on which the code should run */		
		var imgStack		= elementStack.find('img'); /* Object returning all images */
		
		/* METHODS */
		
		var methods = {
			
			init : function(){

				imgStack.hide().first().fadeIn(settings.speed); /* Hide all images and fade in the first */

				if(settings.autoPlay){ /* If the plugin is set to autoplay */
					methods.play(); /* Call the method play */
				}
				
			},
			play : function(){
				if(elementStack.length>0){ /* If the element exists */
					playInterval = setInterval(function(){
						if(settings.random){ /* If random is true */
							var imgStackSize 	= imgStack.size(); /* Returning the amount of images */
							var nextImg 		= Math.floor(Math.random()*imgStackSize); /* Set the nextImg to a random number */
						}
						else /* If random is false */
						{
							var visibleImg 	= elementStack.children('img:visible'); /* Object returning the visible image */
							var currentImg 	= imgStack.index(visibleImg); /* Number of the visible image */
							var nextImg		= currentImg+1; /* Set the nextImg to the next number*/
						}
						methods.fadeTo(nextImg); /* Pass the number for the next image to the fadeTo function */
					}, settings.interval);
				}
			},
			pause : function(){
				clearInterval(playInterval); /* Clear the autoplay interval */
			},
			fadeTo : function(n){

				var visibleImg 	= elementStack.children('img:visible'); /* Object returning the visible image */
				var visibleImgI	= imgStack.index(visibleImg); /* Number of the visible image */
				var fadeToImg	= imgStack.eq(n); /* Object returning the image to fade to */
			
				if(fadeToImg.size()>0){ /* If the image to fade to exists */
					
					if(visibleImgI>n){
						fadeToImg.show();
						visibleImg.fadeOut(settings.speed);
					}
					else if(visibleImgI<n)
					{
						fadeToImg.fadeIn(settings.speed, function(){
							visibleImg.hide();
						});
					}
					else
					{
						methods.fadeTo(n+1);
					}

				}
				else /* If the image to fade to does not exist, most probably when the slideshow is at the end */
				{
					methods.fadeTo(0); /* Fade to the first image */
				}
				
			}
			
		};
		
		/* DEFAULT SETTINGS */
		
		var settings = {
			'speed': 1000, /* The speed of a fade transition */
			'interval': 2000, /* The time between the transitions */
			'random': false, /* Random or linear? */
			'autoPlay': true /* Autoplay? */
		};
		
		/* CALL METHOD OR MERGE CUSTOM SETTINGS WITH DEFAULT SETTINGS & INIT */

		if ( methods[method] ) { /* It's an existing method */
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); /* Call the requested method */
		}
		else if ( typeof method === 'object' || ! method ) /* It's an object */
		{
			return this.each(function(){
				$.extend(settings, method); /* Extend the default settings with the custom settings */
				methods.init(); /* Call the init method */
			});
		} /* The method does not exist and it's not an object */
		else
		{
			alert('Er is een probleem: rein@tnt.be');
		}
		
	};

})( jQuery );
