(function($){
	$.fn.carrousel = function(options){
		var options = $.extend({}, options);
		return $(this).each(function(){

			var container = $(this);
			var wrapper = $('<div></div>').css('margin-left', container.width());
			container.children().detach().appendTo(wrapper);
			wrapper.appendTo(container);
			var $images = container.find('img');
			var imgWidth = 0;
			var notLoaded = $images.length;
			$images.each(function(){
				if (this.complete){
					imageLoaded($(this));
				}
				else{
					$(this).load(function(){
						imageLoaded($(this));
					});
				}
			});
			$images.css({
				float:'left'
				, margin:0
				, padding:0
			});
			$images.clone().appendTo(wrapper);
		
			function scroll(){
				wrapper.animate({
					marginLeft: -imgWidth.toString() + 'px'
				}, {
					duration: 40000
					, easing: 'linear'
					, complete: function(){
						wrapper.css('margin-left', 0);
						scroll();
					}
				});
			}
			
			function imagesLoaded(){
				wrapper.css({
					width: imgWidth * 2
					, overflow: 'hidden'
				});
				// window.console.log(imgWidth); window.console.log(imgHeight);
				scroll();
			}
			
			function imageLoaded($image){
				imgWidth = imgWidth + $image.width();
				notLoaded = notLoaded - 1;
				if (notLoaded == 0){
					imagesLoaded();
				}
			}
		});
	};
})(jQuery);
