/*
	This script finds the current selected node in the menu and add the selected class to it
	as well as to its parents in the menu.
	If you want placeholders to be folded, add the class menuFolding to it, you can do that
	in the sitemap config (php).
*/

	// set to true when you want to fold all other nodes when unfolding another
var blnUnfoldOnlyOne = true;

$(document).ready(function() {

		// first fold all items, this is done here so when JS is disabled, everything will still show.
	$('li.menuFolding ul').css('display', 'none');

	$('.menuFolding').click(function(event){
	
			// make sure we have the right element (it might be triggered both by a span or a li)
		if($(event.target).attr('tagName') == 'SPAN')
			var objElement = $(event.target).next();
		else
			var objElement = $(event.target).children('ul:first');

		if(objElement.css('display') == 'none'){
				// fold everything first
			if(blnUnfoldOnlyOne)
				$('li.menuFolding ul').css('display', 'none');
			objElement.css('display','block');
			setClassActiveNodeTree(objElement);
		}else{
			objElement.css('display','none');
		}
		
	});
	
		// find the node that points to the current page then unfold it and add class 'selected'
	$('ul.MenuLevel0 a').each(function(index, objHref){
		if($(objHref).attr('href') == document.location){
			$(objHref).addClass('selected');
			objParent = $(objHref).parent();
			while(true){
				tagName = $(objParent).attr('tagName');
				if(tagName == 'LI'){
					$(objParent).addClass('selected');
					$(objParent).addClass('active');
				}
				if($(objParent).css('display') == 'none')
					$(objParent).css('display','block');	
				objParent = $(objParent).parent();
				if(tagName != 'LI' && tagName != 'UL')
					break;
			}
		}
	});

});

function setClassActiveNodeTree(objElement){
	// first remove all active classes from other nodes
	$('ul.MenuLevel0 li.active').removeClass('active');
	
	// add class active to all parent li nodes of the element
	objParent = $(objElement).parent();
	while(true){
		tagName = $(objParent).attr('tagName');
		if(tagName == 'LI'){
			$(objParent).addClass('active');
		}
		objParent = $(objParent).parent();
		if(tagName != 'LI' && tagName != 'UL')
			break;
	}
}
