/*
 * ImageRotatorPool() - creates a new ImageRotatorPool instance
 * 
 * 0 - ImageRotator instance (optional)
 */  
function ImageRotatorPool() {

    /* Field Initializers */
    this.unavailableImages = new Array(); /* Unavailable images for rotation */
    this.availableImages = new Array(); /* Available images for rotation */
    this.imgs = new Array(); /* HTML IMG elements to be rotated on */
    this.rotator = null; /* ImageRotator instance */
    this.duplication = true; 
    
    /* Set ImageRotator instance */
    if (arguments.length > 0) {
        this.rotator = arguments[0];
    } else {
        this.rotator = new ImageRotator(null);
		this.rotator.images = this.availableImages;
    }
    
	/* Method Definitions */
	
	/*
	 * addImage() - creates an Image instance from a source file and adds it to
	 * the calling ImageRotatorPool instance
	 * 
	 * src - source file
	 */    
	this.addImage = function(src) {
    	var i = new Image();
    	i.src = src;
    	this.addImageObject(i);
	}
	
	/*
	 * addImageObject() - adds an Image instance to the calling ImageRotatorPool
	 * instance
	 * 
	 * i - Image instance
	 */    
	this.addImageObject = function(i) {
    	this.availableImages.push(i);
	}

	/* 
	 * removeImage() - removes an Image instance from the calling 
	 * RotatorImagePool instance based on the source file
	 * 
	 * src - source file
	 */   
	this.removeImage = function(src) {
    	var i = new Image();
        i.src = src;
    	removeImageObject(i);
	}

	/*
	 * removeImageObject() - removes an Image instance from the calling
	 * RotatorImagePool instance
	 * 
	 * i - Image instance
	 */    
	this.removeImageObject = function(i) {
        removeItemFromArray(this.availableImages, i);
    	removeItemFromArray(this.usedImages, i);
	}
	
	/*
	 * addImgElement() - adds an HTML image element instance from the calling
	 * ImageRotatorPool instance
	 * 
	 *	 
	 *	 
	 * 	 
	 */    
	this.addImgElement = function(e) {
        this.imgs.push(e);
	}

	/*
	 * removeImgElement() - removes an HTML image element instance from the calling
	 * ImageRotatorPool instance
	 * 
	 * e - element to be removed
	 */    
	this.removeImgElement = function(e) {
        removeItemFromArray(this.imgs, e);
	}
	
	/*
	 * rotate() - rotates the sources on the IMG elements in the imgs array to
	 * different images sources
	 */  
    this.rotate = function() {
	    
	    if (this.duplication == false) {
            for (var j = 0; j < this.unavailableImages.length; j ++) {
                this.makeImageObjectAvailable(this.unavailableImages[j]);            
            }
        }
        
        for (var j = 0; j < this.imgs.length; j++) {;
            this.rotator.img = this.imgs[j];
            var i = this.rotator.rotate();
            
            /* Check to see if duplications are allowed */
            if (this.duplication == false) {
                this.makeImageObjectUnavailable(i);
            }
	    }
	    
	}
	
	/*
	 * makeImageAvailable() - moves an image from the unavailable images array 
	 * to the available images array based on the source file name
	 * 
	 * src - source file  
	 */  
	this.makeImageAvailable = function(src) {
        var i = new Image();
        i.src = src;
        this.makeImageObjectAvailable(i);
	}
	
	/*
	 * makeImageObjectAvailable() - moves an image from the unavailable images
	 * array to the available images array
	 * 
	 * i - image object
	 */    
	this.makeImageObjectAvailable = function(i) {
	    var idx = removeItemFromArray(this.unavailableImages, i);
	    
	    /* Make sure i actually was unavailable */
	    if (idx > -1) {
	        this.availableImages.push(i);
	    }
	
	}
	
	/*
	 * makeImageUnavailable() - moves an image from the available images array to
	 * the unavailable images array based on the source file name
	 * 
	 * src - source file  
	 */
	this.makeImageUnavailable = function(src) {
	    var i = new Image();
	    i.src = src;
	    this.makeImageObjectUnavailable(i);
	}
	
	/*
	 * makeImageObjectUnavailable() - moves an image from the available images
	 * array to the unavailable images array
	 * 
	 * i - image object
	 */
	this.makeImageObjectUnavailable = function(i) {
	    var idx = removeItemFromArray(this.availableImages, i);
	    
	    /* Make sure i actually was unavailable */
        if (idx > -1) {
	       this.unavailableImages.push(i);
	    }
	}
	
	/*
	 * rotateWithCallBack() - sets up a call back and starts a timer so the 
	 * image rotation can run indefinitely
	 * 
	 * returns action id
	 */   
	this.rotateWithCallBack = function() {
        var a;
        window.rotatorPool = this;
        var t = window.rotatorPool.rotator.timeout;
        a = window.setInterval("window.rotatorPool.rotate()", t);
	
        return a;
    }
	
	/*
	 * startRotation() - starts the image rotation on the ImageRotatorPool instance
	 */  
	this.startRotation = function() {
		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;
	    }
	}
}
	
/*
 * removeItemFromArray() - removes an item from an array by finding its
 * index
 * 
 * a - array to remove from
 * i - item to be removed
 */ 
function removeItemFromArray(a, i) {
	
    var index = -1;
    
    /* Find index of object */
	for (var j = 0; j < a.length; j++) {
		
		/* Check to see if this is the correct iteration */
		if (a[j] == i) {
			a.splice(j, 1)
            index = j;
			break;
		}
	}
	
    return index;
}	














