var ImageRotator = function(id, aUrls, interval) {
    this.id = id;
    this.images = aUrls;
    this.interval = interval;

    if (this.interval !== undefined)  {
        this.schedule();
    }
};

ImageRotator.prototype = {
    images: [],
    current: null,
    random: function() {
        return this.images[Math.floor(Math.random() * this.images.length)];
    },
    next: function() {
        if (this.current === null ||
            ++this.current == this.images.length)  {

            this.current = 0;
        }
        return this.images[this.current];
    },
    previous: function() {
        if (this.current === null ||
            --this.current < 0)  {
            this.current = this.images.length - 1;
        }
        return this.images[this.current];
    },
    callback: function() {
        document.getElementById(this.id).src = this.next();         
        this.schedule(this.interval);
    },
    schedule: function()  {
        var that = this;
        setTimeout(function() { that.callback.call(that); }, this.interval);
    }
};

