/*
*** event:
* pageX/Y: Dokument-relative Koordinaten, Netscape
* screenX/Y: Bildschirm-relative Mauszeiger-Position, Netscape
* layerX/Y: Layer-relative Mauszeiger-Position, Netscape
* clientX/Y: Anzeigebereich-relative Mauszeiger-Position, Microsoft
* offsetX/Y: Objekt-relative Mauszeiger-Position, Microsoft
* x/y: Elternelement-relative Mauszeiger-Position, Microsoft

*** window:
* innerWidth/Height: Breite/Höhe des Anzeigebereichs - nicht IE
* clientWidth/Height: IE analog innerWidth - document.body.xxx oder document.documentElement.xxx

* outerWidth/Height: Breite/Höhe des gesamten Fensters - nicht IE
* offsetWidth/Height:

* pageXOffset/pageYOffset: Fensterstartposition von links/oben - nicht IE
* scrollLeft/Top: IE analog pageXOffset - document.body.xxx oder document.documentElement.xxx
*/

/* ********************************* */
/* ******** Class PopupMenu ******** */
/* ********************************* */
var isNS6 = document.getElementById && !document.all;

function PopupMenu(myID) { // constructor
	this.offX = -5;   // horizontal offset , old: 4
	this.offY = 14;  // vertical offset , old: 32

	this.constructor = PopupMenu;
	this.id = myID;
	this.timer = null;
	this.myFatherId = null;
	this.myChildrenId = new Array();
	
	this.show = show;
	this.clearTimer = clearTimer;
	this.position = position;
	this.hide = hide;
	this.mouseoutCheck = mouseoutCheck;
	this.setFatherId = setFatherId;
	this.addChildId = addChildId;
};

function show (e, caller) {
	var divobj = document.getElementById? document.getElementById(this.id): null;//div-object
	if (!divobj) return;
	this.position(divobj, e, caller);//this muss da sein fuer IE
};

function clearTimer () {
	if (this.timer) clearTimeout(this.timer);
};

function position (divobj, e, caller) {
	var x = 0;
	var y = 0;

	//--- position von event	
	//firefox, mozilla
	if (isNS6) {
		x = e.pageX;
		y = e.pageY;
	}
	//ie
	else {
		x = e.clientX;
		y = e.clientY; 
	}

	//--- verschieben bis zum ende von caller-obj
	if (caller) {
		if (isNS6){
			//x = caller.offsetLeft + caller.offsetWidth;
			y = caller.offsetTop;// + caller.offsetHeight - divobj.offsetHeight;
		}
		else {
			//x = x - e.offsetX + caller.offsetWidth;
			y = y - e.offsetY;// + caller.offsetHeight - divobj.offsetHeight;
		}
	}
		
	//--- wenn nicht genug platz fuer menue ist
	/*
	if ( x + divobj.offsetWidth + this.offX > this.viewport.width + this.viewport.scrollX ) {
		x = x - divobj.offsetWidth - this.offX;
	}
	//--- wenn genug platz fuer menue ist
	else x = x + this.offX;
	

	if ( y + divobj.offsetHeight + this.offY > this.viewport.height + this.viewport.scrollY )
		y = ( y - divobj.offsetHeight - this.offY > this.viewport.scrollY )? y - divobj.offsetHeight - this.offY : this.viewport.height + this.viewport.scrollY - divobj.offsetHeight;
	else y = y + this.offY;
	*/

	x += this.offX;
	y += this.offY;
	divobj.style.left = x + "px"; 
	divobj.style.top = y + "px";
	this.timer = setTimeout("document.getElementById('" + this.id + "').style.visibility = 'visible'", 100);
};

function hide () {
	this.clearTimer();
	if (this.id && document.getElementById) 
		this.timer = setTimeout("document.getElementById('"+ this.id +"').style.visibility = 'hidden'", 100);
};

function mouseoutCheck (e) {
	e = e? e: window.event;
	
	// is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
	var divobj = document.getElementById(this.id);
	var toEl = e.relatedTarget? e.relatedTarget: e.toElement;

	if ( divobj != toEl && !contained(toEl, divobj, this) ) this.hide();
};

function setFatherId (fatherId) {
	this.myFatherId = fatherId;
};

function addChildId (childId) {
	this.myChildrenId[this.myChildrenId.length] = childId;
};



/* ********************************** */
/* ******** Common Functions ******** */
/* ********************************** */
function divMouseoutCheck (e, divobj) {
	if (!divobj) return;
	e = e? e: window.event;
	try {
		var mnuobj = eval(divobj.id);
		if (mnuobj) mnuobj.mouseoutCheck(e);
		// auch die father informieren
		if (mnuobj.myFatherId) {
			divMouseoutCheckRec(e, mnuobj.myFatherId);
		}
  } catch (e) {;}
};

