(function($){
	
	var settings = {
		blocs: null,
		blocs_container_selector: 'ul:first',
		current: 0,
		transition: 500,
		slideDuration: 4000
	};
	
	var methods = {
		
		init: function(opts) {
			return this.each(function() {
				var $this = $(this);

				var o = $.extend({}, settings, opts );
				o.blocs = (!o.blocs) ? $this.find('ul:first>li') : o.blocs;
				if (o.blocs.length > 0) {
					$this.data('simpleslideshow', {
						opts: o,
						_time: 0,
						pause: false
					})
					.hover(
						$.proxy(methods.stop, $this),
						$.proxy(methods.start, $this)
					)
					.simpleslideshow('start');
				}
			});
		},
		
		destroy: function() {
			return this.each(function(){
				var $this = $(this);
				$this.unbind('hover');
				$this.removeData('simpleslideshow');
			});
		},
		
		slideTo: function(index) {
			var datas = this.data('simpleslideshow');
			var slide = $(datas.opts.blocs.get(index));
			var c = this.find(datas.opts.blocs_container_selector);
			c.stop().animate({
				'opacity': 0
			}, datas.opts.transition, function () {
				$(this).css('top', '-'+slide.position().top+'px');
				$(this).animate({
					'opacity': 1
				});
			});
			datas.opts.current = index;			

			return this.simpleslideshow('auto');
		},
		
		next: function() {
			var datas = this.data('simpleslideshow');
			var index = ((datas.opts.current+1) >= datas.opts.blocs.length ) ? 0 : datas.opts.current+1;
			return this.simpleslideshow('slideTo', index);
		},

		auto : function() {
			var datas = this.data('simpleslideshow');
			if (!datas.pause) {
				clearTimeout(datas._time);
				datas._time = setTimeout(
					$.proxy(methods.next, this),
					datas.opts.slideDuration
				);
			}
			return this;
		},

		start : function () {
			var datas = this.data('simpleslideshow');
			datas.pause = false;
			return this.simpleslideshow('auto');
		},

		stop : function() {
			var datas = this.data('simpleslideshow');
			datas.pause = true;
			clearTimeout(datas._time);
			return this;
		}
	};
	
	
	$.fn.simpleslideshow = function(method) {
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on $.simpleslideshow' );
		}
	};
	
})(jQuery);
