function doDebug(msg) {
  var e = document.getElementById('debug');
  if (e!=null) {
    e.innerHTML += msg + '<br />';
  }
}

var hideTimeout = null;
var showTimeout = null;
var oldShowDivID = '';
var toShowDivID='';
var showElement='';
var lastShowed=null;

function InitSubMenu() {

  if (!checkCSS()) {
  	return;
  }
  var e = document.getElementById('topmenu');
  if (e != null) {
  	e.className = 'js';
  }
  
  var html = '';  
  var e = document.getElementById('submenus_js');
  if (e != null) {
	  for (var i=0; i<subMenus.length; i++) {
	    html = '<div id="submenu_' + subMenus[i].name + '" style="display: none;" class="submenu_div" onmouseover="enterSubMenu(\'' + subMenus[i].name + '\');" onmouseout="leaveSubMenu(\'' + subMenus[i].name + '\');">';
	    html += '<div class="topsubmenu-design">';
	    html += '<ul>';
	    for (var j=0; j<subMenus[i].items.length; j++) {
	    	html += '<li><div><a href=' + subMenus[i].items[j].url + ' class="' + subMenus[i].items[j].cssclass + '"><span>' + subMenus[i].items[j].name + '</span></a></div></li>';
	    }
	  	html += '</ul>';
	  	html += '</div>';
	  	html += '</div>';
	  	e.innerHTML += html;
	  }	  
  }
}

function checkCSS() {
	var result = false;
	var e=document.getElementById('csscheck');
	if (e != null) {	
		if (e.style.backgroundColor == 'red') {
			result = true;
		}
	}
	return result;
}

function getX(element) {
	var tNode = element;
	var xPos = 0;
	while (tNode.nodeName != "BODY")
	{
	   xPos += tNode.offsetLeft;
	   tNode = tNode.offsetParent;	
	}
	return xPos;
}

function showSubMenu(element, divID) {
  if (!checkCSS()) {
  	return;
  }

	element.className = 'js ' + element.className; 
	toShowDivID = divID;
	//element.style.backgroundColor = '#ffffff';
	showElement = element;
	showTimeout = window.setTimeout("timerShowSubMenu()", 200);
}

function timerShowSubMenu() {
  showTimeout = null;
  if (lastShowed != null && lastShowed != showElement) {
	hideLastShowed();
  }
  if (hideTimeout != null) {
    window.clearTimeout(hideTimeout);
    hideTimeout = null;
  }

  var x = getX(showElement);
  if (showElement != null) {
    if (lastShowed != showElement) {
      showElement.oldClassName = showElement.className;
      doDebug('org: ' + showElement.className);
  	  showElement.className = 'jsselected ' + showElement.className;
  	  doDebug('new: ' + showElement.className);
  	}
  	lastShowed = showElement; 
  }
  var e_js = document.getElementById('submenus_js');
  if (e_js != null) {
  	e_js.style.left = x + 'px';
  }

  var e = document.getElementById('submenu_' + toShowDivID);
  if (e!=null) {
    e.style.display = 'block';
    oldShowDivID = toShowDivID;
  }
}

function hideLastShowed() {
  if (lastShowed != null) {
	lastShowed.className = lastShowed.oldClassName;
	//lastShowed.style.backgroundColor = 'transparent';
	lastShowed = null;
  }
  if (oldShowDivID != aktTopMenuItem) {
    var e = document.getElementById('submenu_' + oldShowDivID);
    if (e!=null) {
      e.style.display = 'none';
    }  
  }
}

function hideSubMenu(element) {
  if (!checkCSS()) {
  	return;
  }

  if (element == showElement) {
    if (showTimeout != null) {
      window.clearTimeout(showTimeout);
    }
  }
  toShowDivID = '';
  if (hideTimeout != null) {
    window.clearTimeout(hideTimeout);
  }
  hideTimeout = window.setTimeout("timerHideSubMenu()", 500);
}

function timerHideSubMenu() {
  hideTimeout = null;
  hideLastShowed();
}

function enterSubMenu(divID) {
  if (!checkCSS()) {
  	return;
  }

  if (hideTimeout != null) {
    window.clearTimeout(hideTimeout);
  }
  toShowDivID = divID;
}

function leaveSubMenu(divID) {
  if (!checkCSS()) {
  	return;
  }

  hideSubMenu(divID);
}

function adjustHeight( cssclass ) 
{
	var eMain = document.getElementById( 'main' );
	var eDiv = eMain.getElementsByTagName( 'div' );
	
	var cnt = eDiv.length;
	var elements = Array();
	if( cnt > 1 ) {
		
		for( var i = 0; i < cnt; i++ ) {
			if( eDiv[i].className == cssclass ) {
				elements[elements.length] = eDiv[i];
			}
		}
		var eHeight = 0;
		cnt = elements.length;
		if( cnt > 1 ) {
			
			for( var i = 0; i < cnt; i++ ) {
				test1 = elements[i].scrollHeight;
				test2 = elements[i].offsetHeight;
				
				if( test1 > test2 && test1 > eHeight ) {
					var eHeight = test1;
				}
				else if( test2 > eHeight ) {
					var eHeight = test2;
				}	
			}
			if( eHeight > 0 ) {
				
				for( i = 0; i < cnt; i++ ) {
					elements[i].style.height = (eHeight - 19 ) + 'px';
				}
			}
		}
	}


	
}