/* ======================================

   Farben für die einzelnen Menüs

====================================== */
// function tMenu (menuName, bgColorDefault, bgColorHighlight, bgColorActive, ...)
var menu = new Array ();
menu[0] = new tMenu ('navi', '#495F5D', '#8C9A9A', '#6B7F7F', 0, 67);
menu[1] = new tMenu ('down', '#B6BEC1', '#A0B7CF', '#8CA8C5'); // "down"
menu[2] = new tMenu ('sub', '#E3E6E7', '#CAD1D1', '#B9C1C4'); // "Sub" / "Sub3"
menu[3] = new tMenu ('content', '#FFFFFF', '#E2E6E7', '#E2E6E7');

var navi = new tItem();


// ********************************************
// Austausch Layer bei Klick
// ********************************************

function ShowDiv () {

	for (var i=0; i < ShowDiv.arguments.length; i++) {
		DivID = ShowDiv.arguments[i]
		if (document.layers) {
			window.document[DivID].visibility = "show";
		} else if (document.all) {
			document.all[DivID].style.visibility = "visible";
		} else if (document.body) {
	  	document.getElementById (DivID).style.visibility = "visible";
		}
	}

} // ShowDiv


function HideDiv () {

	for (var i=0; i < HideDiv.arguments.length; i++) {
		DivID = HideDiv.arguments[i]
		if (document.layers) {
			window.document[DivID].visibility = "hide";
		} else if (document.all) {
	  		document.all[DivID].style.visibility = "hidden";
	  } else if (document.body) {
	  	  document.getElementById (DivID).style.visibility = "hidden";
		}
	}
	
} // HideDiv

/* ======================================

   Menü-Funktionen

====================================== */

// Highlight or un-highlight menu items
function MenuHighlight (MenuItemID, Highlight, menuId) {

  if (debug && !Highlight) window.status = 'Menüpunkt-ID: ' + MenuItemID + ', Menü-ID: ' + menuId;
  menuId = (menuId == null) ? 0 : menuId;
  if (Highlight == 2) {
    menu[menuId].curItemName = MenuItemID;
  }

  var tdIterator = 0;
  var DocMenuItemTD = '';
  var Color = Highlight ? menu[menuId].bgColorHighlight : menu[menuId].bgColorDefault;
  Color = (Highlight == 2) ? menu[menuId].bgColorActive : Color; // Highlight Page Menu Item
  
  // Highlight all TDs which build the menu item
	if (document.all) {
	  while ( (DocMenuItemTD = eval ('document.all.td' + MenuItemID + 't' + tdIterator) ) && ( (MenuItemID != menu[menuId].curItemName) || (Highlight == 2) ) ) {
 		  DocMenuItemTD.style.backgroundColor = Color;
  		tdIterator++;
		} 
	} else if (document.body) {
	  DocMenuItemTD = document.getElementsByTagName ('td')['td' + MenuItemID + 't' + tdIterator];
	  while ( DocMenuItemTD && ( (MenuItemID != menu[menuId].curItemName) || (Highlight == 2) ) ) {
  	  DocMenuItemTD.style.backgroundColor = Color;
  		tdIterator++;
  		DocMenuItemTD = document.getElementsByTagName ('td')['td' + MenuItemID + 't' + tdIterator] 
		} 
	}

} // function MenuHighlight


function writeCss () {

	document.write ('<style type="text/css"><' + '!--\n');
	if ( (navigator.appName.indexOf ('Netscape') > -1 ) && (parseInt (navigator.appVersion.substring (0, 1) ) < 5) ) {
		document.write ('body {\n'
			+ 'margin: -8px;\n'
			+ '}\n');
	} 
	
	/* Dynamische Navigation deaktiviert
	else {
	  document.write ('.indentheadline, .indent0, .indent1 {\n'
	    + 'text-align:left;\n'
	    + 'position: absolute;\n'
      + '}');
	}
	writeItemCss (navi, '', 0);
	*/
	document.write ('--' + '><\/style>\n');

} // function writeCss


function writeItemCss (itemHandle, itemName, relHeight) {

	var i;
	
	if (itemHandle.posX != null) {
		var visibility = (itemHandle.visible) ? 'visible' : 'hidden';
		var code ='';
		relHeight = Number ( (itemHandle.visible == true) ? relHeight : (relHeight - itemHandle.height) );
		itemPosY = itemHandle.posY + relHeight;
		itemPosX = itemHandle.posX + pageLeft;
		code += '#dv' + itemName + ' {\n';
		code += 'left: ' + itemPosX + 'px;\n';
		code += 'top: ' + itemPosY + 'px;\n';
		code += 'width: ' + itemHandle.width + 'px;\n';
		code += 'height: ' + itemHandle.height + 'px;\n';
		code += 'visibility: ' + visibility + ';\n';
		code += '}\n';
		document.write (code);
	}
	
	for (i=0; i < itemHandle.item.length; i++) {
		relHeight = writeItemCss (itemHandle.item [i], itemName + 'cm' + i, relHeight);
	}
	
	return relHeight;

} // function writeItemCss


