/**
 * @author Ludovic Tarit
 * 
 */
(function($) {
	$.fn.Slide = function(settings) {
		var config = {};
		settings = $.extend(true,config,settings);
		
		var plugin = new Slide($(this),settings);
		plugin.init();
	};
	
	$.Slide = function(elem,settings) {
		return $(elem).Slide(settings);
	};
	
	Slide = function(puces,settings)
	{
		settings.durationShow 		= settings.durationShow == undefined ? 500 : settings.durationShow;
		settings.durationHide 		= settings.durationHide == undefined ? 500 : settings.durationHide;
		settings.classnameSlide		= settings.classnameSlide == undefined ? 'slide' : settings.classnameSlide;
		settings.time				= settings.time == undefined ? 2000 : settings.time;
		
		this.settings = settings;
		
		this.puces 			= puces;
		this.index			= 0;
		this.slides			= $('.'+this.settings.classnameSlide);
		this.timer 			= null;
		this.stop 			= false;
	};
	
	Slide.prototype.init = function()
	{
		var that = this;
		
		$(this.puces).addClass('off');
		$(this.puces[0]).removeClass('off').addClass('on');
		
		$(this.slides).hide();
		$(this.slides[0]).show();
		
		that.bindMouseEvents();
		that.show();
	};
	
	Slide.prototype.show = function(one)
	{
		var that = this;
		var n = that.slides.length;
		
		$(this.puces).removeClass('on').addClass('off');
		$(this.puces[that.index % n]).removeClass('off').addClass('on');
		
		$(that.slides).hide();
		$(that.slides[that.index % n]).fadeIn(that.settings.durationShow, function() {
			
		} );
		if( !that.stop && !one ) {
			that.timer = window.setTimeout(function() {that.next();}, that.settings.time);
		}
	};
	
	Slide.prototype.next = function()
	{
		var that = this;
		if( !that.stop ) {
			that.index++;
			that.show();
		}
		
	};
	
	Slide.prototype.bindMouseEvents = function()
	{
		var that = this;
		
		$(that.slides).bind('mouseover', function(e) {
			that.stop = false;
			window.clearTimeout(that.timer);
			that.timer = null;
		} ).bind('mouseout',function(e) {
			that.stop = false;
			that.index = $(e.currentTarget).prevAll().length;
			that.timer = window.setTimeout(function() {that.next();}, that.settings.time);
		} );
		
		$(that.puces).bind('mouseover', function(e) {
			that.index = $(e.currentTarget).prevAll().length;
			that.stop = true;
			window.clearTimeout(that.timer);
			that.timer = null;
			that.show(true);
		} ).bind('mouseout',function(e) {
			that.stop = false;
			that.index = $(e.currentTarget).prevAll().length;
			that.timer = window.setTimeout(function() {that.next();}, that.settings.time);
		} );
	}
	
} )(jQuery);





















