var setOpacity = function() {};
if (navigator.appName == 'Microsoft Internet Explorer') {
    setOpacity = function(obj, opacity) {
        obj.style.filter = 'alpha(opacity=' + opacity + ')';
    }
}
else {
    setOpacity = function(obj, opacity) {
        obj.style.opacity = opacity / 100;
    }
}


var RolloverLoop = new function() {
    this._objects = [];

    this._cachObj = [];

    this._currId = null;

    this.S_IMG_OFF    = 0;

    this.S_IMG_VIEW   = 1;

    this.S_IMG_ON     = 2;

    this.S_IMG_HIDDEN = 3;
    
    this._intervalID = null;

    this._is_run = false;

    this.cnf = {
        delay:   10, // Time after which the cycle repeats
        unitOpc: 5, // The unit, which changes the value of opacity
        opcMax:  100, // Maximum opacity
        opcMin:  0 // Minimum opacity
    }

    this.start = function() {
        if (!this._is_run) {
            this._is_run = true;
            this._intervalID = window.setInterval(function() {RolloverLoop.loop.call(RolloverLoop)}, this.cnf.delay);
        }
    }

    this.stop = function() {
        if (this._is_run) {
            this._is_run = false;
            window.clearInterval(this._intervalID);
        }
    }

    this.loop = function() {
        for (index in this._objects) {
            this.action(this._objects[index]);
        }
    }

    this.action = function(obj) {
        if (obj.id != this._currId && (obj.state == this.S_IMG_HIDDEN || obj.state == this.S_IMG_OFF)) {
            obj.state = this.stateRevers(obj.state);
        }

        switch (obj.state) {
            case this.S_IMG_VIEW:
                this.actionView(obj);
                break;

            case this.S_IMG_ON:
                this.deleteObject(obj.id);
                break;

            case this.S_IMG_HIDDEN:
                this.actionHidden(obj);
                break;

            case this.S_IMG_OFF:
                break;
        }
    }

    this._add = function(objId) {
        var img = {}
        img.id = objId;
        if (typeof this._cachObj[objId] == 'undefined') {
            this._cachObj[objId] = document.getElementById(objId);
        }
        img.obj = this._cachObj[objId];
        img.state = this.S_IMG_HIDDEN;
        img.opacity = this.cnf.opcMax;
        this._objects[objId] = img;
    }

    this.addObject = function(objId, hide) {
        if (typeof this._objects[objId] == 'undefined' && hide) {
            this._add(objId);
            this._currId = objId;
//            this.start();
        }
        else if (typeof this._objects[objId] != 'undefined' && !hide) {
            this._currId = null;
            this._objects[objId].state = this.stateRevers(this._objects[objId].state);
        }
    }

    this.deleteObject = function(objId) {
        delete this._objects[objId];
//        if (this._objects.length == 0) {
//            this.stop();
//        }
    }

    this.stateRevers = function(state) {
        return (state == this.S_IMG_VIEW || state == this.S_IMG_ON)
             ? this.S_IMG_HIDDEN
             : this.S_IMG_VIEW;
    }

    this.actionView = function(obj) {
        obj.opacity = obj.opacity + this.cnf.unitOpc;
        if (obj.opacity >= this.cnf.opcMax) {
            obj.opacity = this.cnf.opcMax;
            obj.state = this.S_IMG_ON;
        }
        setOpacity(obj.obj, obj.opacity);
    }

    this.actionHidden = function(obj) {
        obj.opacity = obj.opacity - this.cnf.unitOpc;
        if (obj.opacity <= this.cnf.opcMin) {
            obj.opacity = this.cnf.opcMin;
            obj.state = this.S_IMG_OFF;
        }
        setOpacity(obj.obj, obj.opacity);
    }

    this.start();
}
if (navigator.appName == 'Microsoft Internet Explorer') {
    RolloverLoop.cnf.unitOpc = 34;
    RolloverLoop.cnf.delay = 10;
}

function rollover(objId, hide) {
    RolloverLoop.addObject(objId, hide);
    return true;
}
