
function Rollover () {
    this.element = null;
    
    this.currentState = Rollover.STATE_UNKNOWN;
    
    this.activated = true;
    
    this.display = {
        normal : {
            foreground : Rollover.DISPLAY_NORMAL_FOREGROUND,
            background : Rollover.DISPLAY_NORMAL_BACKGROUND
        },
        over : {
            foreground : Rollover.DISPLAY_OVER_FOREGROUND,
            background : Rollover.DISPLAY_OVER_BACKGROUND
        }
    }
    
    this.rolloverListeners = new Array();
    
}

Rollover.DISPLAY_NORMAL_FOREGROUND = '';
Rollover.DISPLAY_NORMAL_BACKGROUND = '';

Rollover.DISPLAY_OVER_FOREGROUND = '';
Rollover.DISPLAY_OVER_BACKGROUND = '';

Rollover.STATE_UNKNOWN = 0;
Rollover.STATE_OUT = 1;
Rollover.STATE_OVER = 2;

Rollover.subscribes = function (element) {
    var target = element;
    var onCreateFunction = null;
    var activated = true;
    var currentState = Rollover.STATE_UNKNOWN;
    var foregroundValueNormal = Rollover.DISPLAY_NORMAL_FOREGROUND;
    var backgroundValueNormal = Rollover.DISPLAY_NORMAL_BACKGROUND;
    
    var foregroundValue = Rollover.DISPLAY_OVER_FOREGROUND;
    var backgroundValue = Rollover.DISPLAY_OVER_BACKGROUND;
    
    switch (arguments.length) {
        case 8 : {
            onCreateFunction = arguments[7];
            
        }
        
        case 7 : {
            activated = (arguments[6] != null) ? arguments[6] : activated;
            
        }
        
        case 6 : {
            foregroundValueNormal = (arguments[5] != null) ? arguments[5] : foregroundValueNormal;
            
        }
        
        case 5 : {
            backgroundValueNormal = (arguments[4] != null) ? arguments[4] : backgroundValueNormal;
            
        }
        
        case 4 : {
            foregroundValue = (arguments[3] != null) ? arguments[3] : foregroundValue;
            
        }
        
        case 3 : {
            backgroundValue = (arguments[2] != null) ? arguments[2] : backgroundValue;
            
        }
        
        case 2 : {
            currentState = (arguments[1] != null) ? arguments[1] : currentState;
            
            if ((currentState != Rollover.STATE_OUT)
                    && (currentState != Rollover.STATE_OVER)) {
                currentState = Rollover.STATE_UNKNOWN;
            }
            
        } break;
        
        
        default : {
            // do nothing
        } break;
        
    }
    
    window.appendLoadEvent(function () {
        if (typeof element == 'string') {
            target = document.getElementById(element);
            if (DomTools.isElement(target) == false) {
                return false;
            }
        }
        
        
        var r = new Rollover();
        r.setElement(target);
        r.setActivated(activated);
        r.display.normal.background = backgroundValueNormal;
        r.display.normal.foreground = foregroundValueNormal;
        r.display.over.background = backgroundValue;
        r.display.over.foreground = foregroundValue;
        
        
        if (currentState == Rollover.STATE_OUT) {
            r.out();
        } else if (currentState == Rollover.STATE_OVER) {
            r.over();
        }
        
        
        if (onCreateFunction != null) {
            return onCreateFunction(r);
        }
        
        return true;
    });
    
    return true;
}

Rollover.subscribesByClassName = function (className) {
    var onCreateFunction = null;
    var activated = true;
    var currentState = Rollover.STATE_UNKNOWN;
    var foregroundValueNormal = Rollover.DISPLAY_NORMAL_FOREGROUND;
    var backgroundValueNormal = Rollover.DISPLAY_NORMAL_BACKGROUND;
    var foregroundValue = Rollover.DISPLAY_OVER_FOREGROUND;
    var backgroundValue = Rollover.DISPLAY_OVER_BACKGROUND;
    
    switch (arguments.length) {
        case 8 : {
            onCreateFunction = arguments[7];
            
        }
        
        case 7 : {
            activated = (arguments[6] != null) ? arguments[6] : activated;
            
        }
        
        case 6 : {
            foregroundValueNormal = (arguments[5] != null) ? arguments[5] : foregroundValueNormal;
            
        }
        
        case 5 : {
            backgroundValueNormal = (arguments[4] != null) ? arguments[4] : backgroundValueNormal;
            
        }
        
        case 4 : {
            foregroundValue = (arguments[3] != null) ? arguments[3] : foregroundValue;
            
        }
        
        case 3 : {
            backgroundValue = (arguments[2] != null) ? arguments[2] : backgroundValue;
            
        }
        
        case 2 : {
            currentState = (arguments[1] != null) ? arguments[1] : currentState;
            
            if ((currentState != Rollover.STATE_OUT)
                    && (currentState != Rollover.STATE_OVER)) {
                currentState = Rollover.STATE_UNKNOWN;
            }
            
        } break;
        
        
        default : {
            // do nothing
        } break;
        
    }
    
    window.appendLoadEvent(function () {
        var list = _$(className);
        var i = 0;
        var sz = list.length;
        var current = null;
        var img = null;
        
        for (i = 0; i < sz; i++) {
            current = list[i];
            
            Rollover.subscribes(
                current,
                currentState,
                backgroundValue,
                foregroundValue,
                backgroundValueNormal,
                foregroundValueNormal,
                activated,
                onCreateFunction);
            
        }
        
        
        return true;
    });
    
    return true;
}

