//you can't use any divs that start with 'me'!!!

if (!isAdmin){ if (top != self){ top.location.replace(document.location); } }
window.loaded = false;
window.finished = false;

var arrShown = new Array();
//var arrUnHover = new Array(); //use of this backwards-check array slows down the menu (???)

var assigningSub = false;
var rememberY = 0;
function getOffsetFromTop(){ // get current y for all browsers
	if (topmenuhorizontal){
		return 0;
	}else{
		if (document.layers){ //netscape vier heeft een bugje als je naar boven scrollt, er gebeurt dan niets meer
			return (window.pageYOffset > 0)?window.pageYOffset:0;
		}else{
			return (window.scrollY)?window.scrollY:(document.body.scrollTop)?document.body.scrollTop:0;
		}
	}
}
function count(countinstring,stringtocount){
	counthiero = 0; counterin = 0;
	while (countinstring.indexOf(stringtocount) != -1) {
		counthiero = countinstring.indexOf(stringtocount)+1;
		countinstring = countinstring.substr(counthiero);
		counterin++;
	}
	countinstring="";
	return counterin;
}

tomgetpath = setTimeout('',200);
clearTimeout(tomgetpath);
function mgetpath(i,displayinlayer,fromuser){
	if (!i){ i = 0; }
	tempi = i; templ = 0; menuPath = ''; excludei = (assigningSub)?assigningSub:i;
	if (fromuser){ excludei=0; }
	while (tempi != 0){
		templ++;
		for (cmlevel = 0; cmlevel <= arri.length; cmlevel++){ //walk through existing id's
			thisItem = document.getElementById('me'+tempi+'nu'+arri[cmlevel]);
			if (arr[arri[cmlevel]]){ //if current id has a submenu / string of sub id's
				if (arr[arri[cmlevel]].toString().indexOf('{'+tempi+'}') != -1){ //if this id is a submenu of this other id
					if (tempi != excludei){ //don't include the item itself
						try{
							//thisItem = document.getElementById('me'+tempi+'nu'+arri[cmlevel]);
							thisItemText = thisItem.innerHTML;
							//while (thisItem.indexOf('\\') != -1){ thisItem = thisItem.replace('\\',''); }
							//while (thisItem.indexOf('\'') != -1){ thisItem = thisItem.replace('\'',''); }
							thisItemText = thisItemText.replace('onmouseover','donothing');
							thisItemText = thisItemText.replace('class','not');
							thisItemText = thisItemText.replace('<A','<A class="m'+skin+'Path"');
							menuPath = '|' + thisItemText + menuPath;
							//remove excess entries:
							if (count(menuPath,'|') > mPathEntries){
								menuPath = menuPath.replace(menuPath.substring(0,menuPath.indexOf('|',1)),'');
							}
			//set active in menu:
			mSkins[tempi] = mSkins[tempi] + 'Active';
			thisItem.className = 'm'+mSkins[tempi];
			thisItem.children(0).children(0).className = 'm'+mSkins[tempi]+'a';
						}catch(e){ }
					}
					tempi = arri[cmlevel];
					break; //for loop
				}
			}
			if (templ > arr.length){ tempi = 0; } //all items checked, nothing found (probably "offsite")...
		}
	}
	if (displayinlayer){
		clearTimeout(tomgetpath);
		document.getElementById(displayinlayer).innerHTML=menuPath;
		tomgetpath = setTimeout('document.getElementById(\''+displayinlayer+'\').innerHTML=\'\';',2000);
	}
	return menuPath;
}

mHoldScroll = true;
function positionControl(){
	//change = (getOffsetFromTop() - rememberY) / 2;
	//if (Math.abs(change) > 2*mItemHeight){ change = (change < 0)?-2*mItemHeight:2*mItemHeight; }
	//if (Math.abs(change) > 1){
	//	rememberY = rememberY + change;
	//	m(0);
	//}
	change = getOffsetFromTop() - rememberY;
	if (change == 0 && !mHoldScroll){ m(0); }
	mHoldScroll = (change == 0);
	rememberY = rememberY + change;
}
//setInterval('positionControl()',300);

