/**
 * Used jQuery plugin jCarousel from /themes/pokersavvy/jcarousel
 */

jQuery(document).ready(function() {
    jQuery('#rakeback-carousel').jcarousel({
    	scroll: 1,
    	auto: 10,
    	wrap: "both",
    	initCallback: rakeback_carousel_initCallback,
		itemVisibleOutCallback: {  
            onAfterAnimation:  rakeback_carousel_itemVisibleOutCallbackAfterAnimation 
        }, 
        itemVisibleInCallback: {          
            onAfterAnimation:  rakeback_carousel_itemVisibleInCallbackAfterAnimation 
        }, 
    	// This tells jCarousel NOT to autobuild prev/next buttons
    	buttonNextHTML: null,
    	buttonPrevHTML: null
    });
});

function rakeback_carousel_initCallback(carousel, state) {
	// pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
    	if(!carousel.paused) {
    		carousel.startAuto();
    	}
    });
	
	
	jQuery('table#rakeback-carousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('#rakeback-carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('#rakeback-carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
	
	jQuery('#rakeback-carousel-pause').bind('click', function() {
		if(!carousel.paused) {
			carousel.stopAuto();
			carousel.paused = true;
		} else {
			carousel.startAuto();
			carousel.paused = false;
		}
		return false;
	});
};

/** 
 * This is the callback function which receives notification 
 * when an item is no longer the first one in the visible range. 
 * Triggered after animation. 
 */ 
function rakeback_carousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) { 
	$('a#rakeback-control-button-'+idx).removeClass('rakeback-carousel-control-button-active');
	$('a#rakeback-carousel-signup-link-'+idx).css('display', 'none');
}; 
/** 
 * This is the callback function which receives notification 
 * when an item becomes the first one in the visible range. 
 * Triggered after animation. 
 */ 
function rakeback_carousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) { 
	$('a#rakeback-control-button-'+idx).addClass('rakeback-carousel-control-button-active');
	$('a#rakeback-carousel-signup-link-'+idx).css('display', 'block');

	// set separate duration for each clip
	carousel.stopAuto();
	if(clipDuration[idx]) {
		carousel.options.auto = clipDuration[idx];
	} else {
		carousel.options.auto = 10;
	}
	carousel.startAuto();
}; 
