/**
 * @author Falko Zander, www.falko-zander.de
 */

var tooltip_collection_ = [];

/**
 * @param id String
 */
function cTooltip(id) {
    this.id = id || '';
    this.elem = null;
    this.open = false;
    this.handler = null;
    tooltip_collection_.push(this);
}

/**
 * @param obj Element
 * @param event Event
 * @param direc String
 * @param direc_hor String
 */
cTooltip.prototype.show = function(obj, event_, direc, direc_hor) {
    if (typeof direc == 'undefined') direc = 'down';
    if (typeof direc_hor == 'undefined') direc_hor = 'right';

    if (this.elem == null) {
        this.elem = $(this.id);
        if (this.elem == null) error('tooltip: element not found');
    }

    var e = this.elem;
    var Ev = IE ? window.event : event_;  
    var THIS = this;

    if (this.open) this.hide();
    
    e.style.display = 'block';
    var d = getAbsoluteDimensions(e);  
    e.style.left = (direc_hor == 'right')
                 ? Ev.clientX - d.parentsLeft + 15 + 'px'
                 : Ev.clientX - d.parentsLeft - e.offsetWidth + 'px';
    e.style.top = (direc == 'down')
                ? Ev.clientY - d.parentsTop + 8 + 'px'
                : Ev.clientY - d.parentsTop - e.offsetHeight + 8 + 'px';
    e.onmouseover = function() { THIS.hide_stop_(); }
    e.onmouseout = function() { THIS.hide_slow(); }
    obj.onmouseout = function() { THIS.hide_slow(); }

    this.open = true;
}

cTooltip.prototype.hide = function() {
    if (this.elem == null) return;
    this.elem.style.display = 'none';
    if (this.handler != null) {
        window.clearTimeout(this.handler);
        this.handler = null;
    }
    this.open = false;
}

cTooltip.prototype.hide_slow = function() {
    var id = this.id;
    this.handler = window.setTimeout("tooltip_hide('" + id + "');", 100);   //$NON-NLS-1$ //$NON-NLS-2$
}

cTooltip.prototype.hide_stop_ = function() {
    if (this.handler != null) {    
        window.clearTimeout(this.handler);
        this.handler = null;
    }
}

/**
 * @param id String
 */
function tooltip_hide(id) {
    for (var i = tooltip_collection_.length - 1; i >= 0; i--) {
        if (tooltip_collection_[i].id == id) {
            tooltip_collection_[i].hide();
        }
    }
}