function onHTMLload(){
	window.status = 'Starting up menu...';
	startMenu();
	window.status = 'Menu started';
	mWriteScrollLayers();
	window.status = 'Menu finished';
	window.finished = true;
}
h = 0;
w = 0;
tableWidth = tableWidthDefault;
mFromLeft = mFromLeftDefault;
function onBodyLoad(){
	if (isAdmin){ checkchanges('init'); }
	//h = (window.innerHeight)?window.innerHeight:document.body.clientHeight;
	//h = h + rememberY;
	//w = (window.innerWidth)?window.innerWidth:document.body.clientWidth;
	h = window.innerHeight || document.body.clientHeight || document.body.offsetHeight;
	w = document.body.clientWidth || window.innerWidth || document.body.offsetWidth;
	if (tableWidthDefault == 0){ tableWidth = w - margin; }
	mFromLeft = parseInt((w - tableWidth) / 2) + mFromLeftDefault;
	m(0);
	temp = getOffsetFromTop();
	positionLayer('lyrMenuPath',(tableWidth == 0)?margin:(w / 2 - tableWidth / 2) + mPathFromLeft,mPathFromTop - temp)
	positionLayer('lyrZoekForm',(tableWidth == 0)?margin:(w / 2 - tableWidth / 2) + zoekFormFromLeft,zoekFormFromTop - temp)
	positionLayer('me0',(tableWidth == 0)?0:(w / 2 - tableWidth / 2),0 - temp)
	window.loaded = true;
}
function onBodyResize(){
	if (document.layers){ document.location.reload(); }
	//h = (window.innerHeight)?window.innerHeight:document.body.clientHeight;
	//h = h + rememberY;
	//w = (window.innerWidth)?window.innerWidth:document.body.clientWidth;
	h = window.innerHeight || document.body.clientHeight || document.body.offsetHeight;
	w = document.body.clientWidth || window.innerWidth || document.body.offsetWidth;
	if (tableWidthDefault == 0){ tableWidth = w - margin; }
	mFromLeft = parseInt((w - tableWidth) / 2) + mFromLeftDefault;
	mhideimmediately();
	setTimeout('afterBodyResize();',1000);
}
function afterBodyResize(){
	temp = getOffsetFromTop();
	positionLayer('lyrMenuPath',(tableWidth == 0)?margin:(w / 2 - tableWidth / 2) + mPathFromLeft,mPathFromTop - temp)
	positionLayer('lyrZoekForm',(tableWidth == 0)?margin:(w / 2 - tableWidth / 2) + zoekFormFromLeft,zoekFormFromTop - temp)
	positionLayer('me0',(tableWidth == 0)?0:(w / 2 - tableWidth / 2),0 - temp)
}
function onBodyUnload(){
	if (dateassistwindow){
		if (!dateassistwindow.closed){ dateassistwindow.close(); }
	}
	if (isAdmin){ return saveChanges(); }else{ return true; }
}

mFromTop = mFromTopDefault;

mScrollTime = mDefaultScrollTime;
var xPos = 0;
var yPos = 0;
function setTo(e){
	if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Opera") != -1){
		yPos = event.clientY;// + document.body.scrollTop;
		xPos = event.clientX;// + document.body.scrollLeft;
	}else{
		yPos = e.pageY - getOffsetFromTop();
		xPos = e.pageX;
	}
}
function positionLayer(lyr,x,y,z,lyrWidth){ //document.zoekForm.q.value=lyr;
	y = y + rememberY; //smooth position while scrolling
	if (!z){ z = 1; }
	if (document.layers){
		document.layers[lyr].left = x;
		document.layers[lyr].top = y;
		document.layers[lyr].zIndex = z;
	}else if (document.all){
//try{
		document.all[lyr].style.left = x;
		document.all[lyr].style.top = y;
		document.all[lyr].style.zIndex = z;
//}catch(e){ alert(lyr+','+x+','+y+','+z+'\n'+i+'\n'+e.description); }
	}else{
		document.getElementById(lyr).style.left = x;
		document.getElementById(lyr).style.top = y;
		document.getElementById(lyr).style.zIndex = z;
	}
}
arr = new Array(); arri = new Array(); arrCountingAt = new Array();
arrCountingAt[0] = 0; arri[0] = 0; //remember 0th menu manually!!!!

