var pCarousel = {
	animationId: null,
	animationDir: 'right',
	
	//time in miliseconds to wait before scrolling
	animationTimeout: 4000,
	
	//time in miliseconds for the scrolling transition
	animationSpeed: 400,
	
	
	init: function(carousel) {
	
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.clip.hover(function() { pCarousel.halt() }, function() { pCarousel.animate(carousel); });
		pCarousel.animate(carousel);
		
		$('.pCarouselNext').bind('click', function() {
			carousel.next();
			return false;
		});
		
		$('.pCarouselPrev').bind('click', function() {
			carousel.prev();
			return false;
		});
		
	},
	
	animate: function(carousel) {
		pCarousel.animationId = setInterval( function() {
			if( carousel.last == carousel.options.size ) { pCarousel.animationDir = 'left'; }
			else if( carousel.first == 1 ) { pCarousel.animationDir = 'right'; }
	
			if( pCarousel.animationDir == 'right' ) { carousel.next(); }
			else { carousel.prev(); }
		}, pCarousel.animationTimeout );
	},
	
	halt: function() {
		if( pCarousel.animationId ) {
			clearInterval(pCarousel.animationId);
		}
	}
	
};

$(document).ready(function() {
	$('#pCarousel').jcarousel({
		wrap: null,
		scroll: 1,
		visible: 1,
		initCallback: pCarousel.init,
		easing: 'linear',
		animation: pCarousel.animationSpeed,
		buttonPrevHTML: null,
		buttonNextHTML: null
	});
});

