/*
	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).
*/

	// this means on load only the upper level ul's will be visible visible, normally you want this for a vertical menu
var blnFoldToRootElements = true;
var blnAnimate = false;
var intAnimationDuration = 200;

$(document).ready(function() {

		// first fold all items, this is done here so when JS is disabled, everything will still show.
	if(blnFoldToRootElements){
		$('ul.MenuLevel0 li ul').css('display', 'none');
	}else{
		$('li.menuFolding ul').css('display', 'none');
	}
	
	$('ul.MenuLevel0 li').bind('mouseenter', function(event){
		event.stopPropagation();
		if($("ul:first",this).length > 0){
			$("ul:first",this).css('display', 'block');
			
			if(blnAnimate)
				$("ul:first",this).css('opacity', 0);
				
			if(!$("ul:first",this).hasClass('MenuLevel1')){
				// take the outer width of the parent so padding and borderwidth is also included.
				// Not that the top position of the parent is without the left and top borderWidth, so that is subtracted
				objParent = $("ul:first",this).parent();
				$("ul:first",this).css('left', $(objParent).outerWidth() - parseInt($(objParent).css('borderLeftWidth'), 10));
				$("ul:first",this).css('top', -1*parseInt($(objParent).css('borderTopWidth'), 10));
			}else{
				objParent = $("ul:first",this).parent();
				$("ul:first",this).css('left', $(objParent).offset().left + parseInt($(objParent).css('borderLeftWidth'), 10));
				$("ul:first",this).css('top', $(objParent).offset().top + $(objParent).outerHeight() - parseInt($(objParent).css('borderTopWidth'), 10));
			}
			
				// animation does only animate the opacity. Toggling or changing size is resulting in some madness
			if(blnAnimate)
				$("ul:first",this).animate(
					{opacity: 1}, 
					intAnimationDuration);
		}
	});

	$('ul.MenuLevel0 li').bind('mouseleave', function(event){
		$("ul:first",this).css('display', 'none');
		
	});	
	
		// find the node that points to the current page and add class 'selected'
	$('ul.MenuLevel0 a').each(function(index, objHref){
		if($(objHref).attr('href') == document.location){
			$(objHref).addClass('selected');			
			$(objHref).removeClass('normal');
			objParent = $(objHref).parent();
			
			while(true){
				tagName = $(objParent).attr('tagName');
				if(tagName == 'LI'){
					$(objParent).addClass('selected');
					$(objParent).addClass('active');
					$(objParent).removeClass('normal');
				}
				objParent = $(objParent).parent();
				if(tagName != 'LI' && tagName != 'UL')
					break;
			}
		}
	});

});

function setClassActiveNodeTree(objElement){
	// first remove all active classes from other nodes
	$('ul.MenuLevel0 li.active').addClass('normal');
	$('ul.MenuLevel0 li.selected').removeClass('normal');
	$('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).removeClass('normal');
		}
		objParent = $(objParent).parent();
		if(tagName != 'LI' && tagName != 'UL')
			break;
	}
}
