  /*
    Adds CSS class names to the LI elements of a nested UL list.
    leafes get the menu_leaf_class
    open nodes get the menu_open_class
    closed nodes get the menu_closed_class
    the current node gets the menu_active_class

    written 2004 by Ortwin Glueck

    Extended by Mike Pringle for the Iowa Department of Administrative
    Services in February 2009.  If the "standard" automenu logic does
    not find the current page in the menu list, try breaking the name
    at the leftmost underscore to see if that will work.  So:

    finance_faq.html will be treated the same as finance.html *IF*
    finance_faq.html is not in the menu list.

  */

  //config
  var menu_active_class = "active";
  var menu_leaf_class = "leaf";
  var menu_open_class = "open";
  var menu_closed_class = "closed";
  var mp_a = 0;   // assume url was not found in the UL list
  var mp_alert = 0;   // 0 = debug alerts turned off.  1 = debug alerts turned on.
  var mp_reset = 0;
  var mp_list = "";

  //the default page that is displayed if URL ends in /
  var menu_default_page = "index.html";
  var menu_url;

  //main function
  //menu_id : id of the element containing the navigation
  function menu_main(menu_id) {
     var url = location.href;
     var mp_u;       // temporary working variable
     var mp_v;       // temporary working variable
     var mp_w;       // temporary working variable
     var mp_x;       // temporary working variable
     var mp_y;       // temporary working variable
     var mp_z;       // temporary working variable
   if (mp_alert==1) {  alert("Variables defined.");}
     if (url.lastIndexOf("/") == (url.length-1)) {
       url = url+menu_default_page;
     }
     if (url.lastIndexOf("?") >= 0) {
       url = url.substring(0, url.lastIndexOf("?"));
     }
     if (url.lastIndexOf("#") >= 0) {
       url = url.substring(0, url.lastIndexOf("#"));
     }
     menu_url = url;

     var main = document.getElementById(menu_id);
     if (!main) {
       alert("No element with id '"+ menu_id +"' found");
       return;
     }
//     alert("mp_a before 1st call is " + mp_a);
     menu_traverse(main);
//     alert("mp_a after 1st call is " + mp_a);
     if (mp_a==0) {
     mp_x = url.lastIndexOf("/") + 1;     // extract the filename and put it into
     mp_z = url.substring(mp_x);          // ...variable mp_z
   if (mp_alert==1) {   alert("mp_z = " + mp_z);}
     mp_x = mp_z.indexOf("_");            // look for the leftmost underscore in the filename
     mp_z = mp_z.substring(0,mp_x);       // ...and put the part we want to keep into variable mp_z
   if (mp_alert==1) {   alert("mp_z = " + mp_z);}
     mp_v = url.lastIndexOf("/")+ 1;
     mp_w = url.substring(0,mp_v);
   if (mp_alert==1) {   alert("mp_w = " + mp_w);}
//   mp_u = mp_w.lastIndexOf("/");
//   mp_v = mp_v + 1;
//   mp_w = url.substring(mp_u,mp_v);
   if (mp_alert==1) {   alert("mp_w = " + mp_w);}
     mp_x = url.lastIndexOf(".");        // look for the rightmost period in the url name
     mp_z = mp_w + mp_z + url.substring(mp_x);  // construct the new url
   if (mp_alert==1) {   alert("Built a new name "+ mp_z);}
     mp_reset = 1;
     menu_url = mp_z;
     menu_traverse(main);
     }
  }

  /* Walks down the subtree and on the way back
     sets properties.
     returns bit set
             1: set = element is a node, unset = element is a leaf
             2: set = element contains the active node
             4: set = element is the active A node
  */
  function menu_traverse(element) {
    var props  = 0;
    // walk down
    for (var i=0; i<element.childNodes.length; i++) {
      var child = element.childNodes[i];
      props |= menu_traverse(child); // aggregate bits
    }

    // on the way back now
    switch (element.tagName) {
      case "UL":
        props |= 1;
        break;

      case "LI":
        if (mp_reset == 1) element.className = "";
        var c1 = (props & 1) ?
                   ((props & (2|4)) ? menu_open_class : menu_closed_class)
                 : menu_leaf_class;
        element.className = element.className ? element.className+" "+c1 : c1;
        if (props & 4) {
          if (!(props & 2)) element.className += " "+menu_active_class;
          props |= 2;
          props &= 1 | 2; // reset bit 4
        }
        mp_list = mp_list + '\n' + "case LI " + element.className;
   if (mp_alert==1) {      alert(mp_list);}
        break;

      case "A":
        if (props & 2) break; // once is enough
        var href = element.getAttribute("href");
        if (menu_isSameUrl(menu_url, href)) {
            props |= 4;
         if (mp_alert==1) {    alert("Comparing values "+ menu_url + " and " + href);}
            mp_a = 1;
        }
        break;
    }

    return props;
  }

  //matches two URIs when href is the last part of url
  //.. and . are correctly resolved
  function menu_isSameUrl(url, href) {
    var a = url.split(/[?\/]/i);
    var b = href.split(/[?\/]/i);
    var i = a.length - 1;
    var j = b.length - 1;
    while ((i >= 0) && (j >= 0)) {
      if (b[j] == "..") { j-=2; continue; }
      if (a[i] == "..") { i-=2; continue; }
      if ((b[j] == ".") || (b[j] == "")) { j--; continue; }
      if ((a[i] == ".") || (a[i] == "")) { i--; continue; }
      if (! (a[i] == b[j])) return false;
      i--;
      j--;
    }
   if (mp_alert==1) {  alert("Returning true (URIs match)");}
    return true;
  }

