// @@@
// @@ Author: Bas van Dorst
// @@ Date: 2007-08-03
// @@ Last change: 2007-08-03
// @@ Version: 1
// @@ Revision: 0
// @@ Copyright: Ecommany B.V.
// @@ Description: Menu_script.js voor het verzorgen van het menu op de Cultuurnetwerk website
// @@--> v1.0;   Bas van Dorst
// @@-->	   	- Eerste versie tbv versiebeheer
// @@-->	   	- ParentNode van de geselecteerde pagina moet gekleurd zijn
// @@-->	   	- Bij het klikken van de ParentNode moet de eerste childNode geopend worden
// @@-->		- ParentNode non-clickable maken bij selectie eerste child	
// @@--> v2.0;	 Bas van Dorst
// @@-->		- URL decoden: elke location door decodeURIComponent halen
// @@-->		- URL toLowerCase()
// @@$

var openLists = [], oIcount = 0; // global vars


// @@*
// @@ Function: compactMenu
// @@ Description: Menu compacten
function compactMenu(oID,oAutoCol,oPlMn,oMinimalLink) {
	   if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return; }
	   var baseElement = document.getElementById( oID ); if( !baseElement ) { return; }
	   compactChildren( baseElement, 0, oID, oAutoCol, oPlMn, baseElement.tagName.toUpperCase(), oMinimalLink && oPlMn );
	   selfLink('jsMenu','selected',true);
	   printNiveau(getCurrentLayer());

}

// @@*
// @@ Function: compactChildren
// @@ Description: Na het uitklappen van het menu alle childs compacten 
function compactChildren( oOb, oLev, oBsID, oCol, oPM, oT, oML ) {
	   if( !oLev ) { oBsID = escape(oBsID); if( oCol ) { openLists[oBsID] = []; } }
	   for( var x = 0, y = oOb.childNodes; x < y.length; x++ ) { if( y[x].tagName ) {
			   //for each immediate LI child
			   var theNextUL = y[x].getElementsByTagName( oT )[0];
			   if( theNextUL ) {
				   	//collapse the first UL/OL child
				   	theNextUL.style.display = 'none';
				   	//create a link for expanding/collapsing
				   	var newLink = document.createElement('A');
				   	newLink.setAttribute( 'href', '#' );
					newLink.onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + oBsID + '\',' + oCol + ',\'' + escape(oT) + '\');return false;' );
				   //wrap everything upto the child U/OL in the link
				  if( oML ) { var theHTML = ''; } else {
						   var theT = y[x].innerHTML.toUpperCase().indexOf('<'+oT);
						   var theA = y[x].innerHTML.toUpperCase().indexOf('<A');
						   var theHTML = y[x].innerHTML.substr(0, ( theA + 1 && theA < theT ) ? theA : theT );
						   while( !y[x].childNodes[0].tagName || ( y[x].childNodes[0].tagName.toUpperCase() != oT && y[x].childNodes[0].tagName.toUpperCase() != 'A' ) ) {
							   y[x].removeChild( y[x].childNodes[0] ); }
				   }
				   y[x].insertBefore(newLink,y[x].childNodes[0]);
					y[x].childNodes[0].innerHTML = oPM + theHTML.replace(/^\s*|\s*$/g,'');
				   theNextUL.MWJuniqueID = oIcount++;
				   compactChildren( theNextUL, oLev + 1, oBsID, oCol, oPM, oT, oML );
				   } 
			   } 
	   } 

}


// @@*
// @@ Function: clickSmack
// @@ Description: Openklappen van het geselecteerde item
function clickSmack( oThisOb, oLevel, oBsID, oCol, oT ) {
	   if( oThisOb.blur ) { oThisOb.blur(); }
	   oThisOb = oThisOb.parentNode.getElementsByTagName( unescape(oT) )[0];
	   if( oCol ) {
			   for( var x = openLists[oBsID].length - 1; x >= oLevel; x-=1 ) { if( openLists[oBsID][x] ) {
				   openLists[oBsID][x].style.display = 'none'; if( oLevel != x ) { openLists[oBsID][x] = null; }
			   } }
			   if( oThisOb == openLists[oBsID][oLevel] ) { openLists[oBsID][oLevel] = null; }
			   else { oThisOb.style.display = 'block'; openLists[oBsID][oLevel] = oThisOb; }
	   } else { oThisOb.style.display = ( oThisOb.style.display == 'block' ) ? 'none' : 'block'; }

}

