var basemenu = "";
var submenu = "";

var basehighlight = null;
var baseforeground = "";
var basebackground = "";

var subhighlight = null;
var subforeground = "";
var subbackground = "";

var lvl1left = -1;
var lvl2left = -1;

var lvl1highlight = "";
var lvl2highlight = "";
var lvl3highlight = "";

var lvl1name = "";
var lvl2name = "";
var lvl3name = "";

var w = 0;


function MainButtonHighlight(e,object,foreground,background)
{    	
	//cancel bubble event code
	if (window.event) 
	{
		if (!window.event.cancelBubble) 
		{
			window.event.cancelBubble = true;
		}
    }
    else
    {
            try
            { 
		e.preventDefault();
		e.stopPropagation();
            }
            catch(err){}
            
    }

	var location = "http://"+window.location.hostname+"/data/images/menu/";

    var menuPlaats = "Linkermenu";
    
    if (object.name.split("#").length > 0)
    {
		if (object.name.split("#")[0] == "Rechtermenu")
		{
			menuPlaats = "Rechtermenu";
		}
	} 
		
    //breedte button
    var parentWidth = object.width;	
		
	lvl1left = object.offsetParent.offsetLeft;	
   
    object.src = location + "btn_"+foreground+"_"+background+"_"+RemoveBtn(object.id)+".gif";
    object.style.cursor="pointer";   
        
    
	if (submenu != "div"+RemoveBtn(object.id))
	{			
		CloseMenu(submenu);
		
		submenu = "div"+RemoveBtn(object.id);
		
		
		CallSubMenu(submenu,menuPlaats,lvl1left,parentWidth);		
		
		//zorg dat de highlighting van vorige buttons ongedaan gemaakt wordt
		if (basehighlight != object && basehighlight != null)
		{
			MainButtonNormal(e,basehighlight,baseforeground,basebackground);
		}	
		
		if (subhighlight != object && subhighlight != null)
		{
			SubButtonNormal(e,subhighlight,subforeground,subbackground);
		}	
		
	}
	else
	{	
		SubButtonNormal(e,subhighlight,subforeground,subbackground);
	}
	
}

function MainButtonNormal(e,object,foreground,background)
{            
    //cancel bubble event code
	if (window.event) 
	{
		if (!window.event.cancelBubble) 
		{
			window.event.cancelBubble = true;
		}
    }
    else
    {
            try
            { 
		e.preventDefault();
		e.stopPropagation();
            }
            catch(err){}
    }
    
    basehighlight = object;
    baseforeground = foreground;
    basebackground = background;
    
    if (RemoveDiv(submenu) == RemoveBtn(object.id))
    {
		//blijf gehighlight, want submenu is actief
	}
	else
	{
		if (object.id != "btn"+lvl1name)
		{		
		
			var location = "http://"+window.location.hostname+"/data/images/menu/";
			
			//weer naar normale toestand, geen submenu meer geselecteerd
			object.src = location + "btn_"+foreground+"_"+background+"_"+RemoveBtn(object.id)+".gif";
			object.style.cursor="pointer";
		}
	}
}

function SubButtonHighlight(e,object,foreground,background)
{   
	//cancel bubble event code
	if (window.event) 
	{
		if (!window.event.cancelBubble) 
		{
			window.event.cancelBubble = true;
		}
    }
    else
    {
            try
            { 
		e.preventDefault();
		e.stopPropagation();
            }
            catch(err){}
    }		
	
    
	var calculateLeft = lvl2left + object.offsetParent.offsetLeft;	
	
	var buttonName = GetButtonName(object.id);
	
	var location = "http://"+window.location.hostname+"/data/images/menu/";
	
	object.src = location+"btn_"+foreground+"_"+background+"_"+buttonName+".gif";
    object.style.cursor="pointer";
	
		
}

function SubButtonNormal(e,object,foreground,background)
{   
    
       
    //cancel bubble event code
	if (window.event) 
	{
		if (!window.event.cancelBubble) 
		{
			window.event.cancelBubble = true;
		}
    }
    else
    {
            try
            { 
		e.preventDefault();
		e.stopPropagation();
            }
            catch(err){}
    }
    
    subhighlight = object;
    subforeground = foreground;
    subbackground = background;
    
   
if (object!=null)
	{	
   
		var buttonName = GetButtonName(object.id);			


		if (buttonName != lvl2name)
		{	
			var location = "http://"+window.location.hostname+"/data/images/menu/";
				
			object.src = location+"btn_"+foreground+"_"+background+"_"+buttonName+".gif";
			object.style.cursor="pointer";
		}
			
	
		
	}
}



