/*
 * Easing effect copied and tweaked from
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = .7; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
});

$(function() {
	$("#new a.previous,#new a.next").show();
	$("#new a.previous").click(function(e) { move(-1, e); });
	$("#new a.next").click(function(e) { move(1, e); });
	
	var list = $("#new ul.wine-list-new"),
		items = list.children().length - 1,
		width = list.children(":first-child").innerWidth(),
		position = 0,
		duration = 300
		wrapDuration = (items + 1)/2 * duration;
	
	function move(move, e) {
		e.preventDefault();
		
		// Advance position.
		position += move;
		
		// Animate list movement.
		// Wrap list if position less than 0 or greater than the number of items.
		if (position < 0) {
			position = items;
			list.animate({ marginLeft: (position * width * -1) + "px" }, wrapDuration, 'easeInOutBack');
		} else if (position > items) {
			position = 0;
			list.animate({ marginLeft: (position * width * -1) + "px" }, wrapDuration, 'easeInOutBack');
		} else {
			list.animate({ marginLeft: (position * width * -1) + "px" }, duration);
		}
	}
});