
// These are the parameters to define the appearance of the menu.

var showNumbers = false;           // display the ordering strings: yes=true | no=false
var tocBehaviour = new Array(1,0); // Indicates how the menu shall change when clicking in the heading symbol (1st arg.) resp. in the heading text (2nd arg). Arg's meaning: 0 = No change, 1 = menu changes with automatic collapsing, 2 = menu changes with no automatic collapsing.
var tocLinks = new Array(1,0);     // Indicates wether the content's location shall be changed when clicking in the heading symbol (1st arg.) resp. in the heading text (2nd arg). Arg's meaning: 1 = No, 0 = Yes. 


// Checking the client's browser
var isIE = navigator.appName.toLowerCase().indexOf("explorer") > -1;

// Other global variables:

// oldCurrentNumber is required to keep a hedings ordering string (or number) in case the menu should change (tocChange > 0) 
// but the content's location should remain unchanged (i.e. noLink == 1). In those cases the heading preceeded by oldCurrentNumber 
// has to remain hilited (otherwise the heading preceeded by currentNumber has to be hilited).
var oldCurrentNumber = "";
var oldLastVisitNumber = "";

// toDisplay: Array to keep the display status for each heading. It is initialised so only the top level headings are displayed 
// (headings preceeded by a single string without a dot):  
var toDisplay = new Array();
for (ir=0; ir<tocTab.length; ir++) {
  toDisplay[ir] = tocTab[ir][0].split(".").length==1;
}

// ************************************************
// The function redisplays the menu and the content 
// ************************************************