function CallSubMenu(menuName,menuPlaats,LeftDist,parentWidth)
{	
	var divMenu = document.getElementById(menuName);
	
	if(divMenu!=null) 
    {
        divMenu.style.left   = (LeftDist ) + 'px';
		divMenu.style.display = 'block';
		
		//### bereken breedte submenu ###

		var divWidth = 0 + CalculateWidth(divMenu);		


		//### bepaal uitlijning submenu	###	

		//als het hier het linkermenu betreft en de ruimte rechts is te krap, buig dan om
		if (LeftDist + divWidth > 945 && menuPlaats=="Linkermenu")
		{
			LeftDist = LeftDist+parentWidth - divWidth;
			
			submenuleft = divMenu.id+":"+LeftDist;
			
			divMenu.style.left   = (LeftDist ) + 'px'; 
		}

		//als het hier het rechtermenu betreft, voer dan dit stuk code uit:
		if (menuPlaats=="Rechtermenu")
		{
			//
			if (LeftDist + divWidth < 5)
			{
				//skip -> naar links uitlijnen gebeurt standaard, dus geen extra actie nodig
			}
			else
			{
				//plaats voor uitlijning naar links, dus dit uitvoeren
				LeftDist = LeftDist+parentWidth - divWidth;
				
				submenuleft = divMenu.id+":"+LeftDist;
				
				divMenu.style.left   = (LeftDist ) + 'px'; 
			}		
		
		}

		lvl2left = LeftDist;
		
    }
	
}




	function TraverseTree(obj){
		var o = obj;
		var i = 0;
		if(o.childNodes!=null){
			try{
				var col = o.childNodes;
			}catch(e){};
			for(i=0;i<col.length;i++){
				if(col[i].childNodes!=null){
					if(col[i].childNodes.length>0){
						TraverseTree(col[i]);
					}else{
					try{
						SaveImg(col[i]);
					}catch(e){}
					}
				}
			}
		}
	}
			
	function SaveImg(obj){
		try{
			if(obj.tagName!=null && obj.tagName.toLowerCase()=="img")
			{
				w = w + obj.width;
			}				
		}catch(e){}
	}
	
	function CalculateWidth(obj){
		w = 0;
		TraverseTree(obj);
		return w;
	}

//

function CloseMenu(menuName)
{    
    var divMenu = document.getElementById(menuName);
	
	if(divMenu!=null) 
    {
		divMenu.style.display = 'none';
	}
	
}

function CloseAllMenus(e)
{		
	//cancel bubble event code
	if (window.event) 
	{
		if (!window.event.cancelBubble) 
		{
			window.event.cancelBubble = true;
		}
    }
    else
    {
            try
            { 
		e.preventDefault();
		e.stopPropagation();
            }
            catch(err){}
    }
    

    CloseMenu(submenu);
	submenu = "";
	
	if (basehighlight != null)
	{
		MainButtonNormal(e,basehighlight,baseforeground,basebackground);
	}
	
	if (subhighlight != null)
	{
		SubButtonNormal(e,subhighlight,subforeground,subbackground);
	}
	
	
	ShowCurrentPage();
	
	
}

function ShowCurrentPage()
{

	var btnlvl1 = document.getElementById("btn"+lvl1name);
	var btnlvl2 = document.getElementById("btn"+lvl1name+"#"+lvl2name);
	
	var location = "http://"+window.location.hostname+"/data/images/menu/";

	if (btnlvl1 != null) {btnlvl1.src=location+"btn_rood_zwart_"+lvl1name+".gif";}
	if (btnlvl2 != null) {btnlvl1.onmouseover();btnlvl2.onmouseover();}

}


function GetButtonName(longname)
{
	var buttonBase = longname.split("#");
	
	var buttonName = "";
	
	if (buttonBase.length > 1)
	{
		buttonName = buttonBase[buttonBase.length-1];
	}
	
	return buttonName;
		
}

function RemoveDiv(text)
{
    while (text.substring(0,3)=="div")
	{
		text = text.substring(3,text.length)
	}
	
	return(text);
}

function RemoveBtn(text)
{
    while (text.substring(0,3)=="btn")
	{
		text = text.substring(3,text.length)
	}
	
	return(text);
}
