// Created by Nicola Raffaele Di Matteo


(function($) { 
	$.fn.extend({ 
		scroller: function(options) {
			var time = 2000;
			var position = 0;
			var contents;
			var slideWidth = 400; // default, overwritten from the css size.
			var slideNumber;
			var loop = true; // If true you will have continue scroll, with false restart from the first slide
			
			slideNumber = jQuery('.slide').length;
			slideWidth = +$('.slide').css('width').split('px')[0];
			$('#slides').css('width', slideWidth*slideNumber);
			setTimeout(move, time);

			function move() {
				if (position == slideWidth * (slideNumber - 1)) {
					position = 0;					
					// move in the first position the last item then reset the slide to the initial point
					if (loop) {
						var last = $('.slide')[slideNumber - 1];
						$('.slide').splice(0, 1);	// remove the first item	
						$('#slides').prepend(last); // the first now is the last
						$('#slides').animate({'marginLeft' : position}, 0);
					}	
				} else
					position += slideWidth;
				$('#slides').animate({'marginLeft' : -position});
				setTimeout(move, time);
			}
		}	

  });
})(jQuery);


