//config
//id of the list to process
menu_id = "subnav";
//active A and LI nodes will get this class
menu_active_class = "aktiv";

//main function
function menu_autoMenu() {
  var url = location.href;
  var main = document.getElementById(menu_id);
  //if (!main) alert("No element with id '"+ menu_id +"' found");
  var link = menu_findLink(url, main);
  if (link != null) {
    // open the submenu
    var node = menu_ul_of_link(link);
    if (node) menu_display(node, true);

    //hilight the active menu
    link.className = menu_active_class; // A node
    // LI node
    node = menu_climb_to(link, "LI");
    node.className = menu_active_class;

    // open all parent menus
    node = menu_climb_to(link, "UL");
    while (node != null) {
      menu_display(node, true);
      node = menu_climb_to(node, "UL");
    }
  }
}
  
// toggles visibility of an element
function menu_toggle(element) {
  var o = element;
  var style = getStyle(o, "display");
  if ((style == "") || (style == "block")) {
    o.style.display="none";
  } else {
    o.style.display="block";
  }
}

// shows or hides an element
function menu_display(element, state) {
  element.style.display = state ? "block" : "none";
}  
  
//returns submenu of this link
//link is the link element 
function menu_ul_of_link(link) {
  var o = link;
  var i = 0; //avoid endless loops, expect at most 5 elements before submenu
  do {
    o = o.nextSibling;
    if (!o) return false;
      i++;
  } 
  while ((o.nodeType != 1) && (o.tagName != "UL") && (i < 5));
    if (i >= 5) return false;
    return o;  
}
  
//returns next higher node with the specified tag name
function menu_climb_to(node, tagName) {
  var o = node.parentNode;
  while (o) {
    if (o.tagName == tagName) return o;
    o = o.parentNode;
  }
  return null;
}

// finds first link element in child nodes that matches the url
function menu_findLink(url, element) {
  var link = null;
  for (var i=0; i<element.childNodes.length; i++) {
    var child = element.childNodes[i];
    if (child.tagName == "A") {
      var href = child.getAttribute("href");
      if (menu_isSameUrl(url, href)) {
        link = child;
	  }
    }
    if ((child.nodeType == 1) && child.hasChildNodes()) {
      var childlink = menu_findLink(url, child);
      if (childlink != null) link = childlink;
    }
  }
  return link;
}
  
//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 ((b[j] == ".") || (b[j] == "")) { j--; continue; }
    if ((a[i] == ".") || (a[i] == "")) { i--; continue; }
    if (! (a[i] == b[j])) return false;
    i--;
    j--;
  }
  return true;
}

//gets the dynamically calculated style of an element
function getStyle(el, prop) {
  //CSS2 compliant browsers
  if(typeof document.defaultView != "undefined"
    && typeof document.defaultView.getComputedStyle != "undefined")
  return document.defaultView.getComputedStyle(el,'').getPropertyValue(prop);
  //IE is not W3C compliant
  if (el.currentStyle) return el.currentStyle[prop];
  //rest of the world
  return "none";
}
