var isIE = (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Opera") == -1);
var hint = null;
var frameIE = null;
var hintIE = null;
var lastShow = 0;
var showDelay = 3000;
var intervalId = -1;
var callerElement = null;
var visible = false;

function installHint(cssStyle, width, delayed) {
    var hintDiv = document.createElement('DIV');
    hintDiv.className = cssStyle;
    hintDiv.style.width = width;
    hintDiv.style.position = 'absolute';
    hintDiv.style.visibility = 'hidden';
    hintDiv.style.left = -10000;
    hintDiv.style.top = -10000;
    hintDiv.style.zIndex = 10000;

    var body = document.getElementsByTagName('BODY')[0];
    body.appendChild(hintDiv);
    hint = hintDiv;

    if (isIE) {
        hintDiv.className = '';
        hintDiv.innerHTML = '<iframe id="hintFrame" name="hintFrame" src="javascript:\'\'" scrolling="no" marginheight="0" marginwidth="0" frameborder="1" width="100%" height="100%"></iframe>';
        frameIE = hintDiv.getElementsByTagName("IFRAME")[0];
        var hintDivIE = document.createElement('DIV');
        hintDivIE.className = cssStyle;
        hintDivIE.style.width = width;
        hintDivIE.style.position = 'absolute';
        hintDivIE.style.visibility = 'hidden';
        hintDivIE.style.left = -10000;
        hintDivIE.style.top = -10000;
        hintDivIE.style.zIndex = 10001;
        body.appendChild(hintDivIE);
        hintIE = hintDivIE;
    }
    if (delayed) {
        restartInterval();
    }
}

function showHint(caller, html, cssStyle, width, delayed) {
    if (hint == null) {
        installHint(cssStyle, width, delayed);
    } else {
        hideHint();
    }
    var x = caller.offsetLeft;
    var y = caller.offsetTop;
    var w = caller.offsetWidth;
    var h = caller.offsetHeight;

    var element = caller;
    while (element.tagName != 'BODY') {
        element = element.offsetParent;
        x += element.offsetLeft;
        y += element.offsetTop;
    }
    if (hint) {
	 
        //if (x > document.body.clientWidth / 2) {
            hint.style.left = x - hint.offsetWidth + 10; //-10
        //} else {
        //    hint.style.left = x + w + 10;
        //}
        if ((isIE) && (hintIE != null) && (frameIE != null)) {
            hintIE.innerHTML = html;
            hintIE.style.left = hint.style.left;
            hintIE.style.top = y - (- h / 2 ); // hintIE.offsetHeight/2 -  h / 2
            hint.style.top = hintIE.style.top;
            hint.style.height = hintIE.offsetHeight;
            frameIE.style.height = hintIE.offsetHeight;
            hintIE.style.visibility = 'visible';
        } else {
            hint.innerHTML = html;
            hint.style.top = y - (- h / 2);  // hintIE.offsetHeight/ 2 -  h / 2
        }
        hint.style.visibility = 'visible';
    }
    if (delayed) {
        lastShow = new Date().getTime();
        var el = hint;
        if (isIE) {
            el = hintIE;
        }
        el.onmouseover = releaseInterval;
        el.onmouseout = restartInterval;
    }
    callerElement = caller;
    visible = true;
}

function hideHint() {
    if (hint != null) {
        hint.style.visibility = 'hidden';
    }
    if ((isIE) && (hintIE != null)) {
        hintIE.style.visibility = 'hidden';
    }
    if ((callerElement) && (callerElement.hintHiding)) {
        callerElement.hintHiding();
    }
    visible = false;
    releaseInterval();
}

function hideHintAutomatically() {
    var currentTime = new Date().getTime();
    if ((visible) && (currentTime > lastShow + showDelay)) {
        hideHint();
    }
}

function releaseInterval() {
    clearInterval(intervalId);
}

function restartInterval() {
    lastShow = new Date().getTime();
    intervalId = setInterval('hideHintAutomatically()', 1000);
}