var menuCounter = 0;
var theMenu ='';
var language_cookie_value = "";
var language_cookie_name = "user_language";
var pageLanguage = "";

function activateHeaderItem(headerItem){
	var headerItemColor;
	var headerBackgroundColor;
	headerItemColor = getHeaderColor(headerItem);
	headerBackgroundColor = getBackgroundColor(headerItem);
	
	activeMenuColor = headerItemColor;
	activeBackgroundColor = headerBackgroundColor
	//document.getElementById('menu').style.color = headerItemColor;
	document.getElementById('content').style.borderColor = headerItemColor;
	document.getElementById('banner').style.borderColor = headerItemColor;
	try{
		var kop1Arr = document.getElementsByClassName('kop1');
		var kop2Arr = document.getElementsByClassName('kop2');
		for(var i=0;i<kop1Arr.length;i++){
			//kop1Arr[i].style.backgroundColor=headerBackgroundColor;
			kop1Arr[i].style.backgroundColor="#fafafa";
			kop1Arr[i].style.color=headerItemColor;
		}
		for(var i=0;i<kop2Arr.length;i++){
			kop2Arr[i].style.borderColor=headerItemColor;
		}
	}catch(err){
		var divs = document.getElementsByTagName("div");
		for(var i=0;i<divs.length;i++){
			if(divs[i].className=="kop1"){
			//divs[i].style.backgroundColor=headerBackgroundColor;
				divs[i].style.backgroundColor="#fafafa";
				divs[i].style.color=headerItemColor;
			}
			if(divs[i].className=="kop2"){
				divs[i].style.borderColor=headerItemColor;
			}
		}
	}
	//document.getElementById('content').style.backgroundImage = "url('../img/gradient_"+headerItem+".png')";
}

function checkCookie(){
	var language=getCookie(language_cookie_name);
	if (language!=null && language!=""){
  	if(language=="NL"){
  		window.location="./NL/isoproc_isolatie.php";
  	}else{
  		window.location="./FR/isoproc_isolation.php";
  	}
  }
}

function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function setLanguageCookie(){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+30);
	var cookieString = language_cookie_name+ "=" +escape(language_cookie_value)+"; expires="+exdate.toUTCString()+"; path=/";
	document.cookie=cookieString;
}

function generateItem(itemNumber,submenuDepth,itemArray,isRoot,menuKind){
	var hasSubMenu = false;
	if(itemArray[4].length > 0){
		hasSubMenu = true;
	}else{
		submenuDepth ++;
	}
	var anItem='';
	anItem = '<div class="menuItem" id="menuItem' + itemNumber +'" style="margin-left:'+(submenuDepth*15)+'px;'+ (itemArray[2]==0?'display:none;':'') +'">';
	if(hasSubMenu){
		anItem += '<img src="http://www.isoproc.be/img/'+(itemArray[4][0][2]==0?'add':'minus')+'.png"';
		anItem += ' onclick="showHideMenu('+itemNumber+','+(itemArray[4][0][2]==0?'1':'0')+',false,'+"'"+menuKind+"'"+');"/>&nbsp;';
	}
	anItem += '<span id="menuTekst'+itemNumber+'" class="menuTekst '+ (isRoot?menuKind:'') +'" onmouseover="highlightMenu('+itemNumber+',true,getBackgroundColor('+"'"+menuKind+"'"+'));" ';
	anItem += 'onmouseout="highlightMenu('+itemNumber+',false);" ><a class="'+ (isRoot?menuKind:'') +'" href="' + itemArray[3] + '">'+itemArray[1]+'</a></span>';
	anItem += '</div>';
	return anItem;
}

function getBackgroundColor(headerItem){
	switch(headerItem){
		case "advisering":
			return "#CAEEB5";
			break;
		case "instituut":
			return "#96E2EB";
			break;
		case "nieuwsbrief":
			return "#f4f5f5";
			break;
		case "symposium":
			return "#d2d3d4";
			break;
		case "isoproc":
			return "#C7C7C7";
			break;
		case "oplossing":
			return "#C278A5";
			break;
	}
}

function getHeaderColor(headerItem){
	switch(headerItem){
		case "advisering":
			return "#67AE3E";
			break;
		case "instituut":
			return "#009DAD";
			break;
		case "nieuwsbrief":
			return "#8b8c8f";
			break;
		case "symposium":
			return "#d2d3d4";
			break;
		case "isoproc":
			return "#282220";
			break;
		case "oplossing":
			return "#872460";
			break;
	}
}

