/**
 * 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;
    });
    
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function mycarousel_itemVisibleInCallback(carousel){
	$('.jcarousel-control a').parent().find('a').removeClass('current');
	$('.jcarousel-control a.carousel_'+carousel.first).addClass('current');	
	$('.jcarousel-control').hide();
	$('.jcarousel-control').show();
}

// Ride the carousel...
jQuery(document).ready(function() {
	
	startIndex = Math.floor(Math.random() * $("#frontPageCarousel > li").size()) + 1;
	
    jQuery("#frontPageCarousel").jcarousel({
        start: startIndex,
    	scroll: 1,
        auto: 7,
        animation: 400,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemFirstInCallback: {onBeforeAnimation:mycarousel_itemVisibleInCallback},
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});
