var voucherCarousel = {
	animationId: null,
	animationDir: 'right',
	
	//time in miliseconds to wait before scrolling
	animationTimeout: 5000,
	
	//time in miliseconds for the scrolling transition
	animationSpeed: 200,
	
	
	init: function(carousel) {
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.clip.hover(function() { voucherCarousel.halt() }, function() { voucherCarousel.animate(carousel); });
		
		//Disable the previous button
		$('div.previous_button').addClass('previous-button-disabled');
    
		//Disable next button if only 3 items or less
		if (carousel.options.size <=5)
		{
		  $('div.previous_button').unbind("click");
		  $('div.next_button').addClass('next-button-disabled').unbind("click");
		}
		else
		{
		
				voucherCarousel.animate(carousel);
				
				jQuery('div.next_button').bind('click', function() {
				voucherCarousel.halt();
				carousel.nextN(5);
				voucherCarousel.animate(carousel);
				return false;
			});
				
				jQuery('div.previous_button').bind('click', function() {
				 voucherCarousel.halt();
				 carousel.prevN(5);
				 voucherCarousel.animate(carousel);
				 return false;
			});
		}
	},
	
	animate: function(carousel) {
		voucherCarousel.animationId = setInterval( function() {
			if( carousel.last == carousel.options.size ) { voucherCarousel.animationDir = 'left'; }
			else if( carousel.first == 1 ) { voucherCarousel.animationDir = 'right'; }
	
			if( voucherCarousel.animationDir == 'right' ) { carousel.next(); }
			else { carousel.prev(); }
			
				
		}, voucherCarousel.animationTimeout );
	},
	
	halt: function() {
		if( voucherCarousel.animationId ) {
			clearInterval(voucherCarousel.animationId);
		}
	}
	
};

function voucherCarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {

    if( carousel.last == carousel.options.size ){ 
       $('div.next_button').addClass('next-button-disabled').unbind("click");}
    else{
       $('div.next_button').removeClass('next-button-disabled').addClass('next-button').bind('click', function() {
                                                                                                                    voucherCarousel.halt();
                                                                                                                    carousel.nextN(5);
                                                                                                                    voucherCarousel.animate(carousel);
                                                                                                                    return false;
   			                                                                                                              });}
    
    if( carousel.first == 1 ){ 
       $('div.previous_button').addClass('previous-button-disabled').unbind("click");}
    else{
       $('div.previous_button').removeClass('previous-button-disabled').addClass('previous-button').bind('click', function() {
                                                                                                                     voucherCarousel.halt();
                                                                                                                     carousel.prevN(5);
                                                                                                                     voucherCarousel.animate(carousel);
                                                                                                                    return false;
                                                                                                                });}
   
};

jQuery(document).ready(function() {

	jQuery('#vouchercarousel').jcarousel({
		wrap: null,
		scroll: 1,
		visible: 5,
		initCallback: voucherCarousel.init,
		easing: 'linear',
		animation: voucherCarousel.animationSpeed,
		buttonPrevHTML: null,
		buttonNextHTML: null,
		itemVisibleOutCallback: {onAfterAnimation: voucherCarousel_itemVisibleOutCallbackAfterAnimation}
	});
});