imgRotator.collection = []; 

// arguments: image name, rotation speed
function imgRotator(imgname, speed, path) {
    this.imgname = imgname;
	this.speed = (typeof speed == 'undefined') ? 5000 : speed; // display a new image every 5 seconds by default
    this.path = (typeof path == 'undefined') ? '' : path; // display a new image every 5 seconds by default
	this.ctr = 0;
	this.timer = 0;
	this.imgs = [];
    this.index = imgRotator.collection.length;
	imgRotator.collection[this.index] = this;
    this.timeoutFunction = "imgRotator.collection[" + this.index + "]";
}

imgRotator.prototype.addImages = function() { // preloads images to prevent display delay
    var img;
    for (var i=0; arguments[i]; i++) {
        img = new Image();
        img.src = this.path + arguments[i];
        this.imgs[this.imgs.length] = img;
    }
}

imgRotator.prototype.rotate = function() {
    clearTimeout(this.timer); this.timer = null;
    if (this.ctr < this.imgs.length-1) this.ctr++;
    else this.ctr = 0;
    var imgObj = document.images[this.imgname];    
    if (imgObj) {
        imgObj.src = this.imgs[this.ctr].src;
        this.timer = setTimeout( this.timeoutFunction + ".rotate()", this.speed);
    }
}

imgRotator.start = function() {
    var len = imgRotator.collection.length
	var obj;
    for (var i=0; i<len; i++) {
        obj = imgRotator.collection[i];
        if (obj && obj.imgname )
            obj.timer = setTimeout( obj.timeoutFunction + ".rotate()", obj.speed);
    }
}