function startMenu(){ //from = build manually with the javascript func wm(), if not (try to) copy it now
	if (from.length < 1) {from = (document.all)?document.all.tags('div'):document.getElementsByTagName('div');}
	cleanCounter = 1;
	for (cStartMenu=0; cStartMenu < from.length; cStartMenu++){ //alert(from[cStartMenu].id);
		startMenuLyr = from[cStartMenu];
		if (startMenuLyr.id.indexOf('me') == 0){
			i = parseInt(startMenuLyr.id.replace('me','')); //i = i from db
			d = parseInt(startMenuLyr.id.replace('me'+i+'nu','')); // d = d from db
			arr[d] = (arr[d])?arr[d] + ',{' + i + '}':'{' + i + '}'; //input all id's belonging to this id
			arri[cleanCounter] = i; cleanCounter++; //remember existing id's
			arrCountingAt[i] = 0; //remember position of submenu when scrolling
			//arrUnHover[i] = d; //of what item this item is a sub item?
			//set width of the items in this menu
//ONLY WORKS IN IE FOR NOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//widthOfSubMenu = parseInt(startMenuLyr.offsetWidth);
			//if (!arrWidth[i]){
			//	arrWidth[i] = widthOfSubMenu;
			//}else if (arrWidth[i] < widthOfSubMenu){
			//	arrWidth[i] = widthOfSubMenu;
			//}
		}
	}
	from = ''; //don't take up unecessary memory space
}

