(function($) {
	jQuery.fn.attrAutoScroller = function(options) {
		var defaults = {
			time: 20,
			dir: 'top',
			interval: 1
		};
		
		var opts = $.extend(defaults, options);
		
		function createAutoScroller() {
			var zoneScrollable = $('<div class="zoneScrollable"></div>');
			
			var donneesBloc = $(this).html();
			
			$(this).empty();
			
			zoneScrollable.append(donneesBloc);
			
			$(this).append(zoneScrollable);
			
			$(this).css('position', 'relative');
			$(this).css('overflow', 'hidden');
			zoneScrollable.css('position', 'absolute');
			
			var heightBloc = parseInt($(this).css('height'));
			if (!heightBloc) heightBloc = parseInt($(this).innerHeight());
			var heightZone = parseInt(zoneScrollable.css('height'));
			if (!heightZone) heightZone = parseInt(zoneScrollable.innerHeight());
			
			
			var widthBloc = parseInt($(this).css('width'));
			if (!widthBloc) widthBloc = parseInt($(this).innerWidth());
			var widthZone = parseInt(zoneScrollable.css('width'));
			if (!widthZone) widthZone = parseInt(zoneScrollable.innerWidth());
			
			function autoScroll() {
				switch (opts.dir) {
					case 'top':
						var top = parseInt(zoneScrollable.css('top'));
						
						if ((heightZone + top) >= 0) {
							var newValue = top - opts.interval;
							
							zoneScrollable.css('top', newValue + 'px');
						}
						else { 
							zoneScrollable.css('top', heightBloc + 'px');
						}
						break;
					
					case 'bottom':
						var top = parseInt(zoneScrollable.css('top'));
						
						if (top <= heightBloc) {
							var newValue = top + opts.interval;
							
							zoneScrollable.css('top', newValue + 'px');
						}
						else {
							zoneScrollable.css('top', (-heightZone) + 'px');
						}
						break;
					
					case 'left':
						var left = parseInt(zoneScrollable.css('left'));
						
						if ((widthZone + left) >= 0) {
							var newValue = left - opts.interval;
							
							zoneScrollable.css('left', newValue + 'px');
						}
						else {
							zoneScrollable.css('left', widthBloc + 'px');
						}
						break;
					
					case 'right':
						var left = parseInt(zoneScrollable.css('left'));
						
						if (left <= widthBloc) {
							var newValue = left + opts.interval;
							
							zoneScrollable.css('left', newValue + 'px');
						}
						else {
							zoneScrollable.css('left', (-widthZone) + 'px');
						}
						break;
				}
			}
			
			var idInterval = setInterval(autoScroll, opts.time);						
			
			$(this).hover(function() {
				clearInterval(idInterval);
			}, function() {
				idInterval = setInterval(autoScroll, opts.time);
			});
		}
			
		return this.each(createAutoScroller);
	};
})(jQuery);