function PageStartup (itemName) {

  setPageLeft ();

	// Select whether position properties need to be set as numbers or e. g. '12px'
	if (document.body) {
		document.getElementsByTagName ('body')[0].style.left = 0;
		px = (String(document.getElementsByTagName ('body')[0].style.left).indexOf ('p') == -1) ? '' : 'px';
	} else {
		px = 0;
	}
	
	arrowRight.src = globalSharedDir + '/pfeil-navi-weiss.gif';
	arrowDown.src = globalSharedDir + '/pfeil-unten-navi-weiss.gif';
	
	// Draw navigation for the first (javascript) time
	/* Dynamische Navigation deaktiviert
	setItemProperties (navi, 'dv', menu[0].top);
	*/
	
  // Highlight current page menu item(s)
  var menuItemId, menuId;
  for (var i=0; i < PageStartup.arguments.length; i=i+2) {
    menuItemId = PageStartup.arguments[i];
    menuId = (PageStartup.arguments[i+1] != null) ? PageStartup.arguments[i+1] : 0;
    MenuHighlight (menuItemId, 2, menuId);
    menu[menuId].pageItemName = menuItemId;
    // open all menu items above menuItemId
    itemNameArray = menuItemId.split('cm');
    var predecItemName = 'dv';
    for (var l = 1; l < itemNameArray.length; l++) {
      predecItemName += 'cm' + itemNameArray[l];
      if (l > 1) clickItem (getItemById (predecItemName), menuId, true);
    }
  }	

} // function PageStartup


function PageResize () {

  setPageLeft ();
  
	/* Dynamische Navigation deaktiviert
  setItemPropertiesAgain (navi, 'dv', menu[0].top, '');
	*/  
  
} // function PageResize 


function setItemProperties (itemHandle, itemName, curTop, isRecursiveCall) {

	// again, set properties of navi items, while most browsers cannot read them out of the CSS

	// IE on Mac calculates funnily
	curTop = ((isRecursiveCall != true) && (navigator.platform.indexOf ('Mac') > -1) ) ? 0 : curTop ;

	var i;
	if (itemHandle.posX != null) {
		var visibility = (itemHandle.visible == true) ? visible : hidden;
		getItemById (itemName, true).left = parseInt(itemHandle.posX + pageLeft) + px;
		getItemById (itemName, true).top = curTop + px;
		getItemById (itemName, true).width = itemHandle.width;
		getItemById (itemName, true).height = itemHandle.height;
		getItemById (itemName, true).visibility = visibility;
		curTop = (itemHandle.visible) ? (curTop + itemHandle.height) : curTop;
	}

	for (i=0; i < itemHandle.item.length; i++) {
		curTop = setItemProperties (itemHandle.item [i], itemName + 'cm' + i, curTop, true);
	}

	return curTop;

} // function setItemProperties


function setItemPropertiesAgain (itemHandle, itemName, curTop, isRecursiveCall) {

	// Redraw items, but read visibility from actual state, not from javascript object tree

	// IE on Mac calculates funnily
	curTop = ((isRecursiveCall != true) && (navigator.platform.indexOf ('Mac') > -1) ) ? 0 : curTop ;

	var i;
	if (itemHandle.posX != null) {
		var visibility = getItemById (itemName, true).visibility;
		var itemDivHandle = getItemById (itemName, true);
		itemDivHandle.left = parseInt(itemHandle.posX + pageLeft) + px;
		itemDivHandle.top = curTop + px;
		itemDivHandle.visibility = visibility;
		curTop = Number ( ( (visibility.toLowerCase() == 'visible') || (visibility == 'show') ) ? (curTop + itemHandle.height) : curTop );
	}

	for (i=0; i < itemHandle.item.length; i++) {
		curTop = setItemPropertiesAgain (itemHandle.item [i], itemName + 'cm' + i, curTop, true);
	}

	return curTop;

} // function setItemPropertiesAgain


