/*
 * TresbonRotator.js
 * 
 * Contains an implementation of ImageRotatorPool.js for Tres Bon Salon
 * 
 * Requires ImageRotator.js, ImageRotatorPool.js
 */
 
 
 function TresbonRotator(t) {
 
    
    /* Constants - meaning do not change these values ;-) */
    this.SMALL_MODEL = 0; /* Small model pic */
    this.LARGE_MODEL = 1; /* Large model pic */
    this.SMALL_LOGO = 2; /* Small logo pic */
    
    this.LEFT_IMG = 0; /* img element for left image pool */
    this.RIGHT_IMG = 1; /* img element for right image pool */
    
    /* Fields */
    this.smModelImages = new Array(); /* Small model pics */
    this.lgModelImages = new Array(); /* Large Model pics */
    this.smLogoImages = new Array(); /* Small logo pics */
    this.smAvailableImages = new Array(); /* Small images that are available */
    this.lgAvailableImages = new Array(); /* Large images that are available */
    this.smImgs = new Array(); /* Small img elements */
    this.lgImgs = new Array(); /* Large img elements */
    this.leftImgs = new Array(); /* Left side img elements */
    this.rightImgs = new Array(); /* Right side img elements */
    this.rotatorPool = null; /* ImageRotatorPool instance */;
    this.action = null; /* Action id of rotation */
	this.unavailableImages = null /* Unavailable images */
	this.imgs /* Images selected for random processing */
    
    /* Methods */
    
    /*
     * addImage() - adds an image based on the source file to an image pool
     * 
     * src - source file
     * t - constant representing what pool the image should be added to
     */                    
    this.addImage = function(src, t) {
        var i = new Image();
        i.src = src;
        
        this.addImageObject(i, t);
    }
    
    /*
     * addImageObject() - adds Image instance to an image pool
     * 
     * i - Image instance to addF
     * t - constant representing what pool the image should be added to
     */                         
    this.addImageObject = function(i, t) {
        
        
        /* Figure out where Image i should go */
        switch (t) {
            case this.SMALL_MODEL:
                this.smModelImages.push(i);
                break;
            
            case this.LARGE_MODEL:
                this.lgModelImages.push(i);
                break;
                
            case this.SMALL_LOGO:
                this.smLogoImages.push(i);
        }
    }
    
    /*
     * addImgElement() - adds an img element to be rotated over to a 
     * this instance
     * 
     * e - image element to add
     * t - type of image     
     * s - which side of the screen the img element is found on
     */                         
    this.addImgElement = function(e,t, s) {
        
        var tr = this;
        
        /* Group by side */
        if (s == tr.LEFT_IMG) {
            this.leftImgs.push(e);
        } else if (s == tr.RIGHT_IMG) {
            this.rightImgs.push(e);
        }
        
        /* Group by size */
        if (t == tr.SMALL_MODEL || t == tr.SMALL_LOGO) {
            this.smImgs.push(e);
        } else if (t == tr.LARGE_MODEL) {
            this.lgImgs.push(e);
        }
    }
    
    /* addImgElementsFromParent() - adds all img elements found within the
     * scope of the parent element
     * 
     * e - parent element to add img elements from
     * s - side of the screen the parent element is found on
     * 
     * Do Not Use - incomplete          
     */                    
    this.addImgElementsFromParent = function(e, s) {
        var n = e.getElementsByTagName("img");
        for (var i = 0; i < n.length; i++) {
            this.addImgElement(n.item(i), s);
        }
    }
      
    /*
	 * 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.tresbonrotator = this;
        var t = window.tresbonrotator.rotatorPool.rotator.timeout;
        a = window.setInterval("window.tresbonrotator.rotate()", t);
    
        return a;
    }
    
    this.rotate = function() {
        this.synchronize();
		this.selectImgs();
		this.selectImages();
    }
    
    this.selectImages = function() {
        var tr = this;
        var p = this.rotatorPool;
        
        var sm = new Array();
		var lg = new Array();
        
        /* Figure out if the image element is small or large */
        for (var i = 0; i < this.imgs.length; i++) {
			
            var idx = findObjectInArray(this.imgs[i], this.smImgs);
            if (idx > -1) {
                sm.push(this.imgs[i]);
            } else {
                lg.push(this.imgs[i]);
            }
        }
        
        /* Swap the available image pools around as necessary and rotate */
  		for (var i = 0; i < 2; i++) {
			if (sm.length > 0) {
				p.availableImages = this.smAvailableImages;
				p.imgs = sm;
				sm = new Array();
            } else if (lg.length > 0) {
				p.availableImages = this.lgAvailableImages;
				p.imgs = lg;
				lg = new Array();
            }
			if (p.imgs.length > 0 && p.availableImages.length > 0) {
				p.rotator.images = p.availableImages;
				p.unavailableImages = new Array();
				p.rotate();
			}
		
		}
		
		var z;
		
    }
    
    this.synchronize = function() {
        
        var p = this.rotatorPool
        this.smAvailableImages = new Array();
        this.lgAvailableImages = new Array();
		this.unavailableImages = new Array();
		p.unavailableImages = new Array();
        
        var a = new Array(this.smModelImages, this.smLogoImages, 
                this.lgModelImages)
        
        /* Look through each of the image pools */
        for (var i = 0; i < a.length; i++) {
            var ia = a[i];
            
            for (var j = 0; j < ia.length; j++) {
                var idx; 
                
                /* See if the image is on the left side */
                idx = findObjectInArray(ia[j], this.leftImgs);
                
                /* See if the image is on the right side */
                if(idx < 0) {
                    idx = findObjectInArray(ia[j], this.rightImgs);
                }
                
                /* if we didn't find the image make it available */
                if (idx < 0 && i < 2) {
					this.smAvailableImages.push(ia[j]);
                } else if (idx < 0 && i == 2) {
                    this.lgAvailableImages.push(ia[j]);
                } else {
                    this.unavailableImages.push(ia[j]);
                }
				
			} 
		}
    	var z;
	}
	
    /*
     * selectImgs() - selects img tags for rotation
     */     
    this.selectImgs = function() {
        var imgs = new Array();
        var i = -1;
        
        /* Get the image elements from the right side */
        for (var k = 0; k < 2; k++) {
            var j;
			do {
                j = selectRandomIndex(this.rightImgs);
            } while (j == i);
			i = j;
            
            imgs.push(this.rightImgs[i]);
        }
        
        /* Get an image element from the left side */
        i = -1;
        i = selectRandomIndex(this.leftImgs);
        
        imgs.push(this.leftImgs[i]);
        
        /* Put selected img elements into our rotator pool instance */
        this.imgs = imgs;
    }
    
    /*
	 * 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;
		}
	}
	
	this.rotatorPool = new ImageRotatorPool(new ImageRotator(null, t));
	this.rotatorPool.duplication = false;
}

/*
 * selectRandomIndex() - selects a random index from an array
 * 
 * a - array the index is supposed to be selected from
 * 
 * returns selected index
 */     
function selectRandomIndex(a) {
    return Math.round(Math.random() * (a.length - 1));
}

/* 
 * findObjectInArray() - tries to find an object in an array
 * 
 * o - object we are trying to find
 * a - array being searched
 * 
 * returns index or -1 if object is not found
 */     
function findObjectInArray(o, a) {
    for (var i = 0; i < a.length; i++) {
		if (o.src == a[i].src) {
            return i;
        }
    }
    return -1;
}

/* 
 * copyArray() - copies elements from one array to another
 * 
 * a - array being copied
 * 
 * returns new array
 */     
function copyArray(a) {
	var ar = new Array();
	for (var i = 0; i < a.length; i ++) {
		ar.push(a[i]);
	}
	
	return ar;
}
