var MouseX = 0;
var MouseY = 0;

window.onload	= SaveMouseXY;
setTimeout("document.onmousemove = SaveMouseXY;", 500);
window.onscroll	= SaveMouseXY;

function SaveMouseXY (e) {
  MouseX = (e ? e.pageX : ((document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft) + window.event.clientX);
  MouseY = (e ? e.pageY : ((document.documentElement && document.documentElement.scrollTop)  || document.body.scrollTop)  + window.event.clientY);
}

var BrowserWidth = 0;
var ScrollLeft = 0;
var BrowserHeight = 0;
var ScrollTop = 0;

function ShowInfoBox (HTMLObject, YOffset, XOffset, WindowWidth, WindowHeight, MaybeStayRight) {
  Obj = HTMLObject.id + '_InfoBox';
  document.getElementById(Obj).style.visibility = "visible";

  if (! WindowWidth ) WindowHeight = 0;
  if (! WindowHeight) WindowHeight = 0;

  var PosLeft = MouseX + 20 + (XOffset ? XOffset : 0);
  var PosTop  = MouseY + YOffset;

  BrowserWidth = window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.offsetWidth;
  BrowserHeight = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.offsetHeight;
  ScrollLeft = window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft;
  ScrollTop = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;

  if (BrowserWidth - (MouseX-ScrollLeft+XOffset+40) < WindowWidth) {
    if (MaybeStayRight)
      PosLeft = Math.min(PosLeft,BrowserWidth+ScrollLeft-WindowWidth-20)
    else
      PosLeft = Math.max(PosLeft - WindowWidth-25, ScrollLeft);
  }
  else {
    PosLeft = Math.min(PosLeft,BrowserWidth+ScrollLeft-WindowWidth-20);
  }
  document.getElementById(Obj).style.left = PosLeft+'px';

  if (BrowserHeight - (MouseY-ScrollTop+YOffset+20) < WindowHeight) {
    PosTop = Math.max(PosTop - WindowHeight-30, ScrollTop);
  }
  else {
    PosTop  = Math.min(PosTop,BrowserHeight+ScrollTop-WindowHeight);
  }
  document.getElementById(Obj).style.top = PosTop+'px';
}

function HideInfoBox (HTMLObject) {
  document.getElementById(HTMLObject.id + '_InfoBox').style.visibility = "hidden";
}

function GetOffset (HTMLObject) {
  Pos = new Array(0, 0);
  while (HTMLObject) {
    Pos[0] += HTMLObject.offsetLeft;
    Pos[1] += HTMLObject.offsetTop;
    HTMLObject = HTMLObject.offsetParent;
  }
  return Pos;
}