function getItemById (itemId, addStyleHandle) {

  var itemHandle = null;
	// var styleHandle = (addStyleHandle == false) ? '' : 'style';
	if (document.all) { // IE
		itemHandle = document.all.tags('div')[itemId];
		if (itemHandle == null) return false;
    return (addStyleHandle != true) ? itemHandle : itemHandle.style;
  } else if (document.body) { // Opera, Netscape 6 etc. (W3C-DOM)
  	itemHandle = document.getElementsByTagName('div')[itemId];
  	if (itemHandle == null) return false;
    return (addStyleHandle != true) ? itemHandle : itemHandle.style;
  } else if (document.layers) { // Netscape 4.7x
    return ( (itemHandle = document.layers[itemId]) == null) ? false : itemHandle;
  }
  return false;

} // function getItemById


function SNSclickItem (itemId, menuId) {

	// Dynamische Navigation deaktiviert
	return true;
	
	// only necessary for Netscape < 6
	if (document.layers != null) return clickItem (document.layers[itemId], menuId, true); else return clickItem (getItemById ('dv' + itemId), menuId, true);
	
} // function SNSclickItem 


function walkItemForHref (itemHandle) {

	var i;
	if ( (itemHandle.href != null) && (itemHandle.href != '') && (itemHandle.onclick != null) ) {
		if (itemHandle.target == '_blank') {
			window.open (itemHandle.href);
		} else {
			document.location.href = itemHandle.href;
		}
	}
	for (i=0; i < Number(itemHandle.childNodes.length); i++) {
		walkItemForHref (itemHandle.childNodes[i]);
	}

} // function walkItemForHref


function isItemOpen (itemHandle, menuId) {

  /* if exactly itemHandle is opened then return true
  if (menu[menuId].curItemName == itemHandle.id.substr (2, itemHandle.id.length - 2) &&
    menu[menuId].isCurItemOpen) return true;
  */
  // cut the curItemName to the length of itemHandle.id and compare them = check if one 
  // of the child items of itemHandle is open
  // return (menu[menuId].curItemName.substr(0, itemHandle.id.length - 2) == itemHandle.id.substr (2, itemHandle.id.length - 2));

  itemArrow = document['im' + itemHandle.id.substr (2, itemHandle.id.length - 2)];
  return ( itemArrow ? (itemArrow.src.indexOf(arrowDown.src.substring (arrowDown.src.lastIndexOf ('/') ) ) > -1) : true );
  
} // function isItemOpen


function getItem (itemName, returnItemName) {
  
  var itemObjectName = 'navi';
  itemName = itemName.split ('cm');
  for (var i=1; i < itemName.length; i++) {
    itemObjectName += '.item['+itemName[i]+']';
   }
  return (returnItemName ? itemObjectName : eval(itemObjectName) );
  
} // function getItem


function clickItem (itemHandle, menuId, doIt) {

  if (!itemHandle) return true;
  if (doIt != true) return true;

  // if item has no children, follow item link immediately
  if (!getItem (itemHandle.id + 'cm0')) return true;

  menuId = (menuId == null) ? 0 : menuId;
  var itemId = itemHandle.id.substr (2, itemHandle.id.length - 2);
  itemOpen = isItemOpen (itemHandle, menuId)
	var i=1;
  var curItemHandle = getItemById ('dvcm' + i);
	while (curItemHandle.id != null) {
		toggleItems (curItemHandle, itemHandle, !itemOpen, 0);
		i++;
		curItemHandle = getItemById ('dvcm' + i);
	}
	
	// MenuHighlight (itemId, (itemOpen ? false : 2), menuId);
  document['im' + itemId].src = (itemOpen ? arrowRight.src : arrowDown.src);

	// Dynamische Navigation deaktiviert
	return true;

	setItemPropertiesAgain (navi, 'dv', menu[menuId].top, itemId);
	return false;
	// if (itemHandle.childNodes != null) walkItemForHref (itemHandle);
	return false;

} // function clickItem 


function toggleItems (curItem, targetItem, setVisible) {

	var i = 0;
	var child = getChild (curItem, i);
  while (child.id != null) {
    // dm ('  child: '+child.id);
    if (curItem == targetItem) {
  		toggleItem (child, setVisible);
  		if (setVisible == false) toggleItems (child, targetItem, setVisible);
    } else {
  		toggleItems (child, targetItem, setVisible);
	  }
	  i++;
	  child = getChild (curItem, i);
	}
	// dm (curItem.id + ' > ' + targetItem.id + ': '+String(curItem.id.split('cm').length > targetItem.id.split('cm').length));
	if ((curItem.id.split('cm').length > targetItem.id.split('cm').length) ) {
	  // curItem is on a lower menu layer than targetItem
	  if (curItem.id.substr(0, targetItem.id.length) == targetItem.id) 
	    // ancestor items of targetItem
	    toggleItem (curItem, setVisible);
	  else
	    // ancestor items of other items must be closed
      toggleItem (curItem, false);
	} else if ( (itemArrow = document['im' + curItem.id.substr (2, curItem.id.length - 2)]) && (curItem.id != targetItem.id.substr (0, curItem.id.length) ) ) {
	  // dm (' right:'+curItem.id +'!='+ targetItem.id.substr (0, curItem.id.length) );
	  itemArrow.src = arrowRight.src;
	}

	
} // function toggleItems 


