;(function( $ ){

	var $simpleScroll = $.simpleScroll = function( settings ){
		return $(window).simpleScroll( settings );
	};
	$simpleScroll.defaults = {
	
	};
	
	$.fn.simpleScroll = function( options ){
		return this.each(function(){
			var
				settings = $.extend( {}, $simpleScroll.defaults, options ),
				$pane = $(settings.target),
				direction = "right",
				blocksize = parseInt($(settings.items).css('width')),
				auto = true,
				pos = 0,
				dest = -1*(parseInt($pane.css('width')) - (settings.exclude*blocksize))+"px",
				timer = parseInt((parseInt($pane.css('width')) - (settings.exclude*blocksize))*13),
				speedlimit = blocksize*$(settings.items).length/timer,
				stopwatch;
			if( !$pane.ssbound )
				$pane
					.bind('prev.simpleScroll', moveReverse )
					.bind('next.simpleScroll', moveForward );
			if (auto) 
				$pane
					.bind('start.simpleScroll', function(e){
							if( !auto ){
								clear();
								auto = true;
								next();
							}
					})
					.bind('reverse.simpleScoll', function(e){
							if( !auto ){
								clear();
								auto = true;
								prev();
							}
					})
					.bind('jumpStart.simpleScroll', function() {
						if( !auto) {
							clear();
							auto = true;
							jumpNext();
						}
					})
					.bind('jumpReverse.simpleScroll', function() {
						if( !auto) {
							clear();
							auto = true;
							jumpPrevious();
						}
					})
					.bind('restart.simpleScroll', function() {
						auto = true;
						stopwatch = setTimeout(restart, 5000);
					})
					.bind('stop.simpleScroll', function(){
						clear();
						auto = false;
					});
			
			$pane.ssbound = true;
			
			if (auto) {
				cycleMove();
			}
			function cycleMove() {
				$pane
					.animate({left: dest}, timer, "linear",function() {
						direction = "left";})
					.animate({left: 0}, timer, "linear", function () {
						direction = "right";
						cycleMove();});
			}
			function moveForward() {
					direction = "right";
					$pane
						.stop()
						.animate({left: dest}, Math.abs(timer+pos/speedlimit), "linear");
			};
			function moveReverse() {
					direction = "left";
					$pane
						.stop()
						.animate({left: '0'}, Math.abs(pos/speedlimit), "linear");
			};
			function clear(){
				pos = parseInt($pane.css("left"));
				$pane.queue('fx',[]).stop();
			};
			function restart() {
				switch(direction) {
					case "left":
						$pane
							.stop()
							.animate({left: '0'}, Math.abs(pos/speedlimit), "linear", function() {direction = "right"; cycleMove()});
						break;
					case "right":
						$pane
							.stop()
							.animate({left: dest}, Math.abs(timer+pos/speedlimit), "linear")
							.animate({left: 0}, timer, "linear", function () {
								direction = "left";
								cycleMove();});
						break;
				}
			}
			function jumpNext() {
				var jumpPos = pos - blocksize*4 < parseInt(dest) ? parseInt(dest) + "px" : pos - blocksize*4 + "px";
				$pane
					.animate({left: jumpPos}, 50, "linear");
				pos = parseInt($pane.css("left"));
			}
			function jumpPrevious() {
				var jumpPos = pos + blocksize*4 > 0 ? 0 + "px" : pos + blocksize*4 + "px";
				$pane
					.animate({left: jumpPos}, 50, "linear");
				pos = parseInt($pane.css("left"));
			}
			function next() {
				$pane.trigger('next.simpleScroll');
			};
			function prev() {
				$pane.trigger('prev.simpleScroll');
			};	
		});
	};
	
})( jQuery );