

// Logo grid scroller
if($('ul.logo-grid.scroller').length) {
	// Setup
	var $logoGridScroller = $('ul.logo-grid.scroller');
		$logoGridScroller
			.wrap('<div id="l-scroller"><div id="l-scroller-pane">')
			.parent()
			.after('<ul id="l-nav"><li id="l-prev">Previous</li><li id="l-next">Next</li></ul>');
	
	// Set some variables for later
	var gridWidth = 0;
	$logoGridScroller.find('li').each(function() {
		gridWidth += $(this).outerWidth();
	});
	var $container = $('#l-scroller'),
		paneWidth = $('#l-scroller-pane').width(),		// 860
		steps = Math.ceil(gridWidth/paneWidth) - 1,		// 4 (zero based)
		step = 0;
	
	// Bind to click event
	$logoGridScrollerNav = $('#l-nav').bind('click', function(e) {
		var target = e.target;
		if (target.nodeName === 'LI') {
			if (target.id == 'l-prev') {
				direction = -1;
			} else {
				direction = 1;
			}
			scrollLogoGrid(direction);
		};
	});
	
	// Scroll the grid
	var scrollLogoGrid = function(direction) {
		step += direction;
		if (step < 0) {
			step = 0;
			$container.find('.logo-grid').animate({marginLeft: 100}, 200, 'easeOutCirc', function() {$(this).animate({marginLeft: 0}, 200, 'easeInCirc')});
		} else if (step > steps) {
			step = steps;
			$container.find('.logo-grid').animate({marginLeft: '-=100'}, 200, 'easeOutCirc', function() {$(this).animate({marginLeft: '+=100'}, 200, 'easeInCirc')});
		} else {
			$('#l-nav li').removeClass('disabled');
			$container.find('.logo-grid').animate({marginLeft: -step * paneWidth}, 500, 'easeInOutExpo', function() {
				step == steps ? $('#l-next').addClass('disabled') : null;
				step == 0 ? $('#l-prev').addClass('disabled') : null;
			})
		}
	}
}


// 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,
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	}
});