function mWriteScrollLayers(){
	//write scroll layers
	for (cStartMenu = 0; cStartMenu < arri.length; cStartMenu++){
		if (arr[arri[cStartMenu]]){ //this has a submenu
			mWriteTempVar = arri[cStartMenu];
			document.write('<div id="mescrollDownnu')
			document.write(mWriteTempVar);
			document.write('" style="position:absolute;" class="m');
			document.write(skin);
			document.write('"><a href="javascript:;" onMouseOver="mScroll(1,');
			document.write(mWriteTempVar);
			document.write(');" onMouseOut="mScroll(0,');
			document.write(mWriteTempVar);
			document.write(');"><img src="upload');
			document.write(imgDown);
			document.write('" border="0" align="');
			document.write(imgAlign);
			document.write('" /></a></div>\n<div id="mescrollUpnu');
			document.write(mWriteTempVar);
			document.write('" style="position:absolute;" class="m');
			document.write(skin);
			document.write('"><a href="javascript:;" onMouseOver="mScroll(-1,');
			document.write(mWriteTempVar);
			document.write(');" onMouseOut="mScroll(0,');
			document.write(mWriteTempVar);
			document.write(');"><img src="upload');
			document.write(imgUp);
			document.write('" border="0" align="');
			document.write(imgAlign);
			document.write('" /></a></div>\n');
		}
	}
}
function mHover(whatItem,flagHover,mHoverSkin){
//alert(whatItem+': '+flagHover);
	if (flagHover && parseInt(mSkins[mHoverSkin]) != mSkins[mHoverSkin]){ mHoverSkin = parseInt(mHoverSkin); }
	if (document.layers){
		document.layers[whatItem].bgColor = (flagHover)?color:'#FFFFFF';
	}else if (document.all){
		document.all[whatItem].className = (flagHover)?'m'+mHoverSkin+'Hovered':'m'+mHoverSkin;
	}else{
		document.getElementById(whatItem).className = (flagHover)?'m'+mHoverSkin+'Hovered':'m'+mHoverSkin;
	}
}
function mUnHover(i){
	iUnHover = 0;
	//if (arrUnHover[i]){ iUnHover = arrUnHover[i]; } //new array held to speed things up
	for (cmUnHover = 0; cmUnHover < arri.length; cmUnHover++){ //get id where this i is in submenu
		if (arr[arri[cmUnHover]]){
			if (arr[arri[cmUnHover]].toString().indexOf('{'+i+'}') != -1){
				iUnHover = arri[cmUnHover];
				break;
			}
		}
	}
	if (arr.length < 1) {
		window.status='Where is the menu??';
	}else{
		if (iUnHover >= 0){
//if (arrShown[iUnHover]){
			arrstrmUnHover = arr[iUnHover].toString();
			arrstrmUnHover = arrstrmUnHover.substr(1,arrstrmUnHover.length - 2);
			if (arrstrmUnHover.indexOf(',') != -1){ //multiple sub items (single doesn't have to be unhovered!!)
				amUnHover = arrstrmUnHover.split('},{'); //extract all id's
				for (cmUnHover = 0; cmUnHover < amUnHover.length; cmUnHover++){
					if (amUnHover[cmUnHover] != i){ //don't unhover the currently selected item
						//lyrName = 'me'+amUnHover[cmUnHover]+'nu'+iUnHover;
						mHover('me'+amUnHover[cmUnHover]+'nu'+iUnHover,false,mSkins[amUnHover[cmUnHover]]);
					}
				}
			//}else{ //no array but single sub item
			//	amUnHover = arrstrmUnHover;
			//	lyrName = 'me'+amUnHover+'nu'+iUnHover;
			//	mHover(lyrName,false);
			}
		}
//}
	}
}
function mgetlevel(i){
	tempi = i; templ = 0; flagHovered = false;
	while (tempi != 0){
		templ++;
		for (cmlevel = 0; cmlevel <= arri.length; cmlevel++){ //walk through existing id's
			//if (arrUnHover[tempi] == arri[cmlevel]){
			//	tempi = arri[cmlevel];
			//	if (!flagHovered){
					//show hovering of the first found menu item
			//		mHover('me'+i+'nu'+tempi,true);
			//		flagHovered = true;
			//	}
			//	break; //for loop
			//}
			if (arr[arri[cmlevel]]){ //if current id has a submenu / string of sub id's
				if (arr[arri[cmlevel]].toString().indexOf('{'+tempi+'}') != -1){ //if this id is a submenu of this other id
					tempi = arri[cmlevel];
					if (!flagHovered){
						//show hovering of the first found menu item
						mHover('me'+i+'nu'+tempi,true,mSkins[i]);
						flagHovered = true;
					}
					break; //for loop
				}
			}
		}
	}
	return templ;
}
function mgetfromleft(i){
	tempi = i; mgetfromleftdone = false; mgetfromleftlyr = '';
	while (tempi != 0 && !mgetfromleftdone){
		for (cmlevel = 0; cmlevel <= arri.length; cmlevel++){ //walk through existing id's
			if (arr[arri[cmlevel]]){ //if current id has a submenu / string of sub id's
				if (arr[arri[cmlevel]].toString().indexOf('{'+tempi+'}') != -1){ //if this id is a submenu of this other id
				//if (arrUnHover[tempi] == arri[cmlevel]){
					tempi = arri[cmlevel];
					mgetfromleftlyr = 'me'+i+'nu'+tempi;
					mgetfromleftdone = true;
					break;
				}
			}
		}
	}
	//document.zoekForm.q.value=mgetfromleftlyr;
	mgetfromleftvalue = 0
	if (document.layers){
		mgetfromleftvalue = parseInt(document.layers[mgetfromleftlyr].left);
	}else if (document.all){
		mgetfromleftvalue = parseInt(document.all[mgetfromleftlyr].style.left);
	}else{
		mgetfromleftvalue = parseInt(document.getElementById(mgetfromleftlyr).style.left);
	}
	return mgetfromleftvalue;
}
function mScroll(direction,i){
	if (direction != 0){
//document.zoekForm.zoek.value = i+' - '+direction+', counting at: '+arrCountingAt[i]
		//clearTimeout(tomScroll);
		if (arrCountingAt[i] + direction < 0){ direction = 0; }
		//m(i,arrCountingAt[i] + direction); //compared against arrCountingAt to see if it is scrolling
		arrCountingAt[i] = m(i,arrCountingAt[i] + direction); //arrCountingAt[i] + direction;
		tomScroll = setTimeout('mScroll('+direction+','+i+')',mScrollTime);
	}else{
		clearTimeout(tomScroll);
	}
}
tomScroll = setTimeout('mScroll(0,0);',20); // will timeout itself immediately

tomhideall = setTimeout('mhide(0);',mHideAfter);
clearTimeout(tomhideall);