// @@*
// @@ Function: stateToFromStr
// @@ Description: Bekijken welk item er overeenkomt met de URL 
function stateToFromStr(oID,oFStr) {
	if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return ''; }
		var baseElement = document.getElementById( oID ); if( !baseElement ) { return ''; }
		if( !oFStr && typeof(oFStr) != 'undefined' ) { return ''; } if( oFStr ) { oFStr = oFStr.split(':'); }
		for( var oStr = '', l = baseElement.getElementsByTagName(baseElement.tagName), x = 0; l[x]; x++ ) {
		if( oFStr && MWJisInTheArray( l[x].MWJuniqueID, oFStr ) && l[x].style.display == 'none' ) { 
			l[x].parentNode.getElementsByTagName('a')[0].onclick(); 
			l[x].parentNode.childNodes[1].className = "selected";
		} else if( l[x].style.display != 'none' ) { 
			oStr += (oStr?':':'') + l[x].MWJuniqueID;  
		}
	}	   
	return oStr;
}


// @@*
// @@ Function: MWJisInTheArray
// @@ Description: Bekijken of het item in de array zit
function MWJisInTheArray(oNeed,oHay) { 
	for( var i = 0; i < oHay.length; i++ ) { if( oNeed == oHay[i] ) { return true; } } return false; }


// @@*
// @@ Function: selfLink
// @@ Description: Openen van root-element, en child-elements
function selfLink(oRootElement,oClass,oExpand,oLink) {
	   var tmpLink;
	   if(!document.getElementsByTagName||!document.childNodes) { return; }
	   oRootElement = document.getElementById(oRootElement);
	   if( oLink ) {
			   tmpLink = document.createElement('a');
			   tmpLink.setAttribute('href',oLink);
	   }
	   
		for( var x = 0, y = oRootElement.getElementsByTagName('a'); y[x]; x++ ) {
			loc = ''+decodeURIComponent(location);
			loc = loc.toLowerCase();
			var sLocation = getRealLocation();
			
			dir='/';
			aDirectory = loc.split('/');
			for (i=3; i<aDirectory.length-1; i++) {
			   dir+=(aDirectory[i]+'/');
			}
			  	if( dir == y[x].getAttribute('href') || sLocation == sUrlWebsite+y[x].getAttribute('href')) {	   
	   			y[x].parentNode.className = "selected";
				   if( oExpand ) {
						   oExpand = false;
						   for( var oEl = y[x].parentNode, ulStr = ''; oEl != oRootElement && oEl != document.body; oEl = oEl.parentNode ) {
							   if( oEl.tagName && oEl.tagName == oRootElement.tagName ) {
									   ulStr = oEl.MWJuniqueID + (ulStr?(':'+ulStr):''); 
							   } 
						   }
						   stateToFromStr(oRootElement.id,ulStr);			   
				   } 
			   } 
	   } 

}


// @@*
// @@ Function: getRealAddress
// @@ Description: Echte URL acterhalen van de link
function getRealLocation() { 
	loc = ''+decodeURIComponent(location);		
	loc = loc.toLowerCase();	
	aLocation = loc.split('?');
	sLocation = aLocation[0];	
	var aLocation = sLocation.split('#');
	var sLocation = aLocation[0]
	return sLocation;
}
// @@*
// @@ Function: printNiveau
// @@ Description: Echte URL acterhalen van de link
function printNiveau(niv) {
	if(!niv) niv=0;
	   if(niv > 3) niv = 1;
	   if(getHeadItem() !== false) niv= getHeadItem();
	   var nivItem = "item"+niv;
	   document.getElementById("item1").className="line_color2";
	   document.getElementById("item2").className="line_color2";
	   document.getElementById("item3").className="line_color2";

	   if(niv ==0) {
			   document.getElementById("column4").id="column4_inc_spacing";
			   document.getElementById("col2_item").id="col2_item1"
	   } else {
			   document.getElementById(nivItem).className="line_color1";
			   document.getElementById("col2_item").id="col2_"+nivItem;	   
	   }

}

