var doc = document;
var oOpenSubmenu = Array();
var intervalID;
var gaUlShown = Array();
var gbDisplay = false;
var gbSubmenuShown = false;
var giLevelActive = -1;

var aMenuIds = [
  ['MenuLeft', 'click'],
  ['HeaderLinks', 'mouseover'],
  ['MenuAffiliates', 'mouseover']
];

function fnAddEvents(oUlRoot, iMenuId, iLevel)
{
	gaUlShown[iLevel] = false; //Array to track which UL's have already been shown
	for (var i = 0; i < oUlRoot.childNodes.length; i++)
	{
		if (oUlRoot.childNodes[i].nodeName == 'LI')
		{
			oLiItem = oUlRoot.childNodes[i];
    	
			if (iLevel < giLevelActive || iLevel == 0)
				gbDisplay = false;

			if (oLiItem.className.indexOf('active') > -1)
			{
				giLevelActive = iLevel;
				gbDisplay = true;
			}
			
			LiEvent(oLiItem.childNodes[0], iMenuId);
			
			// This LI contains a submenu
			if (oLiItem.childNodes.length > 1)
			{
				// Send the UL within the LI
				for (var j = 0; j < oLiItem.childNodes.length; j++)
				{
					if (oLiItem.childNodes[j].nodeName == 'UL')
					{
						//Geen events toevoegen voor submenu's die altijd opengeklapt moeten zijn
						if (oLiItem.childNodes[0].getStyle("background") != "none")
						{
							oLiItem.childNodes[0].className = 'header_with_sub_close';
							oSubMenu = oLiItem.childNodes[j]; //Get submenu element
							
							// Check if submenu is set to display:block in the CSS
							// Submenu always open. don't add event handlers
							sEvent = aMenuIds[iMenuId][1];
							if ($(oSubMenu).getStyle("display") == 'block')
							{
								if (sEvent == 'mouseover')
								{
									oSubMenu.setStyle('display', 'none'); //Standaard alle submenu's verbergen
								}
								oLiItem.childNodes[0].className = '';
							}
							fnAddEvents(oSubMenu, iMenuId, iLevel+1);
						}
					}
				}
			}
			else if(iLevel == giLevelActive) //Active LI has no submenu
			{
				gbSubmenuShown = true; //LI has no submenu, set shown to true
			}
			
			if (gbDisplay == true && oUlRoot.nodeName == 'UL' && iLevel > 0)
			{
				if (iLevel == (giLevelActive+1) && gbSubmenuShown == false && sEvent != 'mouseover') //Submenu directly under LI which triggered it
				{
					showSub(oUlRoot); //Show child submenu
					gbSubmenuShown = true;
				}
				else if (iLevel <= giLevelActive)
				{
					if (!gaUlShown[iLevel])
					{
						showSub(oUlRoot); //Show parent submenus
						gaUlShown[iLevel] = true;
					}
				}
			}
		}
	}
}

function fnAddMenuEvents()
{
	var oMenuDiv;

	// Loop through all Menu's defined in aMenuIds
	for (i = 0; i < aMenuIds.length; i++)
	{
		oMenuDiv = doc.getElementById(aMenuIds[i][0]);
		
		// If it has content ..
		if (oMenuDiv && oMenuDiv.innerHTML != '' ) 
		{
			// Walk through children and find the first UL
			for(j = 0; j < oMenuDiv.childNodes.length; j++)
			{
				// Found the main UL element
				if (oMenuDiv.childNodes[j].nodeName == 'UL')
					fnAddEvents(oMenuDiv.childNodes[j], i, 0);			

				gbDisplay = false;
			}
		}
	}
}

function addOnclickHandlers()
{	
	fnAddMenuEvents();
}

