window.onload = function() { initDelayedSF('menu', 500, 500, 'li'); };

//CSS: #menu li.wait:hover ul{display: none;  } /*warte auf js-basiertes timeout-hover*/

var timeOutShow = null;
var timeOutHide = null;
var cElementShow = new Array();
var cElementHide = null;



function initDelayedSF(element, showtime, hidetime, hovertagname) {
	var element 				= document.getElementById(element) || null;
	var showtime 				= showtime || 500;
	var hidetime 				= hidetime || 500;
	var hovertagname 		= hovertagname || 'li';
	
	if(element != null) {
		var start 				= element.getElementsByTagName(hovertagname)[0];
		while(start){
			if(start.className != undefined) {
				if(start.className.indexOf('active') == -1){
					//console.debug('binde: ');
					start.onmouseover = function(){ 
							if(cElementHide == this && this.wasShown == true) {
								window.clearTimeout(timeOutHide);
								timeOutHide = null;
								cElementHide = null;
								cElementShow = this;
							}
							if(cElementShow != this) { 
								//console.debug('start show');
								if(timeOutShow != null) { 
									window.clearTimeout(timeOutShow); 
									cElementShow.className = cElementShow.className.replace(/wait/, '');
									timeOutShow = null; 
								}
								cElementShow = this; 
								cElementShow.className = cElementShow.className + ' wait';
								timeOutShow = window.setTimeout('showMenu()', showtime);
							}
						};
					start.onmouseout = function(){ 
							if(cElementShow == this && !this.wasShown) {
								window.clearTimeout(timeOutShow);
								cElementShow.className = cElementShow.className.replace(/wait/, '');
								timeOutShow = null;
								cElementShow = null;
								
							}
							if(cElementHide != this && this.wasShown == true) {
								//console.debug('start hide');
								if(timeOutShow != null) {
									window.clearTimeout(timeOutShow);
									cElementShow.className = cElementShow.className.replace(/wait/, '');
									timeOutShow = null;
									cElementShow = null;
								}
								cElementHide = this;
								timeOutHide = window.setTimeout('hideMenu()', hidetime);
							}
						};
					}
				}
			start = start.nextSibling || false; 
		}
	}
}

function showMenu(element) {
	//console.debug('showMenu ' + cElementShow);
	if(!element) {
		cElementShow.className = cElementShow.className.replace(/wait/, 'hover');
		cElementShow.wasShown = true;
	}
	else
		element.className = element.className.replace(/wait/, 'hover');
	//cElementHide = cElementShow;
	timeOutShow = null;
}

function hideMenu(element) {
	//alert('hideMenu');
	if(!element) {
		cElementHide.className = cElementHide.className.replace(/hover/, '');
		cElementHide.wasShown = false;
	}
	else
		element.className = element.className.replace(/hover/, '');
	
	timeOutHide = null;
}
