// requires events.js

//this is required to callback functions. read http://www.snook.ca/archives/javascript/javascript_pass/ for more info.
if ( ! Function.prototype.bind ) {
    Function.prototype.bind = function( object ) {
        var __method = this;
        return function() {
            __method.apply( object, arguments );
        };
    };
}

function InfoPopup(trigger, popupBox) {
    this.trigger = trigger;
    this.popupBox = popupBox;
    this.timeoutId = 0;
    this.timeout = 200;
    
    //if (debug) debug.writeLine("InfoPopup: this.trigger: "+this.trigger);
    //if (debug) debug.writeLine("InfoPopup: this.popupBox: "+this.popupBox);
               
    InfoPopup.prototype.show = function () {
        //if (debug) debug.writeLine("InfoPopup.show: this.trigger: "+this.trigger);
        //if (debug) debug.writeLine("this.popupBox.style.display: "+this.popupBox.style.display);
        clearTimeout(this.timeoutId);
        if (!this.popupBox)
            return;
        if (this.popupBox.style.display=="none" || this.popupBox.style.display=="") {
            //if (debug) debug.writeLine("this.trigger: "+this.trigger);
            this.popupBox.style.display = "block";
        }
    }
    
    InfoPopup.prototype._close = function () {
        this.timeoutId = 0;
        if (!this.popupBox)
            return;
        this.popupBox.style.display="none";
        //if (debug) debug.writeLine("InfoPopup._close: "+this.trigger);
    }
    
    InfoPopup.prototype._timedClose = function () {
        if (!this.popupBox)
            return;
        if (this.popupBox.style.display!="none") {
            this.timeoutId = setTimeout(this._close.bind(this), this.timeout);
            //if (debug) debug.writeLine("InfoPopup._timedClose");
        }
    }
    
    Events.attachEvent(this.trigger, "mouseover", this.show.bind(this));
    
    if (this.popupBox)
        Events.attachEvent(this.popupBox, "mouseover", this.show.bind(this));
    
    Events.attachEvent(this.trigger, "mouseout", this._timedClose.bind(this));
    
    if (this.popupBox)
        Events.attachEvent(this.popupBox, "mouseout", this._timedClose.bind(this));
}

