/*
 *	jQuery light lightslideshow 1.0.0 - Another simple slideshow
 * 
 *	Requires : jquery-1.3.2.min.js
 *
 *	Params
 *	--------------------
 *	defaultIndex : Choose which slide to start on
 *	auto : True to run the auto slideshow, false to disable it
 *	autoTimeout : Auto scroll duration (ms : less is faster). Must be superior to the fxSpeed.
 *	fxSpeed : Fade effect duration (ms)
 *
 */

(function(jQuery){
	// jQuery plugin
	jQuery.fn.lightslideshow = function(params) {

		// Default params
        var defaults = {
			defaultIndex : 0,
			auto : true,
			autoTimeout : 5000,
            fxSpeed: 700
        };

		// Merge with user params
		options = jQuery.extend(defaults, params);

		return this.each(function(){
			var slideshow = jQuery(this);
			var slideshowItems = slideshow.find("li");
			var slideshowSize = slideshowItems.length-1;
			var slideshowPanelCurrentId = 0;
			var slideshowAutoPlay = null;
			var slideshowFirstTime = true;
			var slideshowActiveClassName = "widget-lightslideshow-panel-active";
			var slideshowOldClassName = "widget-lightslideshow-panel-old";
			//var autoTimeout = 5000;

			// Display first item 
			slideshowSwitch(options.defaultIndex, slideshowFirstTime);
			
			// AutoScrolling starter
			if(options.auto){			
				slideshowStartAutoScroll();
				
				/* Stop autoScrolling when reading content */
				slideshow.hover(
					function(){slideshowStopAutoScroll();},
					function(){slideshowStartAutoScroll();}
				);
			}
			
			// Switches to the current item
			function slideshowSwitch(slideshowPanelId, slideshowFirstTime){
				
				// Set auto current image id for autoplay
				slideshowCurrentPanel = slideshowItems.eq(slideshowPanelId);
				slideshowCurrentPanel.css({"display":"none"});
				
				if(!slideshowFirstTime){				
					// Hiding visible panels
					slideshow.find("."+slideshowOldClassName).removeClass(slideshowOldClassName);							
					slideshow.find("."+slideshowActiveClassName).removeClass(slideshowActiveClassName).addClass(slideshowOldClassName);
					
					// Showing current (smooth effect)								
					slideshowCurrentPanel.addClass(slideshowActiveClassName).fadeIn(options.fxSpeed);
				}
				else{
					slideshowCurrentPanel.addClass(slideshowActiveClassName).css({"display":"block"});
					slideshowFirstTime = true;
				}
			};

			// AutoScrolling handler
			function slideshowAutoScroll(){

				// If last panel /or not
				slideshowPanelCurrentId = (slideshowPanelCurrentId == slideshowSize) ? 0 : slideshowPanelCurrentId+1;
				
				// Switch to the next panel
				slideshowSwitch(slideshowPanelCurrentId, false);
			};
			
			// Start autoScrolling
			function slideshowStartAutoScroll(){
				slideshowAutoPlay = setInterval(slideshowAutoScroll, options.autoTimeout);
			};
			
			// Stop autoScrolling
			function slideshowStopAutoScroll(){
				clearTimeout(slideshowAutoPlay);
				slideshowAutoPlay = null;
			};
		});
    };
})(jQuery);