function m(i,startCountingAt,menutoolstbd,menutoolstbi){
	clearTimeout(tomhideall);
	if (!startCountingAt){ //(startCountingAt == undefined){
		startCountingAt = 0; //arrCountingAt[0]; ?? Waarom was dit?
		arrCountingAt[i] = 0; //this is not a scroller
	}
	mlevel = mgetlevel(i);
	mUnHover(i);
	//xm = (i == 0)?mFromLeft:xPos;
	if (i == 0){ //initialise variables
		//h = (window.innerHeight)?window.innerHeight:document.body.clientHeight;
		//w = (window.innerWidth)?window.innerWidth:document.body.clientWidth;
		//mFromLeft = parseInt((w - tableWidth) / 2);
	}
	//parseInt((xPos - mFromLeft) / mItemWidthTop) * mItemWidthTop
	xm = mFromLeft + mlevel * mItemWidth;
	if (i != 0 && topmenuhorizontal){
		if (mlevel == 1){
			xm = mgetfromleft(i);
		}else{
			xm = mgetfromleft(i) + mItemWidth;
		}
	}
	while (xm + mItemWidthDiv > tableWidth + mFromLeft){ //show submenu to the left if it exceeds content width
		xm = xm - mItemWidthDiv - mItemWidth + (mItemWidthDiv - mItemWidth);
	}
	ym = (i == 0)?mFromTop:yPos;
	if (i >= 0){
		//close all submenu's below this one
		if (i == 0){
			mhide(0,true); //hide main menu, true prevents the hide function from recalling the main menu
		}else{
			for (cm = 0; cm <= arri.length; cm++){ //walk through existing id's
				if (arrShown[arri[cm]]){
					if (arr[arri[cm]]){ //if current id has a submenu / string of sub id's
						if (arr[arri[cm]].toString().indexOf('{'+i+'}') != -1){ //if this id is a submenu of this other id
						//if (arrUnHover[i] == arri[cmlevel]){ //if this id is a submenu of this other id
							arrstrm = arr[arri[cm]].toString();
							arrstrm = arrstrm.substr(1,arrstrm.length - 2);
							if (arrstrm.indexOf(',') != -1){ //if multiple sub items are present
								am = arrstrm.split('},{'); //extract all id's
								for (cmtoo = 0; cmtoo < am.length; cmtoo++){
									mhide(am[cmtoo]);
								}
							}else{ //no array but single sub item
								mhide(arrstrm);
							}
						}
					}
				}
			}
			if (!assigningSub && isAdmin){
				displayMenuTools(i,false,menutoolstbd,menutoolstbi,mgetfromleft(i)+mItemWidth/2,parseInt(ym/mItemHeight)*mItemHeight+getOffsetFromTop());
			}
		}
		if (arr[i] && (!assigningSub || assigningSub != i)){//you can't assign an item to a submenu of itself!!
			//show submenu for this id
			arrstrm = arr[i].toString();
			arrstrm = arrstrm.substr(1,arrstrm.length - 2);
			am = arrstrm.split('},{'); //extract all id's
			//check height of this menu and adjust starting position accordingly
			while ((ym + am.length * mItemHeight + mFromBottom) > h){
				ym = ym - mItemHeight;
				if (ym < mFromTop + mItemHeight){ break; }
			}
//document.zoekForm.q.value = i+' :: '+startCountingAt+' :: '+arrCountingAt[i];
			if (startCountingAt != arrCountingAt[i]){
				if (startCountingAt > 0){
					positionLayer('mescrollUpnu'+i,xm,mFromTop - mItemHeight,100 + mlevel); //display scroll up layer first
					//return startcountingat to max when it has gone too far
					while (am.length - startCountingAt < (h - mFromTop - mFromBottom) / mItemHeight - 1){
						startCountingAt--;
					}
				}
				ym = mFromTop; //reset location to top of menu
			}else{ //stop scrolling (in case scrolling just reached the top of the menu)
				clearTimeout(tomScroll);
			}
			if (ym < mFromTop){ ym = mFromTop; } //reset start of menu to minimum when it has gone too far (scrolling up)
			for (cm = startCountingAt; cm < am.length; cm++){
//document.zoekForm.zoek.value = cm+' van '+am.length+' = '+am[cm];
				if (i==0 && topmenuhorizontal){
					mItemFromLeft = mFromLeft + cm * mItemWidthTop;
					lyrName = 'me'+am[cm]+'nu'+i;
					positionLayer(lyrName,mItemFromLeft,mFromTop,100 + mlevel);
					//accomodate too many items...
				}else{
					//mItemFromLeft = xm;
					mItemFromTop = mItemHeight * (cm - startCountingAt + parseInt(ym / mItemHeight));
					if (mlevel == 1 && topmenuhorizontal){ //adjust position to be under top bar
						//mItemFromLeft = mFromLeft + parseInt((xPos - mFromLeft) / mItemWidthTop) * mItemWidthTop;
						mItemFromTop = mItemFromTop + mItemHeight;
					}
					if (mItemFromTop + 2 * mItemHeight + mFromBottom > h && am.length > cm + 1){
//(cm + 2 - startCountingAt) * mItemHeight + mFromTop
						positionLayer('mescrollDownnu'+i,xm,mItemFromTop,100 + mlevel);
						break;
					}else{
						lyrName = 'me'+am[cm]+'nu'+i;
						positionLayer(lyrName,xm,mItemFromTop,100 + mlevel);
					}
				}
			}
		}
		tomhideall = setTimeout('mhide(0);',mHideAfter);
		arrShown[i] = true;
		return startCountingAt;
	}
}
function mcollect(i,toHide){ //collects layers that need hiding
	cToHide = toHide.length; //continue filling where left of
	if (arr[i]){
		if (arrShown[i]){
			arrstrmcollect = arr[i].toString();
			arrstrmcollect = arrstrmcollect.substr(1,arrstrmcollect.length - 2);
			if (arrstrmcollect.indexOf(',') != -1){ //multiple sub items
				amcollect = arrstrmcollect.split('},{'); //extract all id's
				for (cmcollect = 0; cmcollect < amcollect.length; cmcollect++){
					lyrName = 'me'+amcollect[cmcollect]+'nu'+i;
					if (mShown(lyrName)){ //only hide shown menu's
						toHide[cToHide] = lyrName; cToHide++;
					}
				}
			}else{ //no array but single sub item
				amcollect = arrstrmcollect;
				lyrName = 'me'+amcollect+'nu'+i;
				toHide[cToHide] = lyrName; cToHide++;
			}
			toHide[cToHide] = 'mescrollUpnu'+i;
			cToHide++;
			toHide[cToHide] = 'mescrollDownnu'+i;
		}
	}
	return toHide;
}
function mShown(whichLayer){
	topOfLayer = (document.layers)?document.layers[whichLayer].top:(document.all)?document.all[whichLayer].style.top:document.getElementById(whichLayer).style.top;
	return (parseInt(topOfLayer) != -400); //return boolean that indicates whether this layer is shown (true)
}

