
var FADE_STEP_TIME_MILLIS = 50;
var FADE_STEP_GRADE = 10;
var FADE_OPACITY_START = 0;

var g_currentSelection = ''; // Keeps track of which menu item was clicked so we can hide it if it is clicked again

function showMenu(id) 
{
	// grab the root menu element so we can use it
	var menuMainEl = document.getElementById('sliding-navigation');
	// This hides all submenus, comment out if you want the menus to stay open until reclicked
	hideAllSubMenus(menuMainEl.getElementsByTagName('ul'));
	// If the id is not found, that means we do not have a submenu for this item
	if(document.getElementById(id) != null)
	{
		// Toggle the click item 
		if(g_currentSelection == id)
		{
			// Clicking on the same item again, just close it down
			g_currentSelection = '';
			// Hide it's submenu
			document.getElementById(id).style.display = '';
			// IF YOU WANT TO JUST CLOSE THE MENU WITHOUT GOING TO THE LINK, RETURN FALSE			
			return true;
		}
		else 
		{
			// Set the current selection to the new item
			g_currentSelection = id;
			// Make it's submenu visible
			document.getElementById(id).style.display = 'block';
			// Setup the fading
			startFade(id);
			// Go to it's active link
			return true;
		}
	}
	else 
	{
		// Wipe the old selection
		g_currentSelection = '';
		// Go to the link that was clicked.
		return true;
	}
}

function hideAllSubMenus(subMenus)
{
	for(var i = 0; i < subMenus.length ; i++)
	{
		subMenus[i].style.display = '';
	}
}

function startFade(id)
{
	document.getElementById(id).style.visibility = 'visible';
	setElementOpacityById(id, 0);
	fadeIn(id, FADE_OPACITY_START, FADE_STEP_GRADE, FADE_STEP_TIME_MILLIS);
}

function setElementOpacityById(id, opacity)
{
	var element = document.getElementById(id);
	// Mozilla Bug Handling
	opacity = (opacity == 100)?99.999:opacity;
	// Mozilla, Firefox (Old)
	element.style.MozOpacity = opacity/100;
	// Mozilla, Firefox (New), CSS3, Safari 1.2
	element.style.opacity = opacity/100;
	// Internet Explorer
	element.style.filter = "alpha(opacity:"+opacity+")";
	// Safari (Old), Konqueror
	element.style.KHTMLOpacity = opacity/100;
}

function fadeIn(id, currOpacity, opacityStepAmount, opacityStepTime) 
{
	if (currOpacity <= 100)
	{
		setElementOpacityById(id, currOpacity);
		currOpacity += opacityStepAmount;
		window.setTimeout("fadeIn('"+id+"',"+currOpacity+","+opacityStepAmount+","+opacityStepTime+")", opacityStepTime);
	}
}


