// requires prototype.js

var Rotator = Class.create({
	initialize: function(container, timer) {
		this.container = container;
		this.n = 0;
		this.timer = timer;
		var c = this.container;
		if ($(c + "_menu")) {
			$(c + "_menu").select(".item").each(function(element) {
				element.onclick = function() {
					clearTimeout(this.timeout);
					this.n = element.id.replace(c + "_menu_", "");
					this.show_item();
				}.bind(this)
			}.bind(this));
		}
		this.timeout = window.setTimeout(this.rotate.bind(this), this.timer);
	},

	rotate: function() {
		var c = this.container;
		var boxes = $(c).select(".item");
		var l = boxes.length;
		this.n++;
		if (this.n == l) this.n = 0;
		this.show_item();
		this.timeout = window.setTimeout(this.rotate.bind(this), this.timer);
	},

	show_item: function() {
		var c = this.container;
		var n = this.n;
		$(c).select(".item").each(function(element) {
			element.style.display = "none";
		});
		$(c + "_" + n).style.display = "block";
		if ($(c + "_menu")) {
			$(c + "_menu").select(".item").each(function(element) {
				element.removeClassName("current");
			});
			$(c + "_menu_" + n).addClassName("current");
		}
	}
});
