function imageRotator(element,images,rotateTime,fadeTime){
	
	if (!element || (element && !element.nodeType)) { throw('Rotation element is not a DOM element'); }
	if (!(images instanceof Array || images instanceof Object)) { throw('Rotation images is not an object or array'); }	
	if(!rotateTime){ rotateTime = 8000; } // default 
	if(!fadeTime){ fadeTime = 4000; }     // default 
	
	this.rotationElement = element;
	this.rotationImages = images;
	this.rotateTime = rotateTime;
	this.fadeTime = fadeTime;
	this.imagesLoaded = [];
	
	this.loadImage(0,false);
	
}	
	
imageRotator.prototype.loadImage = function(imgNum,fade) {
    
	if(this.rotationImages.length == 0){
		return false;
	}
	
	if(imgNum >= this.rotationImages.length){
		imgNum = 0;
	}
    
	if(!this.imagesLoaded[imgNum]){
		
		var self = this;
		
		// IMPORTANT NOTE: It's vital that we use 'onload' instead of the camelCased 'onLoad', 
		// otherwise our version of mootools will create a syntax error on load completion:
		// see here: https://mootools.lighthouseapp.com/projects/2706-mootools/tickets/848
		// onError however works correctly.
		// IF MOOTOOLS IS UPGRADED WE WILL NEED TO CHANGE IT TO CAMELCASED. 
		
		var rotImage = new Asset.image(this.rotationImages[imgNum][0], {
	   	    id: 'image' + imgNum,
	   	    'class': 'rotatorImage',
	    	title: 'image',
	    	alt: this.rotationImages[imgNum][1],
	    	onload: function(){ 
	    		this.inject(self.rotationElement);
		    	self.showImage(imgNum, self.rotationImages.length - 1,fade);
		    },
		    onerror: function(){
            }
		});

	}else{ // all images now loaded so simply rotate them
		this.showImage(imgNum,this.rotationImages.length - 1,fade);	
	}		
		
};

imageRotator.prototype.showImage = function(imgNum,lastImage,fade,reverse){

    if(imgNum < 0){
        imgNum = 0;
    }

    // register image as loaded
    this.imagesLoaded[imgNum] = true;

    // store for external access
	this.currentImage = imgNum;

    // write the image number to the page
    if ($('imageNumber')) {
        $('imageNumber').set('text', String(this.currentImage + 1 + '/' + this.rotationImages.length));
    }
    
	if (reverse) {
        var previousNum = imgNum+1;
        if(imgNum == lastImage){
            previousNum = 0;
        }
    } else {
        var previousNum = imgNum-1;
        if(imgNum == 0){
            previousNum = lastImage;
        }
    }
		
	var existingId = 'image' + previousNum;
	var newId = 'image' + imgNum;
    
	if(fade){
		$(newId).set('opacity',0);
	}	
	
	$(newId).show();

	if($(existingId)){
		if(fade){
			$(existingId).set('tween', {duration: this.fadeTime});
			$(existingId).get('tween').start('opacity', 0).chain(function(){
				$(existingId).hide();	 
    		});
		}else{
			$(existingId).hide();
		}		
	}

	if(fade){
		$(newId).set('tween', {duration: this.fadeTime});
		$(newId).tween('opacity',1); 
	}
	
	this.delayControl = this.loadImage.delay(this.rotateTime,this,[imgNum+1,true]);
};

imageRotator.prototype.nextImage = function() {
    clearTimeout(this.delayControl);
    this.loadImage(this.currentImage+1, true);
}

imageRotator.prototype.previousImage = function() {
    clearTimeout(this.delayControl);
    this.showImage(this.currentImage-1, this.rotationImages.length - 1, true, true);
}

imageRotator.prototype.pause = function() {
    clearTimeout(this.delayControl);
}

imageRotator.prototype.play = function() {
    this.loadImage(this.currentImage+1, true);
}
