// JavaScript Document\
var pCarousel = {
	animationId: null,
	animationDir: 'right',


	//time in miliseconds to wait before scrolling
	animationTimeout: 5000,

	//time in miliseconds for the scrolling transition
	animationSpeed: 10,


	init: function(carousel) {

		var nextIterator = null;
		var prevIterator = null;

		// Disable autoscrolling if the user clicks the prev or next button.

		animationId = pCarousel.animate(carousel);

		carousel.buttonNext.bind('mousedown', function() {
			nextIterator = setInterval(function(){carousel.next()},100);
			carousel.stopAuto();
			carousel.options.scroll = 1;
			carousel.options.animation = 0;
			pCarousel.halt();
		});

		carousel.buttonPrev.bind('mousedown', function() {
			prevIterator = setInterval(function(){carousel.prev()},100);
			carousel.stopAuto();
			carousel.options.scroll = 1;
			carousel.options.animation = 0;
			pCarousel.halt();
		});

		carousel.buttonNext.bind('mouseup',function() {
			pCarousel.halt();
			clearInterval(nextIterator);

		});

		carousel.buttonPrev.bind('mouseup',function() {
			pCarousel.halt();
			clearInterval(prevIterator);
		});

        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);
		}
	}

};


