/*
*  Changing Menu Item classes for highlighting by comparing the href with the current site
*  Fixme: Toleranz f?r Leerzeichen zwischen Elementen!
*/
function highlightSubmenu(){
   var direct = false;
   var menue = document.getElementById("titlelist").childNodes;
   
   //handle project menu
   if ( document.getElementById("titlelist").className  == "no" ){
      //check for currently selected menue item on first level
	   for (var i=0; i < menue.length; i++){
	      //check if topmenue is selected
	      if (menue[i].firstChild.href == document.location.href ){
	        menue[i].className = "titlelist-item-selected";
	        direct = true;
	        
	        //alle submen?eintr?ge einblenden erstes element auf selected
	        var submenue = menue[i].childNodes[1].childNodes;
	        submenue[0].className ="titlelist-item-selected";
	        for (j=1; j < submenue.length; j++){
	            submenue[j].className = "titlelist-item";
	        }
	        
	      }else{
	         //if menue is not directly selected check for selected submenueitem
	         var subdirect = -1;
	         submenue = menue[i].childNodes[1].childNodes;
	         for (j=0; j < submenue.length; j++){
	            if (submenue[j].firstChild.href == document.location.href ){
	                subdirect=j;
	            }
	         }
	         if (subdirect >=0){
	            //highlight parent menu element
	            menue[i].className = "titlelist-item-selected";
	            //show all sibling menu items
	            for (var j=0; j < submenue.length; j++){
		            submenue[j].className = "titlelist-item";
	         	}
	         	submenue[subdirect].className = "titlelist-item-selected";
	            direct = true;    
	         }	         
	      }
	   }
	   //kein menuelement direkt ausgew?hlt, erstes childelement selecten
	   if(!direct){
	       menue[0].className = "titlelist-item-selected";
	       //alle submen?eintr?ge einblenden erstes element auf selected
	        var submenue = menue[0].childNodes[1].childNodes;
	        submenue[0].className ="titlelist-item-selected";
	        for (j=1; j < submenue.length; j++){
	            submenue[j].className = "titlelist-item";
	        }
	   }
   
   //handling the news menu
   }else if( document.getElementById("titlelist").className  == "_blog"){
    	var oldMenue = document.getElementById("titlelist").firstChild.childNodes;
   	var newMenue = document.createElement("div");
   	var outerMenue = null;
   	var selectedYear = document.location.search.match(/archive=(\d*)/);
   	selectedYear = selectedYear ? selectedYear.pop() : null;
   	var printedYear = null;
   	//cycle through all menu elements and create new menu
   	element = oldMenue[0];
   	while (element){
     	   //already setting the next Element before DOM is manipulated
     	   var nextElement = element.nextSibling;
     	   var currentYear = element.firstChild.title
     	   //if no year selected:select first year
     	   if(!selectedYear)selectedYear = currentYear; 
     	   //insert a year link on top of each year
     	   if (printedYear != currentYear){
     	   	//first append the old outer menue
     	   	if(outerMenue)
     	   		newMenue.appendChild(outerMenue);
     	   	//write the year before the current element in the new menue
     	   	outerMenue = document.createElement("div");
     	   	if (selectedYear == currentYear)
     	   		outerMenue.setAttribute("class","titlelist-item-selected" );
     	   	else
     	   		outerMenue.setAttribute("class","titlelist-item" );
     	   	//append the yearLink to the outer Menue
     	   	var yearLink = document.createElement("a");
     	   	yearLink.setAttribute("href",element.firstChild.href );
     			yearLink.appendChild(document.createTextNode(currentYear));
     	   	outerMenue.appendChild(yearLink);
     	   	var innerMenue = document.createElement("div");
     	   	innerMenue.setAttribute("class", "titlelist-sub");
     	   	outerMenue.appendChild(innerMenue);
     	   	printedYear = currentYear;
     	   }
     	   //only append the months that are in the selected year
         if (selectedYear == currentYear){
         	//highlight the selected menu element
	         if (element.firstChild.href == document.location.href ){ 
	            element.className = "titlelist-item-selected";
	         }
	         //add the element to the innerMenue
	         innerMenue.appendChild(element);
         }
         element = nextElement;
       }//end while
       //last outerMenue has to be appended here
       if(outerMenue)
     	 	newMenue.appendChild(outerMenue);
   	 //replace old menu with new one
   	 document.getElementById("titlelist").replaceChild(newMenue, document.getElementById("titlelist").firstChild)
   }else{
     //alert ("handling other menu");
     //check for currently selected menue item
     for (i=0; i < menue.length; i++){
         if (menue[i].firstChild.href == document.location.href ){ 
            menue[i].className = "titlelist-item-selected";
            direct = true;
            break;
         }
      }
      if(!direct){
        menue[0].className = "titlelist-item-selected";
      }
   }
}