function CToolTip(instanceName,contentFunc,toolTipWidth,defaultToLeft) {
    this.toolTipWidth = toolTipWidth;
    this.defaultToLeft = defaultToLeft;
    this.instanceName = instanceName;
    this.contentFunc = contentFunc;
    this.mouseoverTimer = null;
    this.mouseoutTimer = null;
    this.showToolTipDelay = 700;
    this.hideToolTipDelay = 700;
    this.createToolTipDiv();
}

CToolTip.prototype.createToolTipDiv = function () {
    this.ttDiv = document.createElement('div');
    this.ttDiv.id = 'ctooltip';
    this.ttDiv.className = 'ctooltip';
    this.ttDiv.style.visibility = 'hidden';
    this.ttDiv.style.position = 'absolute';
    this.ttDiv.style.zIndex = '3';
    this.ttDiv.style.width = this.toolTipWidth+'px';
    this.ttDiv.controller = this;
    document.body.appendChild(this.ttDiv);
}

CToolTip.prototype.findPos = function (obj) {
    var curleft = 0;
    var curtop = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
        curtop += obj.y;
    }

    return {'x':curleft,'y':curtop};
}

CToolTip.prototype.delayedShowToolTip = function (obj,content) {
    this.obj = obj;
    this.content = content;
    this.mouseoverTimer = setTimeout(this.instanceName+'.showToolTip()',this.showToolTipDelay);
}

CToolTip.prototype.showToolTip = function (obj,content) {
    clearTimeout(this.mouseoutTimer);

    if (!obj) {
        obj = this.obj;
        this.obj = null;
    }
    
    if (!content) {
        content = this.content;
        this.content = null;
    }
    
    if (!this.ttDiv) {
        return false;
    }
    
    if (this.contentFunc) {
        content = this.contentFunc(content);
    }
    
    if (!content) {
        return;
    }

    this.ttDiv.innerHTML = content;
    this.ttDiv.style.width = this.toolTipWidth+'px';
    ttWidth = this.toolTipWidth;

    var objCoords = this.findPos(obj);
    var coords = {'x':objCoords.x,'y':(objCoords.y+obj.offsetHeight)};

    var distToRightEdge = document.body.clientWidth - coords.x - 10;
    var distToLeftEdge = coords.x - 10;
    var canShowRight = (distToRightEdge >= ttWidth);
    var canShowLeft = (ttWidth < coords.x);

    var showSide = (this.defaultToLeft ? 'l' : 'r');
    
    if (!this.defaultToLeft && !canShowRight) {
        showSide = 'l';
    } else if (this.defaultToLeft && !canShowLeft) {
        showSide = 'r';
    }
    
    if (showSide == 'r') {
        if (!canShowRight) {
            this.ttDiv.style.width = distToRightEdge+'px';
        }
        this.ttDiv.style.top = coords.y+'px';
        this.ttDiv.style.left = coords.x+'px';
    } else if (showSide == 'l') {
        this.ttDiv.style.top = coords.y+'px';
        this.ttDiv.style.left = (coords.x + obj.offsetWidth - ttWidth)+'px';
    }

    this.ttDiv.onmouseover = function () {
        clearTimeout(this.controller.mouseoutTimer);
    };

    this.ttDiv.onmouseout = function () {
        this.controller.delayedHideToolTip();
    };
    
    this.ttDiv.style.visibility = 'visible';
}

CToolTip.prototype.delayedHideToolTip = function () {
    if (this.mouseoverTimer) {
        clearTimeout(this.mouseoverTimer);
        this.mouseoverTimer = null
    }
    this.mouseoutTimer = setTimeout(this.instanceName+'.hideToolTip()',this.hideToolTipDelay);
}

CToolTip.prototype.hideToolTip = function () {
    if (!this.ttDiv) {
        return false;
    }

    this.ttDiv.style.visibility = 'hidden';
    this.ttDiv.innerHTML = '';
}
