var arraysReady = false;

// mainnavi

var defaultHeight = 21; // default WhiteLayer-Height for NS4, only NS4 needs this 

var sumOfWidthes = 0; // sum of width of first level main navigation 

var resetMainMenu = true; // flag for idle handler so that showMenu is just called first time

// mainnavi & quickAccess

var mainnaviX, mainnaviY, mnMaxX; // coordinates for mousemove-eventhandler

var extremeGmBonus = 10;



// sets coordinates for the mousemove-Event-handlers

function setupCoordsAndArrays(){

	if (bNav3InPage){

  	mainNavigationLayer =	 new eMBLayer( "mnv" );

  	mnSecondLevelBgLayer = new eMBLayer( "mnsub00" );		

  	whiteLayer =					 new eMBLayer( "whiteNavLayer" );

  	naviMainUnderLine =		 new eMBLayer( "mnul" );

		allNavLayerObjects = new Array( allNavLayer.length );

		for (var i=0; i<allNavLayer.length; i++) allNavLayerObjects[i] = new eMBLayer( allNavLayer[i] );

		allMenu1LayersObjects = new Array( allMenu1Layers.length );

		for (var i=0; i<allMenu1Layers.length; i++) allMenu1LayersObjects[i] = new eMBLayer( allMenu1Layers[i] );

	}



	mainnaviX = mainNavigationLayer.getX(); // x-position of mainnavi-layer

	mainnaviY = mainNavigationLayer.getY(); // y-position of mainnavi-layer

	mnMaxX = 770; // right x-border of the mainnavi-layer

}

// initializes main menu postions of first level, background, white highlight layer and line beyond

// is called once by onload

function setMenuLayerPositions(){

	if (!bNav3InPage) return true; 

//	if (IE4) setupNavLayerWidthesForIE4(); // padding patch, just for IE4

	for (i=1; i < allNavLayer.length; i++){

		 var currentXPosition = allNavLayerObjects[i-1].getX() + allNavLayerObjects[i-1].getWidth();

		 allNavLayerObjects[i].setX( currentXPosition );

		 var secondLevelXposition;

		 if ( (currentXPosition + allNavLayerObjects[i].getWidth()) > allMenu1LayersObjects[i].getWidth() ){

			 secondLevelXposition = currentXPosition + allNavLayerObjects[i].getWidth() - allMenu1LayersObjects[i].getWidth();

		 }

		 else{

			 secondLevelXposition = -4 ;

				if (NS && allMenu1LayersObjects[i].isObject()) allMenu1LayersObjects[i].object.document.images[0].src = "images/1pix.gif";

				else if (document.images["nav3subedge"+i]) document.images["nav3subedge"+i].src = "images/1pix.gif";

		 }

		 allMenu1LayersObjects[i].setX( secondLevelXposition );

	} 

	sumOfWidthes = allNavLayerObjects[allNavLayerObjects.length-1].getX() + allNavLayerObjects[allNavLayerObjects.length-1].getWidth();

	setupWhiteLayer(activeTab);

	naviMainUnderLine.setSize ( sumOfWidthes, 1 );

	mainNavigationLayer.show();

	

	// fix for first time loading NS4 (collapsing top navigation)

	

	if (document.layers){ /* may cause Problems, if there's only 1 topic in the topnavigation */

		if ( allNavLayerObjects[allNavLayerObjects.length-1].getX()< 1 ){

			DHTML_init(); 

			setTimeout("setMenuLayerPositions()", 100);

		}

	}

	

}



// positioning the white tab for highlightning the first level of main navigation			

// called by onmouseover on tab element

function setupWhiteLayer(activeTabLayer){

	if (activeTabLayer==0) { whiteLayer.hide(); return true; } // needed if screen has no default tab active

	if ((activeTab==0) && (activeTabLayer!=0)){ whiteLayer.show(); } // needed if screen has no default tab active

	whiteLayer.setX( allNavLayerObjects[activeTabLayer-1].getX() );

	whiteLayer.setSize( allNavLayerObjects[activeTabLayer-1].getWidth(), defaultHeight );	

}

// workaround just for IE4: sets initial default widthes for first level tabs

// just called on initialisation	

function setupNavLayerWidthesForIE4(){

	if (!IE4) return;

	var w = 0;

	for (i=0; i<allNavLayerObjects.length; i++){

		var d = allNavLayerObjects[i].object;

		if ( d ){

			w = d.children[0].clientWidth;

			d.style.pixelWidth = w;

		}

	}

	for (i=0; i<allMenu1LayersObjects.length; i++){

		var d = allMenu1LayersObjects[i].object;

		if ( d ){

			w = d.children[0].clientWidth;

			d.style.pixelWidth = w;

		}

	}

}



