var global = this;
global.colorchanger = new function colorchanger() {
    var colorchanger = this;

    function ColorChanger(element, minvalues, maxvalues, initvalues) {
        if (element) {
            this._init(element, minvalues, maxvalues, initvalues);
        };
    };

    colorchanger.ColorChanger = ColorChanger;

    ColorChanger.prototype._init = function _init(element, minvalues,
                                                  maxvalues, initvalues) {
        this.element = element;
        this.minvalues = minvalues;
        this.maxvalues = maxvalues;
        this.current = initvalues;
        var cc = this;
    };

    ColorChanger.prototype.next = function next() {
        for (var i=0; i < this.current.length; i++) {
            var toadd = parseInt((Math.random() * 4) - 2);
            this.current[i] += toadd;
            if (this.current[i] < this.minvalues[i]) {
                this.current[i] = this.minvalues[i];
            } else if (this.current[i] > this.maxvalues[i]) {
                this.current[i] = this.maxvalues[i];
            };
        };
        var bgcolor = 'rgb(' +
              this.current[0] + ',' +
              this.current[1] + ',' +
              this.current[2] + ')';
        this.element.style.backgroundColor = bgcolor;
    };

    colorchanger.init = function init(elid, interval, minvalues, maxvalues,
                              initvalues) {
        var cc = new ColorChanger(document.getElementById(elid), minvalues,
                                  maxvalues, initvalues);
        window.loop_next = function() {
            cc.next();
        };
        loop_next();
        window.setInterval('window.loop_next()', interval);
    };
}();

