var ImageRotator = Class.create();

ImageRotator.prototype = {
	initialize: function(containerId, imageUrlArray, rotateTime) {
		this.container = $(containerId);
		this.imageUrlArray = imageUrlArray;
		this.rotateTime = rotateTime;
		this.rotateInterval = null;
		this.currentIndex = -1;
		this.start();
	},

	next: function() {
		this.currentIndex++;
		if (this.currentIndex >= this.imageUrlArray.length) {
			this.currentIndex = 0;
		}
	},

	display: function(imageIndex) {
		if (imageIndex != null && imageIndex >= 0 && imageIndex < this.imageUrlArray.length) {
			this.currentIndex = imageIndex;
		} else {
			this.next();
		}

		this.container.innerHTML = '<img src="' + this.imageUrlArray[this.currentIndex] + '" />';
	},

	start: function() {
		var imageRotator = this;

		if (this.rotateInterval == null) {
			this.rotateInterval = new PeriodicalExecuter(
				function () {
					imageRotator.display();
				},
				this.rotateTime
			);
		}
	},

	stop: function() {
		if (this.rotateInterval != null) {
			this.rotateInterval.stop();
			this.rotateInterval = null;
		}
	},

	select: function(imageIndex) {
		this.stop();
		this.display(imageIndex);
	}
};