function firstChildElement(elt)
 {		if (!elt) return null;
 			elt = elt.firstChild ;
 		while (elt && elt.nodeType!=1)
 			elt=elt.nextSibling;
 		return elt;	}

function nextSiblingElement(elt) {
		if (!elt) return null;
				elt = elt.nextSibling ;
		while (elt && elt.nodeType!=1)
				elt=elt.nextSibling;
				return elt;	}

function previousSiblingElement(elt) {
		if (!elt) return null;
				elt = elt.previousSibling ;
		while (elt && elt.nodeType!=1)
				elt=elt.previousSibling;
				return elt;	}

function lastChildElement(elt) {
		if (!elt) return null;
				elt = elt.lastChild ;
		while (elt && elt.nodeType!=1)
				 elt=elt.previousSibling;
		return elt;	}

function getLocation(url) {

	var words=url.split("/"); //split using slash as delimiter
	fileName = words [ words.length - 1];
   words=fileName.split("?"); //split using ? as delimiter
   fileName = words[0];
   words=fileName.split("#"); //split using # as delimiter
	return words[0];
}

function getLink(fileName, root) {
// search for deepest link within root that points to fileName
  var link = null;

  if (root == null){return link;};

  var el = firstChildElement(root);

  while ( el != null){

  		if ( el.nodeName == 'A' &&
       	  getLocation(el.getAttribute('href')) == fileName){
        // link is in this element
        // test if link is also in submenu after this el
        // if so choose that one
        link = getLink(fileName, nextSiblingElement(el));

        if (link != null)
            {return link}
        return el;
      }
      link = getLink(fileName, el);
      if (link != null) {return link}
      el = nextSiblingElement(el);
  }
  return link;
}

function activateMenu() {
 // activates link to current file and its "parent" links
 // and cuts the active sub-lists from the main list
 // and places them below that main list 
 var divmenu = document.getElementById("atletiekmenu");
 var list;
 var listItem;
 var sublist = document.createDocumentFragment();

 var loc = getLocation(document.location.href);

 var link = getLink ( loc, divmenu);
 // alert('link = ' + link.firstChild.nodeValue);
 //activate that link and is "parent" links
 while (link &&
    link.nodeName == 'A'){
    link.setAttribute ("class", "") ;
    link.className = "activated" ;
    listItem = link.parentNode;
    list = listItem.parentNode;
    link = previousSiblingElement(list);
 }
 // cut the first active sub-lists from the last list
 // and place them below that last list 
 list = lastChildElement(divmenu);
 while ( list!= null ) {
  	 listItem = firstChildElement(list);
  	 link = firstChildElement(listItem);
    while ( link != null &&
            link.className != 'activated' ) {
       listItem = nextSiblingElement(listItem);
       link = firstChildElement(listItem);
    }
    if  (listItem == null ) {
    	break;
    }
    // so link.className == 'activated'
    //alert('link = ' + link.firstChild.nodeValue);
     // now read on to next ul-element
    list = nextSiblingElement(link);
    if (list == null ) {
    	break;
    }

    sublist.appendChild( list);
    divmenu.appendChild(sublist);
    list = lastChildElement(divmenu);
  }
}


// function  to open rel="external" links in a new window
document.onclick = function(e)
{
  var target = e ? e.target : window.event.srcElement;

  while (target && !/^(a|body)$/i.test(target.nodeName))
  {
    target = target.parentNode;
  }

  if (target && target.getAttribute('rel')
      && target.rel == 'external')
  {
    var external = window.open(target.href);

    return external.closed;
  }
}
