function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}





function enableFlyoutMenusForOldIEs() {
  var ieflyouts = getElementsByClassName('flyout');
  for (var i = 0; el = ieflyouts[i]; ++i) {
    el.onmouseover = function() {
      var flyout = this.getElementsByTagName('ul')[0];
      flyout.style.display = 'block';
    };
    el.onmouseout = function() {
      var flyout = this.getElementsByTagName('ul')[0];
      flyout.style.display = 'none';
    };
  }
}





function collapseList(el) {
  var children = el.getElementsByTagName("ul");
  if (children) {
    children[0].style.display = "none";
  }
  el.className = "collapsed";
  el.onclick = function() {expandList(this)};
}

function expandList(el) {
  var children = el.getElementsByTagName("ul");
  if (children) {
    children[0].style.display = "block";
  }
  el.className = "expanded";
  el.onclick = function() {collapseList(this)};
}


function collapseLists(id) {
  // Get all lists in the given id
  var lists = document.getElementById(id).getElementsByTagName("li");

  // Go through each list and hide all child lists
  for (var i = 0; i < lists.length; i++) {
    // Collapse list if it is collapsable
    if (lists[i].className == "collapsable") {

      var child = lists[i].getElementsByTagName("ul");
      if (child) {
        child[0].style.display = "none";

        // Give the collapsed list's parent node an expander
        lists[i].className = "collapsed";
        lists[i].onclick = function() {expandList(this)};

        // Don't break links inside the list -- make them nullify the list's onclick()
        var links = lists[i].getElementsByTagName("a");
        for (var j = 0; j < links.length; j++) {
          links[j].onclick = function() {
//                if (this.parentNode.onclick != "") {
              this.parentNode.onclick = "";
//                } else if (this.parentNode.parentNode.onclick != "") {
              this.parentNode.parentNode.onclick = "";
//                } else if (this.parentNode.parentNode.parentNode.onclick != "") {
              this.parentNode.parentNode.parentNode.onclick = "";
//                }
          };
        }

      }


    }
  }
}

// Highlight the current page from the menu links (doing it in JavaScript
// keeps page static across the domain and pushes the processing to the user)
function highlightCurrent(id) {
  var menu = document.getElementById(id);
  var links = menu.getElementsByTagName("a");
  for (var i = 0; i < links.length; i++) {
    var current_url = window.location.toString();
    var link_url = links[i].href.toString();

    // Change link's classname if it's link is the same as the current URL
    if (current_url == link_url) {
//      links[i].className = "current";
      links[i].className += " current";

      // Expand current menu if necessary
      // Note:
      //   links[i] = current <a> link
      //   links[i].parentNode = <li class="X"><a></a></li>
      //   links[i].parentNode.parentNode.parentNode = <li class="X"><ul><li><a></a></li></ul></li>
      if (links[i].parentNode.className == "collapsed") {
        expandList(links[i].parentNode);
      }
      // Expand parent menu if necessary
      if (links[i].parentNode.parentNode.parentNode.className == "collapsed") {
        expandList(links[i].parentNode.parentNode.parentNode);
      }
      
    }
  }
}



/* Toggle search modes */
function toggleSearch(mode) {
  if (mode == 'people') {
    document.search_form.action = '/directory/';
  } else {
    document.search_form.action = 'http://www.google.com/search';
  }
}

