/* -------------------------------------------------------------------

	Boondoggle - Hans Dreesen
	Atlas Copco
	
	This script blah...

------------------------------------------------------------------- */


function showSubNav(hoveredItem){
	$(".navigationLeftActiveItem").removeClass("navigationLeftActiveItem").addClass("navigationLeftActiveItemOff");
	hoveredItem.children("ul:hidden").css({"z-index": 2}).show();
}

function hideSubNav(hoveredItemSubNav){
	$(".navigationLeftActiveItemOff").removeClass("navigationLeftActiveItemOff").addClass("navigationLeftActiveItem");
	$(".hover").removeClass("hover");
	hoveredItemSubNav.hide();
}

function navbar() {
	// create variables
	var subNavTimer;
	var open;
	var hoverableItems = $("#navigationLeft > ul > li").not(".navigationLeftActiveItem, li:first-child");
	// make sure that when user mouses over sub menu ul it stays open
	hoverableItems.children("ul").mouseover(function(){
		$(this).show();
		// lets remember what's open
		open = $(this).parent();
	});
	// when user mouses over main item in navbar
	hoverableItems.mouseover(function(){
		$(".hover").removeClass("hover");
		$(this).addClass("hover");
		// close other nav item submenus
		if (open != null) open.children("ul").hide();
		// stop the timer
		clearTimeout(subNavTimer);
		// show this nav item's sub menu
		if ($(this) != open) showSubNav($(this));
	});
	// when user's mouse leaves the navbar item
	hoverableItems.mouseout(function(){
		$(".hover").not($(this)).removeClass("hover");
		// lets keep tabs of what is open
		open = $(this);
		// start the timer for 2 seconds until it closes.
		subNavTimer = setTimeout(function(){hideSubNav(open.children('ul:visible'))}, 500);
	});
}

$(document).ready(function() {
	navbar();
});
