jQuery.easing.jswing = jQuery.swing;
jQuery.extend( jQuery.easing,
{
	def: 'easeOutCubic',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	}
});

jQuery.iSliderJS = {
	build : function(user_options)
	{
		var defaults = {
			direction:"horizontal",
			list_width: 6740,
			window_width: 280,
			window_height: 250,
			pikachoose: false
		};
		return $(this).each(function(){
			var options = $.extend(defaults, user_options); 
			var acceleration = 0;
			var initx = 0; //intial
			var inity = 0;
			var movex = [];
			var downtime = 0;
				
			$(this).wrap("<div class='sliderjs'></div>");
			var $sliderul = $(this);
			var $sliderjs = $(this).parent("<div>");
			var divcss = {
				width: options.window_width+"px",
				height: options.window_height+"px",
				overflow:"hidden",
				position:"relative"
			};
			var ulcss = {
				position: "relative",
				width: options.list_width+"px"
			};
			$sliderjs.css(divcss);
			$sliderul.css(ulcss);
			if($.browser.msie){
				$sliderul.children().mousedown(function(){
				return false;});
				$sliderul.children().children().mousedown(function(){
				return false;});
			}
			
			$sliderjs.bind('selectstart', function() {
                    return false;
                });
			$sliderjs.bind("mousedown", function(e){
				$sliderul.stop();
				$sliderul.dequeue();
				movex.splice(0);
				initx = e.pageX;
				var date = new Date();
				downtime = date.getTime();
				var xlen = 0;
				var ulinitx =  $sliderul.position().left;
				$().bind("mousemove", function(e){
					if(movex[xlen-1]!=e.pageX){
						xlen = movex.push(e.pageX);
					}
					
					if(xlen>10){
						movex.splice(0,6);
						xlen = movex.push(e.pageX);
					}
					
					if(movex.length>3){
						if((movex[xlen-3]>=movex[xlen-2]) &&(movex[xlen-2]>=movex[xlen-1])){
						}else if((movex[xlen-3]<=movex[xlen-2]) &&(movex[xlen-2]<=movex[xlen-1])){
						}else{
						}
					}
					
					var newleft = parseInt(ulinitx, 10)+parseInt((e.pageX-initx)/1.5, 10);
					if(newleft<((options.list_width*-1)+parseInt(options.window_width, 10)-50)){
						newleft=((options.list_width*-1)+parseInt(options.window_width, 10)-50);
					}
					if(newleft>50){newleft=50;}
					$sliderul.css("left",newleft+"px");
				});
				$().bind("mouseup", MeatAndPatatos);
				return false;
			});
				
			function Animate(xvalue){
				$sliderul.stop();
				$sliderul.dequeue();
				$sliderul.animate({
					left:xvalue+"px"
				},1500,"easeOutCubic");
			}
			
			function MeatAndPatatos(e){
				$().unbind("mousemove");
				$().unbind("mouseup", MeatAndPatatos);
				var date = new Date();
				var uptime = date.getTime();
				
				var velocity = (movex[movex.length-1]*100-movex[movex.length-2]*100)/(uptime-downtime);
				var distance = movex[movex.length-1]-movex[movex.length-2];
				var negative = 1;
				if(distance<0){
					negative = -1;
				}
				var ulinitx =  $sliderul.position().left;
				var animateleft =  parseInt(ulinitx, 10)+(velocity * distance * negative)/2;
				if(animateleft<(options.list_width*-1)+parseInt(options.window_width, 10)){
					animateleft=(options.list_width*-1)+parseInt(options.window_width, 10);
				}
				if(animateleft>0){animateleft=0;}
				if(isNaN(animateleft)){}else{
					Animate(animateleft);
				}				
			}			

			function MoveToLi(){
				var pos = $(this).parent('li').position();
				var width = $(this).css("width").slice(0,-2);
				var lileft = pos.left;
				var liright = parseInt(pos.left, 10)+parseInt(width, 10);
				var ulleft = $sliderul.position().left;
				var low = ulleft*-1;
				var high = low+options.window_width;
				if((lileft>=low)&&(liright<=high)){
					return;
				}else{
					var slide =0;
					if(lileft<low){
						slide = parseInt(lileft, 10)*-1;
					}else{
						slide = ((liright-high)*-1)+parseInt(ulleft, 10);
					}					
					Animate(slide);
				}			
			}
		
			if(options.pikachoose){
				var $lis = $sliderul.children('li');
				$lis.children().bind("click", MoveToLi);
				
			}
		});
		
	}
};
jQuery.fn.SliderJS = jQuery.iSliderJS.build;
