var bMouseEventsActive = true;
var tHideTimeOut, tShowTimeOut, tMenuHideTimeOut;
var sNavID  = '';
var sMenuID = '';
var sNavCode = '';
var sNavItemCode = '';

/*
* ---------------------------------------
* Nav Event Handlers
* ---------------------------------------
*/

var navMouseoverHandler = function(event) {
	showSubMenu(this, event);
	event.stopPropagation();
};

var navMouseoutHandler = function(event) {
	hideSubMenu(this, event);
	event.stopPropagation();
};

/*
* ---------------------------------------
* END Nav Event Handlers
* ---------------------------------------
*/

function initNav(sID)
{
	sNavID  = sID;

	$('#' + sNavID).find('ul').each(
		function() {
			$(this).before('<span class="submenuIcon"></span>');
			$(this).parent('li').addClass('hasSubmenu');
			$(this).find('li:first').addClass('firstItem');
		}
	);
	
	$('.showme').livequery(
		function() {
			$(this).show('slide', { direction : 'left' }, 200, function() { $(this).removeClass('showme').css('display',''); })
			//$(this).show(0,function() { $(this).removeClass('showme').css('display',''); })
		}
	);
	
	//Bind events
	
	$('#' + sNavID).find('li').bind({
		mouseover	: navMouseoverHandler,
		mouseout	: navMouseoutHandler 
	});
}

function showSubMenu(oThis, event) {
	if(!bMouseEventsActive)
		return;
	
	clearTimeout(tHideTimeOut);
	
	var bAlreadyShown = false;
	
	if($(oThis).find('ul:first').hasClass('show'))
		bAlreadyShown = true;
	
	$('#' + sNavID + ' ul').removeClass('showme');
	$('#' + sNavID + ' ul').removeClass('show');
	$('#' + sNavID + ' ul').css('display','');
	$('#' + sNavID + ' li').removeClass('highlight');
	
	if(bAlreadyShown)
		$(oThis).parentsUntil('#' + sNavID).filter('ul').add($(oThis).find('ul:first')).addClass('show');
	else {
		$(oThis).parentsUntil('#' + sNavID).filter('ul').addClass('show');
		//I set the display to none to override the show class temporarily so the animation works correctly, 
		//I changed the show class to be added here instead of livequery as it solved a problem with events firing quicly one after another. 
		//The animation would still be going when we removed all show classes then livequery would add one when we didn't want one.
		$(oThis).find('ul:first').addClass('show showme').css('display','none');
	}
	
	$(oThis).parentsUntil('#' + sNavID).filter('li').add($(oThis)).addClass('highlight');
}

function hideSubMenu(oThis, event) {
	if(!bMouseEventsActive)
		return;
	
	tHideTimeOut = setTimeout('$(\'#' + sNavID + ' ul\').removeClass(\'show\'); $(\'#' + sNavID + ' ul\').removeClass(\'showme\'); $(\'#' + sNavID + ' ul\').css(\'display\',\'\'); $(\'#' + sNavID + ' li\').removeClass(\'highlight\'); ',1000);
}

function toogleBool(bBool) {
	if(bBool)
		return false;
	else
		return true;
}
