function TresbonServicesRotator(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 = 0; /* Small logo pic */
    
    this.LEFT_IMG = 0; /* img element for left image pool */
    this.RIGHT_IMG = 1; /* img element for right image pool */
	
	this.smModelImages = new Array();
	this.lgModelImages = new Array();
	this.smUnavailableImages = new Array();
	this.lgUnavailableImages = new Array();
	
	this.smImgs = new Array();
	this.lgImgs = new Array();
	
	this.leftImgs = new Array();
	this.rightImgs = new Array();
	
	this.leftImg;
	this.right1Img;
	this.right2Img;
	this.timeout;
	this.action;
	
	
	
	

	/*
     * 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;
		}
                
    }
    
    /*
     * 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 == 0)  {
            this.smImgs.push(e);
        } else if (t == 1) {
            this.lgImgs.push(e);
        }
    }
	
	this.rotateWithCallBack = function() {
    	var a;
		var t = this.timeout;
    	window.tresbonrotator = this;
        a = window.setInterval("window.tresbonrotator.rotate()", t);
    
        return a;
    }
	
	this.rotate = function() {

		this.leftImg = this.leftImgs[selectRandomIndex(this.leftImgs)];
		this.right1Img = this.rightImgs[selectRandomIndex(this.rightImgs)];
		this.right2Img = this.rightImgs[selectRandomIndex(this.rightImgs)];
		
		var a = new Array();
		a.push(this.leftImg);
		a.push(this.right1Img);
		a.push(this.right2Img);
		
		for (var i = 0; i < a.length; i++) {
			
			
			var idx = findObjectInArray(a[i], this.smImgs);
			
			var n = a[i].parentNode.getElementsByTagName("img");
			
			for (var j = 0; j < n.length; j++) {
                var iIdx = findObjectInArray(n.item(j), this.smModelImages);
                if (iIdx > -1) {
                    var img = this.smModelImages.splice(iIdx, 1);
					this.smUnavailableImages.push(img[0]);
                } else {
                    iIdx = findObjectInArray(n.item(j), this.lgModelImages);
                    
					var img = this.lgModelImages.splice(iIdx, 1);
					this.lgUnavailableImages.push(img[0]);
				}
            }
            
			var p =  new ImageRotator(a[i], this.timeout);
			
			if (idx > -1) {
				p.images = this.smModelImages;
			} else {
				p.images = this.lgModelImages;
			}
			
			p.rotate();
			
			
            this.smModelImages = this.smModelImages.concat(this.smUnavailableImages);
			this.lgModelImages = this.lgModelImages.concat(this.lgUnavailableImages);
			
			this.smUnavailableImages = new Array();
			this.lgUnavailableImages = new Array();
			
			//alert(this.smModelImages.length);
        
        }
			
		
	}
    
	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.timeout = t;
}

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;
}



	
	
	
    

