/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
	jQuery('#products-nav a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('id').replace(/carousel-/i, '')));
		jQuery(this).blur();
		jQuery('#products-nav a').removeClass('current');
		jQuery(this).addClass('current');
		return false;
	});

	jQuery('#products-nav-next').bind('click', function() {
		carousel.next();
		jQuery(this).blur();
		return false;
	});

	jQuery('#products-nav-prev').bind('click', function() {
		carousel.prev();
		jQuery(this).blur();
		return false;
	});
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		jQuery('#products-nav a').removeClass('current');
	jQuery('#carousel-' + idx).addClass('current');
};

// Credits: Robert Penners easing equations (http://www.robertpenner.com/easing/).
jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

// Ride the carousel...
jQuery(document).ready(function() {
	function getPlan() {
		if ($.query.get('knight')) {
			return 1;
		}
		if ($.query.get('bishop')) {
			return 2;
		}
		if ($.query.get('king')) {
			return 3;
		}
		if ($.query.get('basic')) {
			return 4;
		}
		if ($.query.get('custom')) {
			return 5;
		}
	}
	jQuery("#mycarousel").jcarousel({
		/*easing: 'BounceEaseOut',
		animation: 500,*/
		start: getPlan(),
		scroll: 1,
		initCallback: mycarousel_initCallback,
		itemFirstInCallback:  mycarousel_itemFirstInCallback
		// This tells jCarousel NOT to autobuild prev/next buttons
		/*buttonNextHTML: null,
		buttonPrevHTML: null*/
	});
});