function reDisplay(currentNumber, tocChange, noLink, e) {
  // Input parameters:
  // currentNumber: Hierarchical ordering string (or number) of the heading the user wants to display (we call this heading the "current"). This controls the change of both, the menu depending on the second parameter tocChange and the content's URL depending on the third parameter noLink. 
  // tocChange:     Controls how to change the menu. 0 = No change, 1 = Change with automatic collapsing of expanded headings that are not on the path to the current heading, 2 = Change wthout automatic colapsing (as use for example by Windows Explorer or Mac OS).
  // noLink:        Controls wether the content's URL shall be changed to the value given by the 3rd element of an tocTab's entry (= 0) or not (= 1).
  // e:             The event that triggered the function call. If it is set it must be the event object.

  // If there is an event that triggered the function call: Checking the control key depending on the browser used. If it is pressed and tocChange is greater than 0 tocChange is set to 2 so the menu changes without automatic collapsing: 
  if (e) {
    ctrlKeyDown = (isIE) ? e.ctrlKey : (e.modifiers==2);
    if (tocChange && ctrlKeyDown) tocChange = 2;
  }

  // Initializing the menu window's document and displaying the title on it's top. The menu is performed by a HTML table:
  toc.document.clear();
  toc.document.write("<html>\n<head>\n");
  toc.document.write("<SCRIPT LANGUAGE=\"JavaScript\">\n");
  toc.document.write("  function swapImg(oldImg, newImg) {\n");
  toc.document.write("    var tmp = document[oldImg].src;\n");
  
  toc.document.write("    if (tmp.search(/\_off/) != -1)\n");
  toc.document.write("      var tmp2 = tmp.replace(\/\_off\/, \"_on\");\n");
  toc.document.write("    else\n");
  toc.document.write("      var tmp2 = tmp.replace(\/\_on\/, \"_off\");\n");
  
  toc.document.write("    document[oldImg].src=tmp2;\n");
  toc.document.write("  }\n");
  toc.document.write("</SCRIPT>\n");
  toc.document.write("<title>menu</title>\n<link type=text/css rel=stylesheet href=stylemenu.css></head>\n");
  toc.document.write("<body>\n");
  toc.document.write("<table style=\"position:absolute; left:20px;\"width=200 border=0 cellspacing=2 cellpadding=0>\n");

  toc.document.write("<BR>\n");  

  //  currentLevel = the level of the current heading:
  var currentNumArray = currentNumber.split(".");
  var currentLevel = currentNumArray.length-1;

  // currentIndex = Current heading's index in the tocTab array:
  var currentIndex = null;
  for (i=0; i<tocTab.length; i++) {
    if (tocTab[i][0] == currentNumber) {
      currentIndex = i;
      break;
    }
  }

  // If currentNumber was not found in tocTab: No action.
  if (currentIndex == null) return false;

  // currentIsExpanded = Expand/Collaps-state of the current heading:
  if (currentIndex < tocTab.length-1) {
    nextLevel = tocTab[currentIndex+1][0].split(".").length-1;
    currentIsExpanded = nextLevel > currentLevel && toDisplay[currentIndex+1];
  } 
  else
    currentIsExpanded = false;

  // Determining the new URL and target (if given) of the current heading
  theHref = (noLink) ? "" : tocTab[currentIndex][2];
  theTarget = tocTab[currentIndex][3];

  // ***********************************************************************
  // 1st loop over the tocTab entries: Determining which heading to display:
  // ***********************************************************************
  for (i=1; i<tocTab.length; i++) {
    // Nothing to do if the tocChange parameter is set to 0. If it is set to 1 or 2...
    if (tocChange) {
      thisNumber = tocTab[i][0];
      thisNumArray = thisNumber.split(".");
      thisLevel = thisNumArray.length-1;

      // isOnPath = this heading is on the path to the current heading in the menu hierarchy or a sibling of such a heading:
      isOnPath = true;

      if (thisLevel > 0) {
        for (j=0; j<thisLevel; j++) {
          isOnPath = (j>currentLevel) ? false : isOnPath && (thisNumArray[j] == currentNumArray[j]);
        }
      }

      // By the following, the headings on the path to the current heading and the siblings of such  headings (isOnPath==true, see above) will be displayed anyway. If the tocChange parameter is set to 1 no other heading will be displayed. If it is set to a number greater than 1 the headings that have been displayed before will additionally be displayed again.  
      toDisplay[i] = (tocChange == 1) ? isOnPath : (isOnPath || toDisplay[i]);

      // Now let's perform the expand/collaps mechanism: If the heading is a descendant of the current heading it's next display depends on wether the current heading was expanded or collapsed. If it was expanded the descendants have not to be displayed this time, otherwise only the childs has to be displayed but not the grandchildren, great-grandchildren etc.. Remember that currentIsExpanded says wether the current heading was expanded or not. The if-clause is a criteria for being a descendant of the current heading. If it's a descendant and thisLevel == currentLevel+1 it's a child.
      if (thisNumber.indexOf(currentNumber+".") == 0 && thisLevel > currentLevel) { 		
        if (currentIsExpanded)
          toDisplay[i] = false;
        else 
          toDisplay[i] = (thisLevel == currentLevel+1); 
      }
    } 
  } // End of loop over the tocTab

  // **********************************************************
  // 2nd loop over the tocTab entries: Displaying the headings:
  // **********************************************************
  
  for (i=0; i<tocTab.length; i++) {
    if (toDisplay[i]) {
      thisNumber = tocTab[i][0];
      thisNumArray = thisNumber.split(".");
      thisLevel = thisNumArray.length-1;
      isCurrent = (i == currentIndex);

      // Setting the heading's symbol depending on whether this heading is expanded or not or if it is a leaf. It is expanded if the next heading has a greater level than this one AND has to be displayed: 
      if (i < tocTab.length-1) {
        nextLevel = tocTab[i+1][0].split(".").length-1;
        img = (thisLevel >= nextLevel) ? "icon_dot_off" : ((toDisplay[i+1]) ? "icon_arrowdown_off" : "icon_arrowright_off");
      } 
      else 
        img = "icon_dot_off"; // The last heading is always a leaf.

      // Now writing this menu line, i.e. a table row...:			
      toc.document.writeln("<tr>");

      // ...first some empty cells for the line indent depending on the level of this heading...:
      for (k=1; k<=thisLevel; k++) {
        toc.document.writeln("  <td>&nbsp;</td>");
      }

      // ...then the hading symbol and the heading text each with a javaScript link caling just this function reDisplay again: 
      // *** Luring's Index hack... ***
      var indexHack = (thisNumber == "index_key") ? "CLASS=\"IndexKey\" " : "";

      // Create a unique id for each image
      // Note: For some strange reason, this won't work if you leave out the +"a" thing at the end.
      // Don't ask me why - It's just one of those wierd things...
      var imgName = new String(thisNumber.replace(/\./g, "dot") +"a");

      // Change tocBehaviour[0] to [1] if you want it to be a link instead of expanding/collapsing the menu item...
      toc.document.writeln("  <td width=10 align=center valign=center><a href=\"javaScript:history.go(0)\" onMouseDown=\"parent.reDisplay('" + thisNumber + "'," + tocBehaviour[0] + "," + tocLinks[0] + ",event)\"><img src=\"images/" + img + ".gif\" border=0 name=\"" +imgName+ "\"></a></td>");
      toc.document.writeln("  <td width=240 colspan=" + (nCols-thisLevel) + "><a " +indexHack+ "href=\"javaScript:history.go(0)\" onMouseOver=\"swapImg('" +imgName+ "', 'images/" +img+ ".gif');\" onMouseOut=\"swapImg('" +imgName+ "', 'images/" +img+ ".gif');\"  onMouseDown=\"parent.reDisplay('" + thisNumber + "'," + tocBehaviour[0] + "," + tocLinks[1] + ",event)\">" + ((showNumbers)?(thisNumber+" "):"&nbsp;") + tocTab[i][1] + "</a></td>");
      toc.document.writeln("</tr>");
    }
  } // End of loop over the tocTab

  // *********************************************************************************************************
  // Closing the menu document and displaying new content in the content frame or in the top window if required 
  // *********************************************************************************************************

  // Updating the global variables oldCurrentNumber and oldLastVisitNumber. See above for its definition
  if (!noLink) { 
    oldLastVisitNumber = oldCurrentNumber;
    oldCurrentNumber = currentNumber;
  }

  // Closing the menu table and the document
  toc.document.writeln("</table>\n</body></html>");
  toc.document.close();

  // Setting the top or content window's location if required
  if (theHref) 
    if (theTarget=="top") top.location.href = theHref;
    else if (theTarget=="parent") parent.location.href = theHref;
    else if (theTarget=="blank") open(theHref,"");
    else main.location.href = theHref;
}