// central function for main navigation controling

// called by eventhandler and directly onmouseover-events by html-object

// called also on initizialisation

// take care: showMenu ist called on idle state all of the time by handleMove() or MShandleMove() eventhandler

function showMenu(menu,picNum) {

	if (!bNav3InPage) return true;

	// if menu="" then call comes from idle handler

	// if resetMainMenu == false then showMenu already had been called by idle handler successfully

	if (!arraysReady) setupCoordsAndArrays();

	if ((resetMainMenu == false) && (menu=='')) {

		return true;

	} else {

		resetMainMenu = true; // set after first time called

	}

	for (i=0; i<allMenu1LayersObjects.length; i++) {

			allMenu1LayersObjects[i].hide(); // hide all menus

	}

	if (menu!=mnSecondLevelBgLayer.id && menu!=""){

		var menuLayer = new eMBLayer( menu );

		menuLayer.show();	// show level two menu layer

	}

	if (picNum != null){

		setupWhiteLayer(picNum); // highlight white tab

	}

	// switch back to default main navigation level 2 submenu

	// activeSubNavName and activeTab have to be set in header of html-file

	if (menu=="") {

		if (!isNaN(activeTab) && (activeSubNavName !="")) {

			showMenu(activeSubNavName,activeTab);

		}	

		resetMainMenu = false;	

	}

}

// non-MS event handler for mouse moves

// calls showmenu without parametes on mouseout

// calls showQuickAccess without parametes on mouseout

var oldCoordX, oldCoordY;

function handleMove(ev) {

	var showMenuFlag = true;

	var showQuickAccessFlag = true;

	var eventObject = null;

	var gmBonus = 3;

	var coordX, coordY;

	if (!MS) eventObject=ev; // define an unified event-object for all browsers

	else eventObject = window.event;

	if (OP || MS){

		coordX = eventObject.clientX; // coordinates of the mousepointer, when event was fired

		coordY = eventObject.clientY;

		if (MS){

			if (IE6) // document.body.scrollTop no longer supported in IE6 (now scrollTop is part of html-object)

				var documentBody = document.getElementsByTagName("html")[0];

			else

				var documentBody = document.body;

			coordX = coordX + documentBody.scrollLeft; // correct mouse-coords with possible scrolloffset for MS

			coordY = coordY + documentBody.scrollTop;

		}

	} else {

		coordX = eventObject.pageX;

		coordY = eventObject.pageY;

	}

	if (oldCoordX==coordX && oldCoordY==coordY) return true;

	else {

		oldCoordX=coordX;

		oldCoordY=coordY;

	}

	if( !(coordX < mainnaviX || coordX > mainnaviX+sumOfWidthes || coordY < mainnaviY || coordY > mainnaviY+20)) showMenuFlag=false; // check first level

		else if( !(coordX < mainnaviX || coordX > mnMaxX || coordY <= mainnaviY+20 || coordY > mainnaviY+20+20+extremeGmBonus)) showMenuFlag=false; // check second level



	if (showMenuFlag==true) showMenu(""); // execute showMenu



}

// initializing event handling for catching mousemove-events

// this function is responsible for recognizing mouse-outs

function Event_init() {

	if(!MS) {

		if (DOM && !OP) window.onunload=Event_quit; // for NS6

		else if (NS) document.captureEvents(Event.MOUSEMOVE);

		document.onmousemove=handleMove;		

	}

	else if ( !IE4 && !MAC ) document.getElementsByTagName("html")[0].onmousemove=handleMove;

  else document.all.tags("body")[0].onmousemove=handleMove;

}

// deactivate event-handler of site on unload. For NS6 to avoid senseless errorreports

function Event_quit(ev){

	if (DOM){

    document.getElementsByTagName("body")[0].removeEventListener("mousemove", handleMove, true);

		document.onmousemove=null;	

	}

}



// init has to be called by every html-page which has an top navigation and QuickAccess

// init is called onload of the body tag

function initialisation() {

	setupCoordsAndArrays();

	setMenuLayerPositions(); // building layout of main menu and displaying

	showMenu(activeSubNavName,activeTab); // initialize submenu (setting right white layer and initial second level

	Event_init(); // initialize event-handling for main menu AND quick access

	arraysReady = true;

}

// calls initialisation() using a timeout (needed for NS4), fixes slow menu initialisation on NS4

function init(){

initialisation()

//	setTimeout("initialisation()", OP ? 1000 : 1 );

}

