var notes = [];
var noteVert          =   5;
var noteHorz          =   5;
var noteOffset        =  20;
var noteDefaultHeight = 100;
var noteDefaultWidth  = 150;
var msg;
/*
 * This function is used to open a simple supplemental window for
 * notes that doesn't require the full heading of a lvl-2 window.
 * It's used for small explanations.
 * The size of the note windows is read in the onload() method.
 */

function showNotes (insertIndx) {
  for (var i = 0; i < this.notes.length; i++) {
    if (i == insertIndx) continue;
    if (this.notes[i]) this.notes[i].focus();
  }
}

function openNote (theURL) {
  
  // be sure theURL is a string
  if ((typeof theURL) != "string") {
    alert ("Warning: theURL (" + theURL + ") is not string");
    return;
  }
  // extract the file name from theURL
  var fileName1 = theURL.substring (theURL.lastIndexOf ("/") + 1);
  this.msg = "fileName1: " + fileName1;
  /* 
   * next check that theURL isn't already open
   * if it is, return.
   * note: closed windows are bypassed here, and delt with next
   */
  for (var i = 0; i < this.notes.length; i++) {
    this.msg += "\n loop1: " + i;
    if (notes[i].closed) {
      this.msg+=" continuing.";
      continue;
    }
    var slashIndx = this.notes[i].location.href.lastIndexOf ("/");
    if (slashIndx <= 0) {
      alert ("Warning: location.href has no /");
      return;
    }
    var slashIndex = this.notes[i].location.href.lastIndexOf ("/");
    var fileName2 = this.notes[i].location.href.substring(slashIndex+1);
    this.msg += "\n fileName2: " + fileName2;
    if (fileName1 == fileName2) {
      this.msg += "\n fileName1 == fileName2: ";
      this.showNotes(i);
      this.notes[i].focus();
      return;
    }
  }
  
  // next remove any closed windows from the END of this.notes
  for (var j = (this.notes.length - 1); j >=0; j--) {
    if (this.notes[j].closed) {
      this.notes.splice (j,1);
    } else {
      break;
    }
  }
  /*
   *  next see if there is any element holds a closed window.
   *  if so use its index.
   */
  var insertIndx = -1;
  for (i = 0; i < notes.length; i++) {
    this.msg += "\n loop2: " + i;
    if (notes[i].closed) {
      this.msg += " insertIndx: " + i;
      insertIndx = i;
    }
  }
  // if no element holds a closed window, add an element
  if (insertIndx == -1) {
    this.msg += "\n no closed window: insertIndx: " + this.notes.length;
    insertIndx = this.notes.length;
  }
  // the new window will be put in this.notes[indx]
  var cnrV = noteVert     + (insertIndx * noteOffset);
  var cnrH = noteHorz     + (insertIndx * noteOffset);
  var name = "table" + notes.length;
  var styleStr  = "toolbar=no, scrollbars=yes, resizable=yes, fullscreen=no, location=no";
  styleStr      += ", width=" + this.noteDefaultWidth  + ", height=" + noteDefaultHeight;
  styleStr      += ", top="   + cnrV + ", left="   + cnrH;
  var w = open (theURL, name, styleStr);
  notes[insertIndx] = w;
  this.msg += "\ncalling showNotes()";
  this.showNotes (insertIndx);
  w.focus();
}

