// JavaScript Document

var Carousel =
{
	framePrefix: "quote-",
	carouselId: "carousel",
	wait: 5000,
	currentFrame: 0,
	totalFrames: 4,
	intervalId: 0,
	swapFrame: function(frame)
	{
		new Effect.Fade(Carousel.framePrefix + Carousel.currentFrame, { duration:1, from:1.0, to:0.0 });
		new Effect.Appear(Carousel.framePrefix + frame, { duration:1, from:0.0, to:1.0 });
	},
	moveFrame: function(frame)
	{
		clearInterval(Carousel.intervalId);
		Carousel.intervalId = setInterval('Carousel.moveFrame(Carousel.currentFrame + 1)', Carousel.wait);

		var currentFrame = Carousel.currentFrame;
		
		if (frame < 0)
		{
			frame = Carousel.totalFrames - 1;
		}
		
		frame = frame % Carousel.totalFrames;

		Carousel.swapFrame(frame);		
		Carousel.currentFrame = frame;
	},
	init: function()
	{
		var carousel = $(Carousel.carouselId);
		var frames = $$('#' + Carousel.carouselId + ' li');		
		
		for (var i=0; i < Carousel.totalFrames; i++)
		{
			frames[i].frame = i;
			frames[i].id = Carousel.framePrefix + i;
		}
		
		// Random starting frame
		Carousel.totalFrames = frames.length;		
		Carousel.currentFrame = Math.floor(Math.random() * Carousel.totalFrames);		
		frames[Carousel.currentFrame].setStyle({display: 'block'});
		
		Carousel.intervalId = setInterval('Carousel.moveFrame(Carousel.currentFrame + 1)', Carousel.wait);
	}	
}

Carousel.init();