// Calculate the 'position' of an element including scroll areas, etc
function helper_calculateOffset(startObj)
{
  var loopObj = startObj;
  var tmpTop = 0;
  var tmpLeft = 0;

  // Determine the offset due to scroll
  if (document.body) {
    tmpLeft += document.body.scrollLeft;
    tmpTop += document.body.scrollTop;
  }
  else {
    tmpLeft += window.pageXOffset;
    tmpTop += window.pageYOffset;
  }
 
  // Add up the offsets
  while (loopObj.offsetParent)
  {
    tmpLeft += loopObj.offsetLeft;
    tmpTop += loopObj.offsetTop;
    loopObj = loopObj.offsetParent;
  }

  // Remove any scroll-induced issues
  loopObj = startObj;
  while (loopObj.parentNode)
  {
    tmpTop -= loopObj.scrollTop;
    loopObj = loopObj.parentNode;
  }
   
  return {'left':tmpLeft, 'top':tmpTop};
}


// Provide the mouse position in globally accessible variables: curMouseX, curMouseY
  var tracking = 0;
  helper_trackMouseLocation();

  function helper_curMouseX() { if (tracking == 0) {helper_trackMouseLocation();} return cmx; }
  function helper_curMouseY() { if (tracking == 0) {helper_trackMouseLocation();} return cmy; }

  function helper_trackMouseLocation() {
        if (document.captureEvents) {
          cmx = 0;
          cmy = 0;
          tracking = 1;

          document.captureEvents(Event.MOUSEMOVE);
          document.onmousemove  = function(e) {
                cmx = e.clientX + window.pageXOffset;
                cmy = e.clientY + window.pageYOffset;
          }
        } else {
          tracking = -1;
        }
  }


// Fade objects in and out

  var fdata = new Array();
  
  function helper_fadeObject(objId, opacity, fd)
  {
    // Get the object
    object = document.getElementById(objId);

    // Drop out if the object doesn't exist.
    if (object == null ) { alert('attempting to fade nonexistant object');return false; }

    // Set display property IF this is a 'start' command.
    if (fd != null && fd > 0) {
      object.style.display = 'block';
    }

    // Create new fader data for this object, if it doesn't have any.
    if (fdata[objId] == null)
    {
      fdata[objId] = new Array();
      fdata[objId]['opacity'] = opacity;
      fdata[objId]['delta'] = fd;
      fdata[objId]['done'] = false;
    }
    else 
    {
      // If we're currently fading this object, then we just switch the fade direction and return;
      if (fd != null) {
	fdata[objId]['delta'] = fd;
	if (fdata[objId]['done'] == false) {
	  return false;
	}
      } 
    }

    // Set the current fade level
    object.style.opacity = opacity;
    object.style.filter = 'alpha(opacity=' + opacity * 100 + ')';

    if (  ((opacity < 1 && fdata[objId]['delta'] > 0) ||
 	   (opacity > 0 && fdata[objId]['delta'] < 0)))
    {  
      // Set the new opacity level.
      var newOpacity = opacity + fdata[objId]['delta'];
      fdata[objId]['done'] = false;
      
      // Create a function call string based on new values
      var fn = 'helper_fadeObject("' + objId + '",' + newOpacity + ')';
      
      // Recursively call this function to continue fading.
      window.setTimeout(fn, 50);
    } else {
      // Set the entry to done.
      fdata[objId]['done'] = true;
      
      // If we're "done", and we're fading OUT, then we hide the object altogether.
      if (opacity < 0) {
        object.style.display = 'none';
      }
    }
  }
