var offsetxpoint = -60;
var offsetypoint = 20;
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var enabletip = false;
var tipobjId = 'tooltip_object';
var tipobj = $(tipobjId);
if (tipobj == undefined) {
  tipobj = new Element('div', {id : tipobjId });
  $('popupContainer').appendChild(tipobj);
}

function showTooltip(text, className)
{
  if (ns6 || ie) {
    tipobj.className = className;
    tipobj.innerHTML = text
    enabletip = true;
    return false;
  }
}

function positionTooltip(e)
{
  if (enabletip) {
    var scrollOffsets = document.viewport.getScrollOffsets();
    var curX = (ns6) ? e.pageX : event.clientX + scrollOffsets.left;
    var curY = (ns6) ? e.pageY : event.clientY + scrollOffsets.top;
    
    // Find out how close the mouse is to the corner of the window
    var rightedge=ie&&!window.opera? $('body').clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
    var bottomedge=ie&&!window.opera? $('body').clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;
    
    var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
    
    // If the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge < tipobj.offsetWidth) {
      // Move the horizontal position of the menu to the left by it's width
      tipobj.setStyle({
        left: ie? $('body').scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
      });
    } else if (curX < leftedge) {
      tipobj.setStyle({
        left: "5px"
      });
    } else {
      // Position the horizontal position of the menu where the mouse is positioned
      tipobj.setStyle({
        left: curX + offsetxpoint + "px"
      });
    }
    
    // Same concept with the vertical position
    if (bottomedge < tipobj.offsetHeight) {
      tipobj.setStyle({
        top: ie ? $('body').scrollTop + event.clientY - tipobj.offsetHeight - offsetypoint + "px" : window.pageYOffset + e.clientY - tipobj.offsetHeight - offsetypoint + "px"
      });
    } else {
      tipobj.setStyle({
        top: curY + offsetypoint + "px",
        visibility: 'visible'
      });
    }
  }
}

function hideTooltip()
{
  if (ns6 || ie) {
    enabletip = false;
    tipobj.setStyle({
      visibility: "hidden",
      left: '-1000px'
    });
  }
}

// Register the tooltip reposition funciton
document.onmousemove = positionTooltip;