function showCurrentSubMenu()
{
	return false;
	var oMenuDiv, oLiColl, oUlList;

	for (var i = 0; i < aMenuIds.length; i++)
	{
		oMenuDiv = doc.getElementById(aMenuIds[i][0]);
		if (oMenuDiv && oMenuDiv.innerHTML != '')
		{
			for(x=0; x<oMenuDiv.childNodes.length; x++)
			{
				if (oMenuDiv.childNodes[x].nodeName == 'UL') //Get main menu UL element
				{
					oLiColl = oMenuDiv.childNodes[x].childNodes; //Collection with all parent menu items
					break;
				}
			}
			
			for (var j = 0; j < oLiColl.length; j++)
			{
				if (oLiColl[j].childNodes.length > 1)
				{
					oUlList = oLiColl[j].childNodes[1];
					
					if (oLiColl[j].className.indexOf('active') > -1)	
					//if (oLiColl[j].className == 'active')
						switchMenuState(oUlList, i);
					for (var k = 0; k < oUlList.childNodes.length; k++)
						if (oUlList.childNodes[k].className.indexOf('active') > -1)
						//if (oUlList.childNodes[k].className == 'active')
							switchMenuState(oUlList, i);
					
				}
			}
		}
	}
	
	oMenuDiv = oLiColl = oUlList = null
}

function LiEvent(oObj, iMenuID)
{
	var oEffect;
	sEvent = aMenuIds[iMenuID][1];
	oMenuItem = $(oObj);
	
	//if (oMenuItem.getNext() && oMenuItem.getNext().getTag() == 'ul') //GRRRRRR AT MOOTOOLS 1.2
	if (oMenuItem.getNext() && oMenuItem.getNext().get('tag') == 'ul')
	{
		var oUlList = oMenuItem.getNext();
		// Events
		if (sEvent == 'click')
		{
			oMenuItem.addEvent('click', function(event) {
				// It's most likely the A triggered the event
				// We have our UL element, now show or hide it
				switchMenuState(oUlList, iMenuID);
				
				oItem = oUlList = null;
			});
		}
		else if (sEvent == 'mouseover')
		{
			oMenuItem.addEvent('mouseover', function(event) {
				//if(typeof oEffect != 'undefined')
				//	oEffect.stop();
				
				oEffect = new Fx.Tween(oUlList).start('opacity', oUlList.opacity, 0);
				//oEffect = new Fx.Tween(oUlList, 'opacity').start(oUlList.opacity, 0);
			});
		}
	}
} 
  
function SubMenu(oObj)
{
	$(oObj).addEvent('mouseover', function(event) {
		$clear(intervalID);
	});
}

var intervalID;
var oCurrentMenuNode;
function hideSubMenu(MenuNode, iDelay)
{
	$clear(intervalID);
	oCurrentMenuNode = MenuNode;
	var intervalID = (function() { 
		if (oCurrentMenuNode)
		{
			oCurrentMenuNode.style.display = 'none';
		}
	}).delay(iDelay);
	//intervalID = setInterval("ReallyHideSubMenu()", iDelay);	
}

function ReallyHideSubMenu()
{
	if (oCurrentMenuNode)
	{
		oCurrentMenuNode.style.display = 'none';		
	}
}

function switchMenuState(oSubMenu, iMenuID) //Toggle submenu on/off
{
	if (oSubMenu.style.display == 'block') 
	{
		hideSub(oSubMenu);
	}
	else
	{
		showSub(oSubMenu);
	}
}

function hideSub(oSubMenu)
{
	oSubMenu.previousSibling.className = 'header_with_sub_close';
	oSubMenu.style.display = 'none';
}

function showSub(oSubMenu)
{
	oSubMenu.previousSibling.className = 'header_with_sub_open';
	oSubMenu.style.display = 'block';
}

function getNextSibling(oStartBrother){
  oEndBrother = oStartBrother.nextSibling;
  while(oEndBrother.nodeType!=1){
    oEndBrother = oEndBrother.nextSibling;
  }
  return oEndBrother;
}