window.amountOfItemsInTopMenu = 1;

//Markerer det valgte punkt i toppen :
var findSelected = false;
window.topMenuActive = "";

window.aTopMenus = new Array ();

//topMenu
function topMenu(sText, sImage1, sImage2, sHref) {
	window.aTopMenus [ window.aTopMenus.length ] = new Array ( sText, sImage1, sImage2, sHref );
}

function menu() { 
	surveillence ();

	window.amountOfItemsInTopMenu =  aTopMenus.length;
	
	for ( var i=0; i < amountOfItemsInTopMenu; i++){
	
		//Create Div which contain <a> and textNode :
		var eMenuTextContainer = document.createElement( "div" );
		eMenuTextContainer.className = "floater02" ;
		
		//var eSpacer = document.createElement( "div" );
		//eSpacer.className = "menuSpacerPixel";
		
		//Create <a> element :
		var aMenuElementA = document.createElement( "a" );
		aMenuElementA.id = "topMenuItem_" + i;
		//aMenuElementA.className = "menuMainDiv01";
		aMenuElementA.onmouseover = fCreateMenuDropDown;
		aMenuElementA.setAttribute( "href", aTopMenus[i][3] );
		
		//Append textNode to <a> :
		var aMenuElementB =  document.createElement ("img");
		aMenuElementB.id = "imgTopMenuItem_" + i;
		aMenuElementB.setAttribute("src", aTopMenus[i][1]);
		aMenuElementB.setAttribute("border", "0");
		aMenuElementA.appendChild (aMenuElementB) ;
		
		//Append textNode to <a> :
		if (i != (amountOfItemsInTopMenu - 1)) {
			var aMenuElementC =  document.createElement ("img");
			aMenuElementC.id = "spacer_" + i;
			aMenuElementC.setAttribute("src", "/images/spacer.gif");
			if(topLanguage == "th") {
				aMenuElementC.setAttribute("width", "0" );
			} else {
				aMenuElementC.setAttribute("width", "0" );	
			}
			aMenuElementC.setAttribute("height", "8");
			aMenuElementC.setAttribute("border", "0");
			aMenuElementA.appendChild (aMenuElementC) ;
		}

		//append menutextnodecontainer :
		//document.getElementById( "menuMainDiv01" ).appendChild( eSpacer );
		document.getElementById( "menuMainDiv01" ).appendChild( eMenuTextContainer );
		eMenuTextContainer.appendChild( aMenuElementA );
	}
	//var eEnd = document.createElement( "div" );
	//eEnd.className = "menuSpacerPixel";
	//document.getElementById( "menuMainDiv01" ).appendChild( eEnd );
}

function fCreateMenuDropDown ( e ){
	if ( !e ) var e = window.event;
	var oNode = e.target ? e.target : e.srcElement;
	if ( oNode.nodeType == 3 ) oNode = oNode.parentNode;

	//Removes dropDownContainers children : 
	while ( document.getElementById( "dropDownContainer" ).hasChildNodes () ) 	
	document.getElementById( "dropDownContainer" ).removeChild ( document.getElementById( "dropDownContainer" ).firstChild )
		
	//finder topMenuId id :
	topMenuId = oNode.id;

	//splitter id'et op i et array :
	sArray = topMenuId.split("_");
	//finder hvilken portlet der er trykket på :
	nTopMenu = Number( sArray[1] );
	
	if(sArray[0] != "spacer" ) {
		var topMenu = document.getElementById( "menuMainDiv01" );
		var ddMenu = document.getElementById( "dropDownContainer" );
		
		var aTopMenuLength = eval( "aTopMenu"+nTopMenu ).length;
		
		for ( var b=0; b < aTopMenuLength; b++ ) {				
			//creates div element :
			var dropdownDiv = document.createElement( "div" );
			dropdownDiv.className = "allLevelsContent";
			var eMenuDropDownElementA = document.createElement( "a" );
			eMenuDropDownElementA.id = "dropdown" + b;
		
			eMenuDropDownElementA.setAttribute( "href", window ["aTopMenu"+nTopMenu][b][2] );
			
			if( window ["aTopMenu"+nTopMenu][b][3] == '_blank' )
				eMenuDropDownElementA.setAttribute( "target", window ["aTopMenu"+nTopMenu][b][3] );
			
			eMenuDropDownElementA.appendChild ( document.createTextNode ( window ["aTopMenu"+nTopMenu][b][0] ) ) ;
	
			ddMenu.appendChild( dropdownDiv ) 
			dropdownDiv.appendChild( eMenuDropDownElementA ) 
		}	
			
		if(aTopMenuLength > 0) {	
		  	//placerer menuen :
			ddMenu.style.visibility = "visible";
			ddMenu.style.top = globalOffsetTop ( document.getElementById( topMenuId )) + 31+ "px";	
			ddMenu.style.left = globalOffsetLeft ( document.getElementById( topMenuId )) + 5 + "px";
		

	
	//if (( topMenuId == "topMenuItem_4" ) || ( topMenuId == "topMenuItem_5" ) || ( topMenuId == "topMenuItem_6" )){
			if(document.getElementById('DivShim')) {
				var IfrRef = document.getElementById('DivShim');
    			IfrRef.style.width = ddMenu.offsetWidth;
    			IfrRef.style.height = ddMenu.offsetHeight;
    			IfrRef.style.top = ddMenu.style.top;
    			IfrRef.style.left = ddMenu.style.left;
    			IfrRef.style.zIndex = 90;
    			IfrRef.style.display = "block";
				ddMenu.style.zIndex = 99;
			}
		} else {
			if(document.getElementById('DivShim')) {
				var IfrRef = document.getElementById('DivShim'); 
    			IfrRef.style.width = ddMenu.offsetWidth;
    			IfrRef.style.height = ddMenu.offsetHeight;
			}			
		}
	} // end test  sArray[0] != "spacer"
	
	//nulstiller menuen
	for ( var i=0; i < window.amountOfItemsInTopMenu; i++ ){
		document.getElementById( "imgTopMenuItem_" + i  ).src = aTopMenus[i][1];  	 	
		document.getElementById( "imgTopMenuItem_" + i  ).height = "38";  	
		
		if (i != (window.amountOfItemsInTopMenu - 1)) {
			var aMenuElementC =  document.createElement ("img");
			document.getElementById( "spacer_" + i ).src =  "/images/spacer.gif";
			if(topLanguage == "th") {
				document.getElementById( "spacer_" + i ).width =  "0";
			} else {
				document.getElementById( "spacer_" + i ).width =  "0";
			}
			document.getElementById( "spacer_" + i ).height =  "8";
			document.getElementById( "spacer_" + i ).border =  "0";
		}
	}
		
	//markerer det valgte punkt : 
	//document.getElementById( topMenuId ).style.backgroundColor = "#154473";
	//document.getElementById( topMenuId ).style.color = "#f7941d";
	sArray = topMenuId.split("_");
	nTopMenu = Number( sArray[1] );
	if(sArray[0] != "spacer" ) {
	  	document.getElementById( topMenuId ).src = aTopMenus[nTopMenu][2];  	 	
		document.getElementById( topMenuId ).height = "38";
	}
	
}

