function winWidth() {
    if (window.innerWidth) {
        return window.innerWidth - 18;  //scrollbar
    } else if (document.documentElement && document.documentElement.clientWidth) {
        return document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    } else {
        return false;
    }
}
  
function winHeight() {
    if (window.innerHeight) {
        return window.innerHeight - 18;  //scrollbar
    } else if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight; 
    } else {
        return false;
    }
}
  
function winScrollX() {
    if (typeof window.pageXOffset == 'number') {
        return window.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        return document.documentElement.scrollLeft;
    } else if (document.body && document.body.scrollLeft) {
        return document.body.scrollLeft;
    } else if (window.scrollX) {
        return window.scrollX;
    } else {
        return false;
    }
}
  
function winScrollY() {
    if (typeof window.pageYOffset == 'number') {
        return window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        return document.body.scrollTop;
    } else if (window.scrollY) {
        return window.scrollY;
    } else {
        return false;
    }
}

var Tooltip = {
    offX: 8,
    offY: 12,
    tipID: 'tooltip',
    showDelay: 100,
    hideDelay: 0,
    timer: null,
    tip: null,

    init: function() {
        if (document.createElement && document.body && typeof document.body.appendChild != 'undefined' && !document.getElementById(this.tipID)) { 
            var el = document.createElement('div');
            el.id = this.tipID;
            document.body.appendChild(el);
        }
    },

    show: function(e, msg) { 
        if (this.timer) { 
            clearTimeout(this.timer);
            this.timer = 0;
        }
        this.tip = document.getElementById(this.tipID);
        if (document.addEventListener) document.addEventListener('mousemove', this.trackMouse, true);
        else if (document.attachEvent) document.attachEvent('onmousemove', this.trackMouse);
        this.writeTip(msg);
        this.positionTip(e);
        this.timer = setTimeout('Tooltip.toggleVis(\'' + this.tipID + '\', \'visible\')', this.showDelay);
    },

    writeTip: function(msg) {
        if (this.tip && typeof this.tip.innerHTML != 'undefined') this.tip.innerHTML = msg;
    },

    positionTip: function(e) {
        if (this.tip && this.tip.style) {
            var x = e.pageX ? e.pageX : e.clientX + winScrollX();
            var y = e.pageY ? e.pageY : e.clientY + winScrollY();
            if (x + this.tip.offsetWidth + this.offX > winWidth() + winScrollX()) {
                x = x - this.tip.offsetWidth - this.offX;
                if (x < 0) x = 0;
            } else x = x + this.offX;
            if (y + this.tip.offsetHeight + this.offY > winHeight() + winScrollY()) {
                y = y - this.tip.offsetHeight - this.offY;
                if (y < winScrollY()) y = winHeight() + winScrollY() - this.tip.offsetHeight;
            } else y = y + this.offY;
            this.tip.style.left = x + 'px';
            this.tip.style.top = y + 'px';
        }
    },

    hide: function() {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = 0;
        }
        this.timer = setTimeout('Tooltip.toggleVis(\'' + this.tipID + '\', \'hidden\')', this.hideDelay);
        if (document.removeEventListener) document.removeEventListener('mousemove', this.trackMouse, true);
        else if (document.detachEvent) document.detachEvent('onmousemove', this.trackMouse);
        this.tip = null;
    },

    toggleVis: function(id, vis) {
        var el = document.getElementById(id);
        if(el) el.style.visibility = vis;
    },

    trackMouse: function(e) {
        e = e ? e: window.event;
        e.tgt = e.srcElement? e.srcElement: e.target;    
        if (!e.preventDefault) e.preventDefault = function () { return false; }
        if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        Tooltip.positionTip(e);
    }
};

Tooltip.init();