// Methods and functions related to notes that appear when mouse hover a glossary term
// There is only one note object, call CurrentNote which is always updated. 
// Calling notes within a note (which would imply an array of notes) does not work: the browser
// does not correctly compute the position of the "target" object within the note
// Pre-requisite: there must be a DIV zone in the page called noteContainer

//////////////
// Global variables
//////////////
// Current Note object
var CurrentNote;

//////////////
// Methods
//////////////
// Computes the position that the receiver Note object should take on X or Y
// axis is "x" or "y"
function getNotePos(axis){
  var windowEdgePos;
  var targetObj = this.target;
  var tooltipObj = getObject("noteContent");
  var resPos;
  var heightOffset = 2;

  if (!tooltipObj || !targetObj)
    return;

  // First get the position of the target object 
  targetPos = getObjAbsPos(targetObj, axis);
  // As a first approximation, the tooltip is aligned on the left of the target object
  // and below it
  if (axis == "x")
    tooltipPos = targetPos;
  else
    tooltipPos = targetPos + heightOffset + targetObj.offsetHeight;
  
  // Compute position of edge of window (on x or y axis): 
  // scroll + window width/eight - 15 (for scrollbar)
  windowEdgePos = getScroll(axis) + getWindowDim(axis) - 15;

  // Test if window right/bottom edge is on the left/above the right/bottom edge of 
  // the tooltip rectangle
  if (axis == "x"){
    if (windowEdgePos < tooltipPos + tooltipObj.offsetWidth)
      // Yes it is on the left! So we will align the tooltip on the right of the window
      tooltipPos = windowEdgePos - tooltipObj.offsetWidth;
  }else{           // Y axis
    if (windowEdgePos < tooltipPos + tooltipObj.offsetHeight)
      // Yes it is below the bottom of the window. Align above the target
      tooltipPos = targetPos - tooltipObj.offsetHeight - heightOffset;
  }

  return tooltipPos;
}

// Hide a Note object, the receiver of the method
function hideNote(){
  var tooltipObj;

  tooltipObj = getObject("noteContent");
  if (tooltipObj)
    tooltipObj.style.visibility = "hidden";
}


//////////////
// CONSTRUCTOR
//////////////
// Attributes of class Note:
// - text: text of the note (can be HTML)
// - target: DOM object (event receiver), on which the mouse hovers and which causes the note to appear
function Note(text, target){
  var nodeObj;
  var noteContainer;

  //// Attributes 
  this.text = text;
  this.target = target;

  // Assign onmouseout event handler for target object:
  target.onmouseout = hideNoteAfterTimeOut;

  // Create note content block, hidden at the present time
  noteContainer = getObject("noteContainer");
  if (noteContainer){
    // Set the initial position at (-500, -500): this is to avoid a flashing effect
    noteContainer.innerHTML = "<div id=\"noteContent\" class=note style=\"left: -500px; top: -500px; visibility:hidden; z-index:20;\" onMouseover=\"clearTimeOut()\" onMouseout=\"hideNoteAfterTimeOut()\">" + this.text + "</div>";
  }

  //// Methods
  this.getNotePos = getNotePos;
  this.hideNote = hideNote;
}


//////////////
// FUNCTIONS
//////////////

// Hide note after a timeout has expired
function hideNoteAfterTimeOut(){
  hideTimeOutFct = setTimeout("hideNoteHandler()", 200);
}

// Function called by setTimeout - event is passed as an argument, but not used
function hideNoteHandler(evt){
  CurrentNote.hideNote();
}

// Clear timeout 
function clearTimeOut(){
  if (typeof hideTimeOutFct!="undefined")
    clearTimeout(hideTimeOutFct);
}

// Displays a note as a tooltip
// passedEvent: Javascript event, as passed by calling function in Netscape browsers
// noteText: text of the note (supports HTML)
function showNote(passedEvent, noteText){
  var evt;
  var targetObj;
  var noteObj;
  var tooltipObj;
  var obj1;

  // Clear time out, in case we come from another note, with a pending time out
  clearTimeOut();

  // Compute cross-browser event
  evt = getEvent(passedEvent);
  // Compute DOM object receiving event
  targetObj = getTarget(evt);

  // First hide previous note
  if (CurrentNote != null)
    CurrentNote.hideNote();

  // Create a Note object
  CurrentNote = new Note(noteText, targetObj);

  // Show note at right position
  tooltipObj = getObject("noteContent");
  if (tooltipObj){
    // Now update the coordinates of the note:
    tooltipObj.style.left = CurrentNote.getNotePos("x") + "px";
    tooltipObj.style.top = CurrentNote.getNotePos("y") + "px";
    // Show note
    tooltipObj.style.visibility = "visible";
  }
}


