(function($) {

$.fn.hoverscrolled = function(params) {
	var opts = {
		arrows: false
	};
	
	$.extend(opts, params);
	
	this.each(function() {
		var $this = $(this);
		$this.find('li').append('<i class="b-sep"></i>');
		var ctnr = $this.parent('.jcarousel-clip');
		var size = ctnr.outerWidth(true);
		
		var zone = {
			1: { action: 'move', from: 0, to: 0.06 * size, direction: -1 , speed: 32 },
			2: { action: 'move', from: 0.06 * size, to: 0.15 * size, direction: -1 , speed: 8 },
			3: { action: 'move', from: 0.15 * size, to: 0.25 * size, direction: -1 , speed: 4 },
			4: { action: 'move', from: 0.25 * size, to: 0.47 * size, direction: -1 , speed: 2 },
			5: { action: 'stop', from: 0.47 * size, to: 0.53 * size },
			6: { action: 'move', from: 0.53 * size, to: 0.75 * size, direction: 1 , speed: 2 },
			7: { action: 'move', from: 0.75 * size, to: 0.85 * size, direction: 1 , speed: 4 },
			8: { action: 'move', from: 0.85 * size, to: 0.94 * size, direction: 1 , speed: 8 },
			9: { action: 'move', from: 0.94 * size, to: size, direction: 1 , speed:32 }
		}
		
		if (opts.arrows) {
			ctnr.append('<span class="jcarousel-arrow jcarousel-arrow-prev"><b></b><i></i></span><span class="jcarousel-arrow jcarousel-arrow-next"><b></b><i></i></span>');
			var arrow_prev = ctnr.find('.jcarousel-arrow-prev');
			var arrow_next = ctnr.find('.jcarousel-arrow-next');
		}
		
		$this.wrap($('<div />').attr('class', 'jcarousel-wp'));
		ctnr = $this.parent('.jcarousel-wp');
		
		function checkArrows() {
			if (!opts.arrows) {
				return false;
			}
			
			var maxScroll = ctnr.attr('scrollWidth') - ctnr.width();
			var scroll = ctnr.scrollLeft();
			var opacity = (scroll / maxScroll);
			var limit = 1;
			
			if (isNaN(opacity)) { opacity = 0; }
			
			var done = false;
			if (opacity <= 0) { arrow_prev.hide(); }
			if (opacity >= limit || maxScroll <= 0) { arrow_next.hide(); done = true; }
			
			if (!done) {
				arrow_prev.show().css('opacity', (opacity > limit ? limit : opacity));
				arrow_next.show().css('opacity', (1 - opacity > limit ? limit : 1 - opacity));
			}
		}
		
		function checkMouse(x, y) {
			var pos = x - ctnr.offset().left
			for (i in zone) {
				if (pos >= zone[i].from && pos < zone[i].to) {
					if (zone[i].action == 'move') { startMoving(zone[i].direction, zone[i].speed); }
					else { stopMoving(); }
				}
			}
		}
		
		function startMoving(direction, speed) {
			if (ctnr.data('speed') != speed) {
				ctnr.data('speed', speed);
			}
			if (ctnr.data('direction') != direction) {
				stopMoving();
				ctnr.data('direction', direction);
				ctnr.data('isChanging', true);
				move();
			}
		}
		
		function stopMoving() {
			if (ctnr.data('isChanging')) {
				ctnr.data('isChanging', false);
				ctnr.data('direction', 0);
				ctnr.data('speed', 1);
				clearTimeout(ctnr.data('timer'));
			}
		}
		
		function move() {
			if (ctnr.data('isChanging') == false) { return; }
			ctnr.scrollLeft(ctnr.scrollLeft() + ctnr.data('direction') * ctnr.data('speed'));
			ctnr.data('timer', setTimeout(function() { move(); }, 50));
			checkArrows();
		}
		
		ctnr.parent('.jcarousel-clip').mousemove(function(e) { checkMouse(e.pageX, e.pageY); }).bind('mouseleave', function() { stopMoving(); });
		checkArrows();
		
	});
	
	return this;
};

})(jQuery);


