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() {
  hideSearchDiv();
  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 + '><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;
	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 = 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);
}
