var sideWindows          =  [];
var sideWindowVert       =    5;
var sideWindowHorz       =    5;
var sideWindowOffset     =   20;
var sideWindowDefaultHeight = 650;
var sideWindowDefaultWidth  = 700;

function getHeight (name) {
  if (this.fileNames[name]) {
    return this.fileNames[name]['height'];
  } else {
    return this.sideWindowDefaultHeight;
  }
}

function getWidth (name) {
  if (this.fileNames[name]) {
    return this.fileNames[name]['width'];
  } else {
    return this.sideWindowDefaultWidth;
  }
}

//-- --------------------------openSideWindow (theURL)-------------------------- --
//-- The function is used to open a supplemental window in which the user can       --
//-- display supplemental information like the lexicon of definitions. For some     --
//-- reason, once a window is opened and then closed, 'typeof' will return          --
//-- 'undefined' even though 'undefined' is not a value 'typeof' is supposed to     --
//-- The array is used to be sure the windows are opened in a tiled pattern.  So    --
//-- when called, it checks to see if any postion in the array holds a closed       --
//-- window.  If so, it opens the next widow in the closed slot.  If the array is   --
//-- empty or if all the windows in it are still open, it will open the new window  --
//-- at the end of the array.                                                       --
//-- ------------------------------------------------------------------------------ --
function openSideWindow (theURL) {
  var msg ="openSideWindow(" + 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);
  msg += "  fileName1: " + fileName1; 
  // next check that theURL isn't already open
  for (var i = 0; i < this.sideWindows.length; i++) {
    if (sideWindows[i].closed) continue;
    var slashIndx = this.sideWindows[i].location.href.lastIndexOf ("/");
    if (slashIndx <= 0) {
      alert ("Warning: location.href has no /");
      return;
    }
    var slashIndex = this.sideWindows[i].location.href.lastIndexOf ("/");
    var fileName2 = this.sideWindows[i].location.href.substring(slashIndex);
    msg += "  fileName2: [" + i + "] " + fileName2; 
    if (fileName1 == fileName2) {
      msg += "  window already open."; 
      alert (msg);
      return;
    }
  }
  /* next remove any closed windows from the END of this.sideWindows */
  for (var j = (this.sideWindows.length - 1); j >=0; j--) {
    if (this.sideWindows[j].closed) {
      this.sideWindows.splice (j,1);
    } else {
      break;
    }
  }
  /* next see if there is any element that holds a closed window. if so use its index. */
  var insertIndx = -1;
  for (i = 0; i < sideWindows.length; i++) {
    if (sideWindows[i].closed) {
      insertIndx = i;
    }
  }
  /* if no element holds a closed window, add an element */
  if (insertIndx == -1) {
    insertIndx = this.sideWindows.length;
  }
  /* the new window will be put in this.sideWindows[indx] */ 
  /* scan this.sideWindows[] for an element that holds a closed window.*/
  var finalWidth = this.getWidth (fileName1);
  var finalHeight = this.getHeight (fileName1);
  var cnrV = sideWindowVert     + (insertIndx * sideWindowOffset);
  var cnrH = sideWindowHorz     + (insertIndx * sideWindowOffset);
  var name = "table" + sideWindows.length;
  var styleStr  = "toolbar=no, scrollbars=yes, resizable=yes, fullscreen=no, location=no";
  styleStr      += ", width=" + finalWidth    + ", height=" + finalHeight;
  styleStr      += ", top="   + cnrV + ", left="   + cnrH;
  var w = open (theURL, name, styleStr);
  sideWindows[insertIndx] = w;
}

