(function() {
  Willows = {
    menuNodes: function( aMenuID) { 
      var menu = $ID(aMenuID);
      // Get all of the anchors in the menu hierarchy, iterate through them until 
      // the href attribute matches the page name
      var anchors = menu.getElementsByTagName('a');
      for ( var i = 0; i < anchors.length; ++i)
        if ( window.location.href == anchors[i].href) break;

      // For a page not in the menu, return null
      if ( i == anchors.length) return null;

      var theNodes = [];
      // Now we go walk back up the tree and get the node hierarchy and save it for later (e.g., for breadcrumbs)
      var ptr = anchors[i];
      while( ptr && ptr.id != aMenuID) {
        if ( ptr.nodeName.toLowerCase() == 'li') {
          var a = ptr.getElementsByTagName('a');
          if ( a.length) a[0].className = 'active';
          theNodes.push(ptr);
        }
        ptr = ptr.parentNode;
      }
      var home = menu.getElementsByTagName('li')[0];
      if ( home != theNodes[theNodes.length-1])
        theNodes.push(home);

      // Return 'em in top-down order
      return theNodes.reverse();
    },
    createSideNav: function() {
      var foo = Willows.menuNodes('nav');
      if ( !foo || foo.length < 2) return;

      var heading = $ID('side_nav');
      if ( !heading) return;

      // Get the first unordered list tag from the menu...
      var ul = foo[1].getElementsByTagName('ul');
      var link = foo[1].getElementsByTagName('a')[0]; // Anchor from the menu bar
      heading.innerHTML = '<a id="heading" href="' + link.href + '">' + link.innerHTML + '</a>';

      if ( ul.length) {  // Has 1 or more levels
        var newUL = ul[0].cloneNode(true);
        heading.appendChild(newUL);
      }
    },
    normalizeColumns: function() {
      var left = $ID('left').offsetHeight;
      var right = $ID('right').offsetHeight;
      var center = $ID('center').offsetHeight;
      var max = center > right ? center : right;
      if ( left < max) {
        //alert('Left: '+left+'\nMax: '+max);
        var filler = $ID('left_filler');
        filler.style.height = (max - left - 24) + 'px';
      }
    }
  }

  Neaux.onload( function() {
    Willows.createSideNav();
    //Willows.normalizeColumns();
  });
})();