function mhide(i,calledFromM){
	displayMenuTools(0,true);//!i);//true for 'hide'
	arrToHide = new Array();
	arrToHide = mcollect(i,arrToHide); //initialise first run
	cmhide = 0;
	while (cmhide < arrToHide.length){
//window.status=cmhide + ' van ' + arrToHide.length;
		mcollect(parseInt(arrToHide[cmhide].toString().replace('me','')),arrToHide); //append all sub menu's
		cmhide++;
	}
	for (cmhide = 0; cmhide < arrToHide.length; cmhide++){
		positionLayer(arrToHide[cmhide],-400,-400);
//UNHOVER CLASS NOG FIXEN!!!
		lyrToHide = arrToHide[cmhide];
		mHover(lyrToHide,false,mSkins[parseInt(lyrToHide.replace('me',''))]); //hidden menu items should not be hovered
	}
	arrShown[i] = false; //might be set to true by the next condition
	if (i == 0){ 
		clearTimeout(tomhideall);
		if (!calledFromM){ m(0); }//,arrCountingAt[0]+1); //(re-)show first level of menu
	}
}

function mhideimmediately(){
	mhide(0);
}
if (navigator.userAgent.indexOf("MSIE") == -1) document.captureEvents(Event.MOUSEUP);
document.onmouseup = mhideimmediately;
if (navigator.userAgent.indexOf("MSIE") == -1) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = setTo;