function highlightMenu(highlightedItem,show,menuColor){
	var highlightid = "menuTekst" + highlightedItem;
	if(show){
		document.getElementById(highlightid).style.backgroundColor=menuColor;
	}else{
		document.getElementById(highlightid).style.backgroundColor="";
		if(highlightedMenuItem!=100){
			document.getElementById("menuTekst" + highlightedMenuItem).style.backgroundColor=activeBackgroundColor;
		}
	}
}

function loadFullMenu(){
	//theMenu+='<div style="width:100%;border:1px solid #6B9148;padding-left:5px;margin-right:100px;">';
	loadMenu(oplossingenMenuArray,0,true,'oplossing');	
	//theMenu+="</div>";
	theMenu+="<br>";
	loadMenu(adviseringMenuArray,0,true,'advisering');
	theMenu+="<br>";
	loadMenu(instituutMenuArray,0,true,'instituut');
	theMenu+="<br>";
	loadMenu(nieuwsbriefMenuArray,0,true,'nieuwsbrief');
	theMenu+="<br>";
	loadMenu(symposiumMenuArray,0,true,'symposium');
	theMenu+="<br>";
	loadMenu(isoprocMenuArray,0,true,'isoproc');	
}

function loadMenu(itemArray,menuDepth,isRoot,menuKind){
	for (var i=0; i<itemArray.length; i++){
		//menuCounter ++;
		//itemArray[i][0]=menuCounter;
		theMenu += generateItem(itemArray[i][0],menuDepth,itemArray[i],isRoot,menuKind);
		if(itemArray[i][4].length>0){
			loadMenu(itemArray[i][4],menuDepth+1,false,menuKind);
		}
	}
}

function menuArrayToString(menuArray){
	var resultString="["
	for (var i=0; i<menuArray.length; i++){
		resultString+='[' + menuArray[i][0] + ',"' + menuArray[i][1] + '",' + menuArray[i][2] + ',"' + menuArray[i][3] + '",';
		if(menuArray[i][5].length>0){
			resultString+=menuArrayToString(menuArray[i][4]);
		}else{
			resultString +='[]';
		}
		resultString +=']';
		if(i<menuArray.length-1){
		resultString +=',';
		}
	}
	resultString +=']';
	return resultString;
}

function showHideMenu(itemNumber,showSubmenus,updateHighlighted,showHideArrayString){
	if(updateHighlighted){
		highlightedMenuItem = itemNumber;
	}
	var showHideArray;
	switch(showHideArrayString){
		case "advisering":
			showHideArray = adviseringMenuArray;
			break;
		case "instituut":
			showHideArray = instituutMenuArray;
			break;
		case "nieuwsbrief":
			showHideArray = nieuwsbriefMenuArray;
			break;
		case "symposium":
			showHideArray = symposiumMenuArray;
			break;
		case "isoproc":
			showHideArray = isoprocMenuArray;
			break;
		case "oplossing":
			showHideArray = oplossingenMenuArray;
			break;
	}
	updateMenuItemArray(showHideArray,itemNumber,showSubmenus);
	menuCounter = 0;
	theMenu = "";
	loadFullMenu();
	document.getElementById('menus').innerHTML=theMenu;
	highlightMenu(highlightedMenuItem,true,activeBackgroundColor);
}

function updateMenuItemArray(theArray,itemNumber,showSubmenus){
	for (var i=0; i<theArray.length; i++){
		if(theArray[i][0] == itemNumber){
			for(var j=0;j<theArray[i][4].length;j++){
				theArray[i][4][j][2]=showSubmenus;
				if(showSubmenus==0){
					updateMenuItemArray(theArray[i][4],theArray[i][4][j][0],showSubmenus);
				}
			}
		}else{
			if(theArray[i][4].length > 0){
				updateMenuItemArray(theArray[i][4],itemNumber,showSubmenus);
			}
		}
	}
}

function updateMenuLoading(theArray){
	for(var i=0;i<activeMenuArray.length;i++){
		updateMenuItemArray(theArray,activeMenuArray[i],1);
	}
}

function updateScreenSize(){
	if(navigator.appName.indexOf("Microsoft")==-1){
		document.getElementById('content').style.height=(document.body.clientHeight-145)+"px";
		document.getElementById('menus').style.height=(document.body.clientHeight-125)+"px";
	}else{
		document.getElementById('background').style.height=(document.body.clientHeight)+"px";
		document.getElementById('content').style.height=(document.body.clientHeight-145)+"px";
		document.getElementById('menus').style.height=(document.body.clientHeight-125)+"px";
	}
}

