(function($) {
	$.fn.autoslider = function(options) {
	
		var calcSpeed = function(currentPos, wantedPos, pxPerSecond) {
			
			if(currentPos == null) {
				currentPos = 0;
			}
			var abs = Math.abs(((parseInt(currentPos, 10) - parseInt(wantedPos, 10)) / pxPerSecond) * 1000);
			return abs;
		}
		
		var findPosInArr = function(number, arr) {
			for(var i = 0; i < arr.length; i++) {
				if(i == 0) {
					if(number >= 0 && number < arr[i]) {
						return i+1;
					}
				}
				else {
					if(number >= arr[i-1] && number < arr[i]) {
						return i+1;
					}
				}
			}
			
			return -1;
		}
		
		var settings = $.extend({}, $.fn.autoslider.defaults, options);

		return this.each(function() {
			var $table = $(this).css('position', 'relative');
			if($table.width() > settings.containerWidth) {
				
				$container = $table.parent();
				
				var mouseX;
				var containerX = $container.offset().left;
				var containerWidth = settings.containerWidth; //$container.width(); (marche pas a cause de IE6)
				if(!$table.css('left') || $table.css('left') == 'auto') {
					$table.css('left', '0px');
				}

				var tableWidth = $table.width();
				
				var slideWidth = settings.slideWidth;
				
				var minLeft = -(tableWidth - containerWidth);
				var maxLeft = 0;
				
				// divide container into 3rds
				var oneDivision = containerWidth / 30;
				var divisions = [];
				
				for(var i = 0; i < 30; i++) {
					if(i < 15) {
						divisions[i] = Math.floor(oneDivision * (i+1));
					}
					else {
						divisions[i] = Math.ceil(oneDivision * (i+1));
					}
				}
				
				var currentPos = false;
				var newPos = false;
				var coef = 1;
				var tmpcoef = 1;
				var allowMove = true;
				
				$('div.popup').bind('mouseenter', function() {
					$table.stop();
					allowMove = false;
				}).bind('mouseleave', function() {
					allowMove = true;
				});
				
				$container.bind('mousemove', function(e) {
					if(allowMove) {
						mouseX = e.pageX - containerX;
						
						newPos = findPosInArr(mouseX, divisions);
						
						if(newPos !== currentPos) {
							currentPos = newPos;
							
							if(currentPos < 11) {
								//go left
								
								coef = (10 - currentPos) + 1;

								var speed = calcSpeed($table.css('left'), maxLeft, slideWidth * coef);

								$table.stop().animate({
									'left': maxLeft + 'px'
								}, speed, 'linear');
							}
							else if(currentPos > 20) {
								// go right
								
								tmpcoef = (30 - currentPos) + 1;
								coef = (10 - tmpcoef) + 1;
								
								$table.stop().animate({
									'left': minLeft + 'px'
								}, calcSpeed($table.css('left'), minLeft, slideWidth * coef), 'linear');
							}
							else {
								// stop
								$table.stop();
							}
							
							
						}
					}
					
				});
				
				$container.bind('mouseleave', function(e) {
					$table.stop();
				});
			
			}
		});
		
	};
	
	$.fn.autoslider.defaults = {
		'containerWidth': 591,
		'slideWidth': 104
	};
})(jQuery);