function toggleItem (itemHandle, setVisible) {

  if (itemHandle.id != null) itemArrow = document['im' + itemHandle.id.substr (2, itemHandle.id.length - 2)];
  if (itemArrow != null) {
    var targetStatus = (setVisible == true) ? visible : hidden;
	  getItemById (itemHandle.id, true).visibility = targetStatus;
	  itemArrow.src = arrowRight.src;
	}

} // function toggleItem


function getChild (itemHandle, count) {

	return getItemById (itemHandle.id + 'cm' + count);

} // function getChild


function childrenCount (itemHandle) {

	var counter = 0;
	var child = getChild (itemHandle, counter);
	while (child != null) {
		counter++;
		child = getChild (itemHandle, counter);
	}
	counter--;
	return counter;

} // function childrenCount


function isItemVisible (itemHandle) {

	var visibility = itemHandle.visibility.toLowerCase();
	return ( (visibility == 'visible') || (visibility == 'show') );

} // function isItemVisible


function setPageLeft () {

	if (document.layers) { // Netscape 4
	  pageLeft = (window.innerWidth - 24 - 720) / 2;
	} else if (document.all && document.body) { // MS IE, page body rendered
  	pageLeft = parseInt( (document.body.clientWidth - 720) / 2);
  	if (navigator.platform.indexOf ('Mac') > -1) pageLeft = 0; // IE on Mac calculates funnily
	} else if (document.body) { // W3C-DOM, e. g. Opera, Mozilla, Netscape 6
	  pageLeft = parseInt ( (document.body.clientWidth - 720) / 2);
	}
	
	pageLeft = (pageLeft < 0) ? 0 : pageLeft;
	
	
} // function getPageLeft


function navOn (name){
//	document[name].src = eval(name + "1.src");
}


/* --------------------------------------

   Funktion, um neues Browserfenster zu öffnen

-------------------------------------- */

