/*
 * ImageRotator.js - Rotates an image by switching it with another set of images
 */

/*
 * ImageRotator - creates a new ImageRotator object
 * 
 * 0 - image element to be rotated on (required)
 * 1 - timeout between iterations in milliseconds (optional - default: 500)
 * 
 * returns instance  
 */   
function ImageRotator() {
	
	var args = arguments;
	var t = 500;
	
	/* If no arguments are supplied throw an exception */
	if (args.length == 0) {
		throw ("At least one argument is required.");
	}
	
	/* See if we have a new value for timeout */
	if (args.length > 1) {
	   t = args[1];
	}

	/* Field Initializers */
	this.timeout = t;
	this.action = null;
	this.img = args[0];
	this.images = new Array();

	/* Method Definitions */

	/*
	 * addImage() - Adds an image to the image rotation based on 
	 * the image source
	 *
	 * src - source of the image
	 */
	this.addImage = function(src) {
		var i = new Image();
		i.src = src;
		this.addImageObject(i);
	}
	
	/*
	 * addImageObject() - Adds an image object to the image 
	 * rotation
	 * 
	 * i - image object to add
	 */
	this.addImageObject = function(i) {
		this.images.push(i);
	}
	
	/*
	 * removeImage() - removes an image from the image rotation 
	 * based on the image source
	 *
	 * src - source of the image
	 *
	 * returns index or -1 if image object not found
	 */
	this.removeImage = function(src) {
		var i = new Image(src);
		this.removeImageObject(i);
	}

	/*
	 * removeImageObject(i) - removes an image from the image rotation 
	 * based on the image source
	 *
	 * i - Image object
	 *
	 * returns index or -1 if image object not found
	 */
	this.removeImageObject = function(i) {
	
		var index = -1;
	
		/* Find index of object */
	   for (var j = 0; j < this.images.length; j++) {
		
		  /* Check to see if this is the correct iteration */
		  if (this.images[j] == i) {
			 this.images.splice(j, 1);
				index = j;
			 break;
		  }
	   }
	
		return index;
	}

	/*
	 * rotate() - Rotates the rotating image on the web page
	 *
	 * returns rotated image
	 */
	this.rotate = function() {

		if (this.images.length > 0  && this.img.src) {
			var j;
			j = Math.round(Math.random() * (this.images.length - 1));
	

			var i = this.images[j];
	
			this.img.src = i.src;
	
			return i;
		} else {
			return null;
		}
	}

	/*
	 * rotateWithCallBack() - sets a pointer to the current ImageRotator 
	 * instance on window.rotator so rotate() can be called from a context 
	 * where this is not available.  Also starts a setInterval() for a 
	 * duration of this.timeout
	 * 
	 * returns action id  
	 */  
	this.rotateWithCallBack = function() {
		var a;
		window.rotator = this;
		var t = window.rotator.timeout;
		a = window.setInterval("window.rotator.rotate()", t);

		return a;
	}
	
	/*
	 * startRotation() - starts the image rotation on the ImageRotator 
	 * instance
	 */   
	this.startRotation = function() {
	
		/* If there is already a rotation in progress don't want to trash it */
		if (this.action == null) {
			this.action = this.rotateWithCallBack();
		}
	}
	
	/*
	 * stopRotation() - stops a rotation if one is currently in progress
	 */ 
	this.stopRotation = function() {
		if (this.action != null) {
			window.clearInterval(this.action);
			this.action = null;
		}
	}
}