Rollover.prototype.setElement = function (node) {
    DomTools.addHelpers(node);
    
    node.rugama.set('Rollover', this);
    
    this.element = node;
    
    this.element.appendNewEvent('mouseover',
        function () {
            node.rugama.get('Rollover').over();
            return true;
        })
    
    this.element.appendNewEvent('mouseout',
        function () {
            node.rugama.get('Rollover').out();
            return true;
        })
    
}


Rollover.prototype.appendRolloverListener = function (rolloverEvent) {
    rolloverEvent.owner = this;
    
    if (!rolloverEvent.out) {
        rolloverEvent.out = function () {
            return true;
        }
    }
    
    if (!rolloverEvent.over) {
        rolloverEvent.over = function () {
            return true;
        }
    }
    
    if (!rolloverEvent.roll) {
        rolloverEvent.roll = function () {
            return true;
        }
    }
    
    this.rolloverListeners.push(rolloverEvent);
    
}

Rollover.prototype.removeRolloverListener = function (rolloverEvent) {
    var i = 0;
    var sz = this.rolloverListeners.length;
    
    for (i = 0; i < sz; i++) {
        if (this.rolloverListeners[i] != rolloverEvent) {
            continue;
        }
        
        this.rolloverListeners[i].owner = null;
        delete this.rolloverListeners[i];
    }
    
}

Rollover.prototype.setActivated = function (aBoolean) {
    this.activated = (aBoolean === false) ? false : true;
}

Rollover.prototype.isActivated = function () {
    return this.activated;
}

Rollover.prototype.isOut = function () {
    if (this.element == null) {
        return null;
    }
    
    if (this.currentState == Rollover.STATE_OUT) {
        return true;
        
    } else if (this.currentState == Rollover.STATE_OVER) {
        return false;
        
    }
    
    return (this.isOver() == false) ? true : false;
}

Rollover.prototype.out = function () {
    if (this.element == null) {
        return true;
    }
    
    if (this.isActivated() != true) {
        return true;
    }
    
    var i = 0;
    var sz = this.rolloverListeners.length;
    for (i = 0; i < sz; i++) {
        if (this.rolloverListeners[i].out() === false) {
            return true;
        }
    }
    
    this.currentState = Rollover.STATE_OUT;
    this.element.style.color = this.display.normal.foreground;
    this.element.style.background = this.display.normal.background;
    
    return true;
}

Rollover.prototype.isOver = function () {
    if (this.element == null) {
        return null;
    }
    
    if (this.currentState == Rollover.STATE_OUT) {
        return false;
        
    } else if (this.currentState == Rollover.STATE_OVER) {
        return true;
        
    } else if (this.element.getActualStyle('background-color') == this.display.background) {
        return true;
    }
    
    return false;
}

Rollover.prototype.over = function () {
    if (this.element == null) {
        return true;
    }
    
    if (this.isActivated() != true) {
        return true;
    }
    
    var i = 0;
    var sz = this.rolloverListeners.length;
    for (i = 0; i < sz; i++) {
        if (this.rolloverListeners[i].over() === false) {
            return true;
        }
    }
    
    this.currentState = Rollover.STATE_OVER;
    this.element.style.color = this.display.over.foreground;
    this.element.style.background = this.display.over.background;
    
    return true;
}

Rollover.prototype.roll = function () {
    if (this.element == null) {
        return true;
    }
    
    var i = 0;
    var sz = this.rolloverListeners.length;
    for (i = 0; i < sz; i++) {
        if (this.rolloverListeners[i].roll() === false) {
            return true;
        }
    }
    
    if (this.isOut() == true) {
        this.over();
        
    } else {
        this.out();
        
    }
    
    return true;
}