// Browser-Fenster öffnen (V3)
function OpenWindow (URL, Width, Height, Left, Top, Name, Resizable, ScrollBars, MenuBar, ToolBar, LocationBar, BookmarkBar, StatusBar, Dependent, HotKeys) {

	// Vorsicht bei NN: Wird die MenuBar angezeigt, wird ab Breiten kleiner 303 die Fensterhöhe vom NN falsch berechnet.

	// Testen, ob Positions- und Größen-Parameter angegeben und nicht zu groß sind
	if ( (Top == null) || isNaN (Top) ) Top = 20;
	if ( (Left == null) || isNaN (Left) ) Left = 20;
	if ( (Height == null) || isNaN (Height) || (Height + Top > screen.availHeight - 10) ) Height = screen.availHeight - 10 - Top;
	if ( (Width == null) || isNaN (Width) || (Width + Left > screen.availWidth - 10) ) Width = screen.availWidth - 10 - Left;
	Name = (Name == null) ? '' : Name; // Name = Leerstring, wenn kein Name angegeben ist
	// Testen, ob alle Parameter vorhanden sind, sonst Default-Werte vergeben

	Resizable = ((Resizable == null) || (Resizable == true)) ? 'yes' : 'no';
	ScrollBars = ((ScrollBars == null) || (ScrollBars == true)) ? 'yes' : 'no';
	MenuBar = ((MenuBar == null) || (MenuBar == false)) ? 'no' : 'yes';
	ToolBar = ((ToolBar == null) || (ToolBar == false)) ? 'no' : 'yes';
	LocationBar = ((LocationBar == null) || (LocationBar == false)) ? 'no' : 'yes';
	BookmarkBar = ((BookmarkBar == null) || (BookmarkBar == false)) ? 'no' : 'yes';
	StatusBar = ((StatusBar == null) || (StatusBar == true)) ? 'yes' : 'no';
	Dependent = ((Dependent == null) || (Dependent == true)) ? 'yes' : 'no';
	HotKeys = ((HotKeys == null) || (HotKeys == true)) ? 'yes' : 'no';

	// Für IE Fenstergrößen extra berechnen, da IE "innerWidth" und "InnerHeight" nicht beachtet.
	if (document.all) {
		Height += 31; // 31 Pixel höher für Fensterrahmen und Titelleiste des Fensters
		Width = Width + 12; // 12 Pixel breiter für den Fensterrahmen
		Height = (Resizable == 'no') ? Height - 2 : Height; // Der Fensterrahmen von Nicht-"Resizable"-Fenstern ist 2 Pixel schmaler.
		Width = (Resizable == 'no') ? Width - 2 : Width; // dto.
		Width = (ScrollBars == 'yes') ? Width + 16 : Width;
		Height = (MenuBar == 'yes') ? Height + 29 : Height;
		Height = (ToolBar == 'yes') ? Height + 47 : Height;
		Height = (LocationBar == 'yes') ? Height + 29 : Height; 
		var BarCompensation = (MenuBar == 'yes') ? 1 : 0; // je "Bar"-Paar werden wiederum 5 Pixel eingespart
		BarCompensation = (ToolBar == 'yes') ? BarCompensation + 1 : BarCompensation;
		BarCompensation = (LocationBar == 'yes') ? BarCompensation + 1 : BarCompensation;
		BarCompensation = (BarCompensation < 1) ? 1 : BarCompensation;
		Height -= ( (BarCompensation - 1) * 5);
		Height = (StatusBar == 'yes') ? Height + 20 : Height;
		// Fenster wieder verkleinern, falls es jetzt wieder zu groß ist
		if (!Width || isNaN (Width) || (Width > screen.availWidth - 10)) Width = screen.availWidth - 10;
		if (!Height || isNaN (Height) || (Height > screen.availHeight - 10)) Height = screen.availHeight - 10;
	}
	
	// Fenster mit den gegebenen Parametern öffnen
	NewWindow = window.open(URL, Name, 'screenX=' + Left + ',screenY=' + Top 
	+ ',height=' + Height + ',width=' + Width + ',innerHeight=' + Height + ',innerWidth=' + Width
	+ ',resizable=' + Resizable + ',scrollbars=' + ScrollBars + ',menubar=' + MenuBar 
	+ ',toolbar=' + ToolBar + ',location=' + LocationBar + ',directories=' + BookmarkBar 
	+ ',status=' + StatusBar + ',dependent=' + Dependent);

	if (navigator.appVersion.indexOf ('MSIE 5.0')) NewWindow.focus (); // IE 5.0 bricht hier sonst ab

	URL += ''; // IE versteht die URL nur so als String ...

	// Für IE muss extra positioniert. Der Fenster-Zugriff wird aber verweigert, wenn eine 
	// komplette HTTP-Adresse aufgerufen wird ...

	if (!NewWindow.innerWidth && (URL.indexOf ('http://') < 0)) {
		NewWindow.moveTo (Left, Top);
	}

	NewWindow.focus (); // Fenster ganz nach vorn stellen, also Focus auf das Fenster

	return false; // damit der HREF-Link nicht ausgeführt wird
	
} // function OpenWindow


/* ======================================

   Constants, global variables, type constructors

====================================== */

var pageLeft = 0;
setPageLeft ();


// "Konstanten" zur Browser-Kompatibilität
var px = '';
var visible = (document.layers) ? 'show' : 'visible';
var hidden = (document.layers) ? 'hide' : 'hidden';

var arrowDown = new Image();
var arrowRight = new Image();

function tMenu (menuName, bgColorDefault, bgColorHighlight, bgColorActive, left, top, isPopUp, opensOnMouseOver, opensOnlyOneItem) {
	
	this.name = menuName;
	this.bgColorDefault = bgColorDefault;
	this.bgColorHighlight = bgColorHighlight;
	this.bgColorActive = bgColorActive;
	this.left = left;
	this.top = top;
	this.isPopUp = isPopUp;
	this.opensOnMouseOver = opensOnMouseOver;
	this.opensOnlyOneItem = opensOnlyOneItem;

	this.curItemName = '';
	this.pageItemName = '';
	this.isCurItemOpen = false;
	this.MouseOutTimeout = 0;
	// this.item = new Array();
	
} // function tMenu 


function tItem (posX, posY, width, height, visible) {
	
	this.posX = posX;
	this.posY = posY;
	this.width = width;
	this.height = height;
	this.visible = visible;

	this.item = new Array ();
	
} // function tItem



/* ======================================

   Debug stuff

====================================== */

var debug = false;


function dm (message) {
  
  if ( debug && (document.forms.debug) ) alert (message); // document.forms.debug.elements.dta.value += message + '\n';
  
} // function dm 