function surveillence () { 

	document.onmousemove = function ( e ) { 
	
	if ( !e ) var e = window.event; 
	var node = e.target ? e.target : e.srcElement; 
	//if ( node.nodeType == 3 ) node = node.parentNode; why this line???
	
	var oStart = node; 
	var pStart = oStart; 
	var bHide = true; 

	while ( oStart.parentNode ) {
		if ( oStart.className == "dropdown" || oStart.className == "menuTable1Level" ) {bHide = false;}
		oStart = oStart.parentNode;
	}
	
	while ( node.parentNode ) {
		if ( node.className == "menuTable1Level" || node.className == "menuTable1LevelNormal" ){ bHide = false;}
		node = node.parentNode;	
	}
	
	if ( bHide ) { setTimeout ( closeMenu, 800 ); window.bSluk = true; } 
	else window.bSluk = false; 
	} 
} 

function closeMenu () {
	if ( !window.bSluk ) return;
/*
	for (var z=0; z<document.getElementsByTagName("select").length;z++) {
		document.getElementsByTagName("select")[z].style.visibility = "visible";
	}
*/		
	//slukker menuen
	document.getElementById( "dropDownContainer" ).style.visibility = "hidden";
	
	if(document.getElementById('DivShim')) {
		var IfrRef = document.getElementById('DivShim');
		IfrRef.style.display = "none";
	}
	
	//nulstiller topmenuen
	for ( var i=0; i < window.amountOfItemsInTopMenu; i++ ){
		//document.getElementById( "topMenuItem_" + i ).className = "menuMainDiv01";
		//document.getElementById( "topMenuItem_" + i ).style.color = "#FFFFFF";
		document.getElementById( "imgTopMenuItem_" + i  ).src = aTopMenus[i][1];  	 	
		document.getElementById( "imgTopMenuItem_" + i  ).height = "38"; 
	}
}


var agent = navigator.userAgent.toLowerCase();
var opera = agent.indexOf("opera") != -1 ? true : false;

function globalOffsetTop ( object ) { // return global y coordinate
	if ( document.layers ) {
		return object.y;
	} else 
		return ( document.all && !opera && object.tagName.indexOf ("t") == 0 ? 0 : object.offsetTop ) 
		+ ( object.offsetParent && object.offsetParent.tagName != "html" ? globalOffsetTop ( object.offsetParent ) : 0 );
}

function globalOffsetLeft ( object ) { // return global x coordinate
	if ( document.layers ) {
		return object.x;
	} else 
	    return ( document.all && !opera && object.tagName.indexOf ("t") == 0 ? 0 : object.offsetLeft ) 
		+ ( object.offsetParent && object.offsetParent.tagName != "html" ? globalOffsetLeft ( object.offsetParent ) : 0 );
}
