function ImageRotator(id, interval){
	var defaults = {
	_id: id,
	_image_array: [],
	_image_count: 0,
	_current_image: null,
	_container: null,
	_display_style: 'inline',
	_interval: (interval || 5000)
	}
	for(var a in defaults)
		this[a] = defaults[a];
	ImageRotator.instances[id] = this;
	this.start_rotating_images()
}
ImageRotator.instances = {};

ImageRotator.prototype.load_image_array = function(container) {
		elements = container.getElementsByTagName('img')
		for (var i=0; i < elements.length; i++) {
			img = elements[i]
			this._image_array[this._image_count] = img;
			this._image_count++;
			this._display_style = img.style.display;
			if (this._image_count != 1) { img.style.display = 'none'; }
		}
}
ImageRotator.prototype.rotate = function() {
			this.show_next_image();
			setTimeout('ImageRotator.rotate("'+this._id+'")', this._interval);
}
ImageRotator.prototype.show_next_image = function() {
		if (this._current_image != null) {
			// hide the last image we were looking at
			this._image_array[this._current_image].style.display = 'none';
			// Move onto the next image
			if (++this._current_image == this._image_count) { this._current_image = 0; }
			// show the next image
			this._image_array[this._current_image].style.display = this._display_style;
		}
		else { // this is the first run so we don't need to set any displays
			this._current_image = 0;
		}
}
ImageRotator.prototype.start_rotating_images = function() {
		var id = this._id
		if (this._container = document.getElementById(id)) {
			this.load_image_array(this._container);
			if (this._image_count > 0) { 
				if (this._image_count > 1) { this.rotate(); }
			}
			else {
				// alert('ImageRotator Error: No images found to rotate through.');
			}
		}
		else {
			alert('ImageRotator Error: Container id not found: '+id);
		}
}
// allow global reference by id instead of having to save the variable and create closures
ImageRotator.rotate = function(id) {
	this.instances[id].rotate();
}
// for backward compatibility
ImageRotator.start_rotating_images = function(id, interval) {
	new ImageRotator(id, interval);
}

