
function getWindowWH() {
    if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      winW = window.innerWidth-20;
      winH = window.innerHeight-20;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth-20;
      winH = document.body.offsetHeight-20;
     }
    }
    return [winW, winH];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function xstooltip_findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function xstooltip_findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function xstooltip_show(tooltipId, parentId, posX, posY) {
    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        parent = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > parent.offsetWidth) posX = parent.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        x = xstooltip_findPosX(parent) + posX;
        y = xstooltip_findPosY(parent) + posY;

        winWH = getWindowWH();
        winW = winWH[0];
        winH = winWH[1];
        ScrollXY = getScrollXY();
        scrOfX = ScrollXY[0];
        scrOfY = ScrollXY[1];

        width = parseInt(it.style.width.substring(0,it.style.width.indexOf('p')))
        height = parseInt(it.style.height.substring(0,it.style.height.indexOf('p')))

        //alert('x: '+x+'\nwidth: '+width+'\nx + width: '+(x + width)+'\nwinW: '+winW+'\nscrOfX: '+scrOfX+'\nwinW + scrOfX: '+(winW + scrOfX)+'\nwinW + scrOfX - width: '+(winW + scrOfX - width)+'\ny: '+y+'\nheight: '+height+'\ny + height: '+(y + height)+'\nwinH: '+winH+'\nscrOfY: '+scrOfY+'\nwinH + scrOfY: '+(winH + scrOfY)+'\nwinH + scrOfY - height: '+(winH + scrOfY - height))
        if(x + width > winW + scrOfX) {
            x = winW + scrOfX - width - 8
            if(x<0) x = 0
        }
        if(y + height > winH + scrOfY) {
            y = winH + scrOfY - height - 8
            if(y<0) y = 0
        }

        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    
    it.style.visibility = 'visible'; 
}

function xstooltip_hide(id) {
    it = document.getElementById(id); 
    it.style.visibility = 'hidden'; 
}

function xstooltip_hide_all(id_prefix) {
    if(typeof id_prefix != 'string' || id_prefix=='')
        return;
    divs = document.getElementsByTagName("div")
    for(var i=0;i<divs.length;i++)
        if(divs[i].id.substr(0,id_prefix.length) == id_prefix)
            divs[i].style.visibility = 'hidden'; 
}function mouseX(evt) {
    if (evt.pageX)
        return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
            document.documentElement.scrollLeft :
            document.body.scrollLeft);
    else return null;
}

function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
            document.documentElement.scrollTop :
            document.body.scrollTop);
    else return null;
}
