/**
 * jQuery plugin: FadeGallery (minified edition)
 * http://www.exalted-web.com/FadeGallery
 *
 * Written by Shay from http://www.exalted-web.com
 * Release date: 29/08/2009
 * Version: 0.1
 *
 * Licensed under the GNU GPL license.
 * http://www.gnu.org/licenses/gpl.txt
 */
(function($){
	$.fn.gallery = function(options) {
		
		// Settings
		var defaults = {
			FadeIn:500,
			FadeOut:500,
			Delay:10000,
			Repeat:true
		};
		var options = $.extend(defaults, options);
		
		// The function
		return this.each(function() {
			// Set element to work with
			var elements;
			elements = $(this).children().filter(".galItem");
			// Set showing varible
			var showing;
			showing = 0;
			
			// Hide all Elements
			elements.hide();
			
			// Call fade function
			Fade();
			
			// Fade function does the real thing
			function Fade() {
				var hide;
				
				// Check where are we in the loop and if set to repeat
				if(options.Repeat && showing >= elements.length) {
					showing = 0;
				}
				
				// If 0 hide the last element
				if(showing == 0) {
					hide = elements.length-1;
				}
				// Else hide the showen element -1
				else {
					hide = showing-1;
				}
				
				// Fade out the hide element
				elements.eq(hide).fadeOut(options.FadeOut,function callback(){
					// Fade in the in element
					elements.eq(showing).fadeIn(options.FadeIn,function callback(){
						// Increase the showing varible
						showing++;
						// If set to repeat or if not showing the last element repeat the function
						if(options.Repeat || showing < elements.length)
							setTimeout(Fade,options.Delay);
					});
				});
				
			}
		});
	};
	
})(jQuery);