﻿function Rotator(panel)
{
	this.panel = $(panel);
}

Rotator.prototype = {

	init: function()
	{
		var parts = this.panel.select('.rotate');
		if (parts && parts.length > 0)
		{
			parts.each(this.initPart);
			this.panel.show();
			this.parts = parts;
			this.partIndex = -1;
			this.setPartIndex(0);
		}
	},

	initPart: function(part, index)
	{
		if (index > 0)
			part.hide();

		part.time = 5000;
		var m = part.className.match(/(\d+)sec/i);
		if (m)
		{
			part.time = 1000 * parseInt(m[1]);
		}
	},

	setPartIndex: function(index)
	{
		var time = 5000;

		// hide current part
		if (this.partIndex >= 0)
		{
			var part = $(this.parts[this.partIndex]);
			if (part)
			{
				// part.hide();
				new Effect.Fade(part, { duration: 0.5 });
			}
		}

		// check new index
		if (index >= this.parts.length)
		{
			index = 0;
		}

		// set new index
		this.partIndex = index;

		// show new part
		if (this.partIndex >= 0)
		{
			var part = $(this.parts[this.partIndex]);
			if (part)
			{
				// part.show();
				new Effect.Appear(part, { duration: 0.5, queue: 'end' });
				time = part.time;
			}
		}

		setTimeout(this.tick.bind(this), time);
	},

	tick: function()
	{
		this.setPartIndex(this.partIndex + 1);
	}
};

Event.observe(window, 'load', function()
{
	$$('.rotator').each(function(p)
	{
		var rotator = new Rotator(p);
		rotator.init();
	});
});