var offsetfromcursorX = 0; //16;
var offsetfromcursorY = 0; //10;
var offsetdivfrompointerX = 15;
var offsetdivfrompointerY = 14;



function ddrivetip(e, thetext, evt, thewidth, thecolor) {
    var tipobj = createIf("dhtmltooltip");
    var pointerobj = createIf("dhtmlpointer");
    if (typeof thewidth != "undefined") tipobj.style.width = thewidth + "px";
    if (typeof thecolor != "undefined" && thecolor != "") tipobj.style.backgroundColor = thecolor;
    tipobj.innerHTML = thetext;
    
    var nondefaultpos = false;
    var curX, curY, winwidth, winheight, rightedge, bottomedge;

    //alert(document.body.scrollLeft);

    if (ivy.browser.isIE) {
        var tb = document.body;
        curX = evt.clientX - evt.offsetX + e.offsetWidth;
        curY = evt.clientY - evt.offsetY + e.offsetHeight;
        winwidth = tb.clientWidth;
        winheight = tb.clientHeight;
        rightedge = winwidth - evt.clientX - offsetfromcursorX;
        bottomedge = winheight - evt.clientY - offsetfromcursorY;
    } else {
        curX = evt.clientX;
        curY = evt.clientY;
        winwidth = window.innerWidth - 20;
        winheight = window.innerHeight - 20;
        rightedge = winwidth - e.clientX - offsetfromcursorX;
        bottomedge = winheight - e.clientY - offsetfromcursorY;
    }

    var leftedge = ((offsetfromcursorX < 0) ? offsetfromcursorX*(-1) : -1000);
    if (rightedge < tipobj.offsetWidth) {
        tipobj.style.left = curX - tipobj.offsetWidth + "px";
        nondefaultpos = true;
    } else if (curX < leftedge) {
        tipobj.style.left = "5px";
    } else {
        tipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px";
        pointerobj.style.left = curX + offsetfromcursorX + "px";
    }

    if (bottomedge < tipobj.offsetHeight) {
        tipobj.style.top = curY - tipobj.offsetHeight - offsetfromcursorY + "px";
        nondefaultpos = true;
    } else {
        tipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px";
        pointerobj.style.top = curY + offsetfromcursorY + "px";
    }
    tipobj.style.visibility = "visible";
    pointerobj.style.visibility = (!nondefaultpos ? 'visible' : 'hidden');
}

function hideddrivetip(tooltip) {
    var tipobj = createIf("dhtmltooltip");
    var pointerobj = createIf("dhtmlpointer");
    tipobj.style.visibility = "hidden";
    pointerobj.style.visibility = "hidden";
    tipobj.style.left = "-1000px";
    tipobj.style.backgroundColor = '';
    tipobj.style.width = '';
}

function getChildNodeById(o, name) {
    for (var i = 0; i < o.childNodes.length; i++) {
        if (o.childNodes[i].id == name) return o.childNodes[i];
        var c = getChildNodeById(o.childNodes[i], name);
        if (c != null) return c;
    }
}

function getParentNode(o, name) {
    if (o != null && o.id != name) return getParentNode(o.parentNode, name);
    if (o.id == name) return o;
}

function calcTill(e, dir, className) {
    if (e == null) return 0;
    var v = (dir == 'left' ? e.offsetLeft : e.offsetTop);
    if (e.className == className) return v;
    return v + calcTill(e.parentNode, dir, className);
}

function createIf(id) {
    var o = document.getElementById(id);
    if (o != null) return o;
    o = document.createElement('DIV');
    o.id = "dhtmltooltip";
    p = document.createElement('IMG');
    p.id = 'dhtmlpointer';
    p.src = 'gfx/tooltip_arrow.gif';
    document.body.appendChild(o);
    document.body.appendChild(p);
    return document.getElementById(id);
}