function divMouseoutCheckRec(e, mnuid) {
	e = e? e: window.event;
	try {
		var mnuobj = eval(mnuid);
		if (mnuobj) mnuobj.mouseoutCheck(e);
		// auch die father informieren
		if (mnuobj.myFatherId) {
			divMouseoutCheckRec(e, mnuobj.myFatherId);
		}
  } catch (e) {;}
}

function divClearTimer(divobj) {
	if (!divobj) return;
	try {
		var mnuobj = eval(divobj.id);
		if (mnuobj) mnuobj.clearTimer();
  } catch (e) {;}
}

function showPopupMenu(id, e, caller){
	try {
		var mnu = eval(id);//mnu ist das menu-object
		if (mnu)
			mnu.show(e, caller);
  } catch (e) {;}
}

function showPopupMenu_byID(id, e, callerid){
	try {
		var mnu = eval(id);//mnu ist das menu-object
		if (mnu)
			mnu.show(e, callerid);
  } catch (e) {;}
}

function hidePopupMenu(id, e){
	try {
		var mnu = eval(id);
		if (mnu)
			mnu.mouseoutCheck(e);
  } catch (e) {;}
}

// returns true if oNode is contained by oCont (container)
function contained (oNode, oCont, mnuobj) {
	if (!oNode) return; // in case alt-tab away while hovering (prevent error)
	var savedNode = oNode;

	//im eigenen Container?
	while ( oNode = oNode.parentNode ) 
		if ( oNode == oCont ) return true;

	//in children containers?
	var allchildren = new Array();
	getAllChildren(mnuobj, allchildren);
	if (allchildren && allchildren.length>0) {
		var inChild = false;
		var childCont;
		for (i=0; i<allchildren.length; i++) {
			childCont = document.getElementById(allchildren[i]);
			if (childCont) {
				inChild = containedInChild(savedNode, childCont);
				//alert("mouse is in container [" + childCont.id + "]? " + inChild);
				if (inChild) return true;
			}
			else
				alert("childcont is null: " + allchildren[i]);
		}
	}

	//nix da
	return false;
};

function containedInChild (oNode, oCont) {
	if (!oNode) return; // in case alt-tab away while hovering (prevent error)

	//im eigenen Container?
	while ( oNode = oNode.parentNode ) 
		if ( oNode == oCont ) return true;

	//nix da
	return false;
};

function getAllChildren(mnuobj, allchildren) {
	if (!mnuobj) return;
	if (!mnuobj.myChildrenId || mnuobj.myChildrenId.length==0) return;

	//own children
	for (i=0; i<mnuobj.myChildrenId.length; i++) {
		allchildren[allchildren.length] = mnuobj.myChildrenId[i];
	}	

	//children of children
	var tmpmnuobj;
	for (i=0; i<mnuobj.myChildrenId.length; i++) {
		tmpmnuobj = eval(mnuobj.myChildrenId[i]);
		if (tmpmnuobj) {
			getAllChildren(tmpmnuobj, allchildren);
		}
	}	
}

function setRelation (fatherobj, childobj) {
	fatherobj.addChildId(childobj.id);
	childobj.setFatherId(fatherobj.id);
}



/* ********************************* */
/* ******** Write Functions ******** */
/* ********************************* */
var bufmenus;
var bufjavascript;
var bufchildren;

function startAllMenus() {
	bufmenus = "";
	bufjavascript = "";
	bufchildren = "";
}

function startMenu(mnuid) {
	bufmenus += "<div id=\"" + mnuid + "\" class=\"pu_menu\" onmouseover=\"divClearTimer(this)\" onmouseout=\"divMouseoutCheck(event, this)\"><ul>\n";
	bufjavascript += "var " + mnuid + " = new PopupMenu(\"" + mnuid + "\");\n";
}

function addFunction(label, hrefcontent) {
  	bufmenus += "<li><a href=\"" + hrefcontent + "\">" + label + "</a></li>\n";
}

function addBreakLine() {
  	bufmenus += "<hr />\n";
}

function addChildMenu(mnuid, label, childmnuid) {
  	bufmenus += "<li><a href=\"javascript:;\" onmouseover=\"showPopupMenu('" + childmnuid + 
  		"', event, this)\" onmouseout=\"hidePopupMenu('" + childmnuid + "', event)\">" + label + "</a></li>\n";
	bufchildren += "setRelation (" + mnuid + ", " + childmnuid + ");\n";
}

function endMenu() {
	bufmenus += "</ul></div>\n";
}

function writeAllMenus() {
	if (bufmenus!=null && bufmenus.length > 0) {
		document.writeln(bufmenus);
		document.writeln("<p />");
		document.writeln("<script type=\"text/javascript\">");
		document.writeln(bufjavascript);
		document.writeln(bufchildren);
		document.writeln("</script>");
	}
}
