/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		carousel.startAuto(0);
		return false;
	});

	jQuery('#mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
	
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

/**
 * Triggers before animation.
 */
function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
	// No animation on first load of the carousel
	if (state == 'init')
		$("#carousel_nav1").addClass('picked');
	if (idx==1) {
		$("#carousel_nav1").addClass('picked');
		$("#carousel_nav2").removeClass('picked');
		$("#carousel_nav3").removeClass('picked');
		$("#carousel_nav4").removeClass('picked');
	}
	if (idx==2) {
		$("#carousel_nav1").removeClass('picked');
		$("#carousel_nav2").addClass('picked');
		$("#carousel_nav3").removeClass('picked');
		$("#carousel_nav4").removeClass('picked');
	}
	if (idx==3) {
		$("#carousel_nav1").removeClass('picked');
		$("#carousel_nav2").removeClass('picked');
		$("#carousel_nav3").addClass('picked');
		$("#carousel_nav4").removeClass('picked');
	}
	if (idx==4) {
		$("#carousel_nav1").removeClass('picked');
		$("#carousel_nav2").removeClass('picked');
		$("#carousel_nav3").removeClass('picked');
		$("#carousel_nav4").addClass('picked');
	}
};

/**
 * Helper function for printing out debug messages.
 * Not needed for jCarousel.
 */
var row = 1;
function display(s) {
	// Log to Firebug (getfirebug.com) if available
	//if (window.console != undefined && typeof window.console.log == 'function')
	  //  console.log(s);

	if (row >= 1000)
		var r = row;
	else if (row >= 100)
		var r = '&nbsp;' + row;
	else if (row >= 10)
		var r = '&nbsp;&nbsp;' + row;
	else
		var r = '&nbsp;&nbsp;&nbsp;' + row;

	jQuery('#display').html(jQuery('#display').html() + r + ': ' + s + '<br />').get(0).scrollTop += 10000;

	row++;
};

// Ride the carousel...
jQuery(document).ready(function() {
	jQuery("#mycarousel").jcarousel({
		auto: 5,
		wrap: 'last',
		scroll: 1,
		initCallback: mycarousel_initCallback,
		
		itemVisibleInCallback: {
			onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation
		},
		
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	var randomImages = ['one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty','twentyone','twentytwo','twentythree'];
	var rndNum = Math.floor(Math.random() * randomImages.length);
	$("div#header").addClass(randomImages[rndNum]);
	
});