// @@*
// @@ Function: printboom
// @@ Description: Vanuit de array menu een gehele lijst printen
function printboom(parent,tree, depth, niv) {
			   
		loc = ''+decodeURIComponent(location);
		loc = loc.toLowerCase();		
		var sLocation = getRealLocation();
	   
	   dir='/';
	   aDirectory = loc.split('/');
	   for (i=3; i<aDirectory.length-1; i++) {
		   dir+=(aDirectory[i]+'/');
	   }
	   
		for(i in tree) { 
			   if(niv != 0) { 
				   if(tree[i].name) { 
						   // als er de hyperlink # is, alleen een naam wegschrijven, zonder url
						   if(tree[i].hyperlink == "#") {
							   //alert(tree[i].chil.name);
							   var li = document.createElement("li");
							   x = parseInt(i);
							   if (tree[x+1]) {
								var backlink = document.createElement("a");
								
								if(sUrlWebsite+tree[x+1][0].hyperlink ==sLocation || tree[x+1][0].hyperlink == dir) {
								backlink.setAttribute("href","#");
								}else {
								 backlink.setAttribute("href", tree[x+1][0].hyperlink);
								}
								backlink.appendChild(document.createTextNode(tree[i].name));
							   }
							   var txt = document.createTextNode(tree[i].name);
							   li.appendChild(backlink);
							   
							   par = parent.appendChild(li);
							   var ul = document.createElement("ul");
							   newparent = par.appendChild(ul);
						   } else {
							   // url zoals die gedefinieerd staat in menu_conf, in een string zetten
							   url = tree[i].hyperlink;
							   // bekijken of er http in de url zit 
							   var li = document.createElement("li");
							   var backlink = document.createElement("a");
							   backlink.setAttribute("href", url);
							   // zo ja, url wegschrijven met target=_blank
							   if(url.substring(0,7)== "http://"){
									   backlink.setAttribute("target", "_blank");
							   } 
							   backlink.appendChild(document.createTextNode(tree[i].name));
							   li.appendChild(backlink);   
							   parent.appendChild(li);
						   } // else
				   } else { 
						   if(typeof tree[i] == "object") { 
							   // als het weer een object betreft, nieuwe 'tak' printen
							   printboom(newparent,tree[i], depth+1); 
						   } // 
				   } // else
			   } // if
	   } // for
}

// @@*
// @@ Function: Link
// @@ Description: Menu in een object stoppen
function Link(name, hyperlink) {
  this.name = name;
  this.hyperlink = hyperlink;
  

}


// @@*
// @@ Function: getCurrentLayer
// @@ Description: Bekijk de huidige laag
function getCurrentLayer(){
			   
		loc = ''+decodeURIComponent(location);			
		loc = loc.toLowerCase();
		var sLocation = getRealLocation();
	   
		dir='/';
		aDirectory = loc.split('/');
		for (i=3; i<aDirectory.length-1; i++) {
			dir+=(aDirectory[i]+'/');
		}
				   
	   var niveau = new Array(menu0,menu1,menu2,menu3);
	   // niveau's doorlopen
	   for(laag in niveau) {	 
			   for(i in niveau[laag]) {
				   if(!niveau[laag][i].name) {
						   for (z in niveau[laag][i]) {
							   if(niveau[laag][i][z].name) {
									   link = sUrlWebsite+niveau[laag][i][z].hyperlink;
									   if(dir == niveau[laag][i][z].hyperlink || sLocation == link) {
										   return(laag);
									   }
							   }
						   }
				   } else {
						   if(niveau[laag][i].name) { 
							   link = sUrlWebsite+niveau[laag][i].hyperlink;
							   if(dir == niveau[laag][i].hyperlink || sLocation == link) {
									   return(laag);
							   }
						   }
				   } 
			   }
	   } 

}

// @@*
// @@ Function: getHeadItem
// @@ Description: Hoofditem bepalen
function getHeadItem() {
	   // 3 takken definieren	  
	   loc = ''+decodeURIComponent(location);		
	   loc = loc.toLowerCase();	   
	   aDirectory = loc.split('/');
	   
	   if(aDirectory[3] === "english") {
			   return "1";
	   } else if(aDirectory[3] === "products_and_services") {
			   return "2";
	   } else if(aDirectory[3] === "european_cooperation") {
			   return "3";
	   } else {
			   return false;
	   }

}


// @@*
// @@ Function: getCurrentMenu
// @@ Description: Juiste menu returnen n.a.v. huidige laag
function getCurrentMenu() {
	   var niveau = new Array(menu0,menu1,menu2,menu3);		   
	   if(getHeadItem() !== false) return niveau[getHeadItem()];
	   for(laag in niveau) { 
			   if(laag == getCurrentLayer()) {
				   return niveau[laag];
			   }
	   }

}