// -----------------------------------------------------------------------------| 
//               (c) Copyright 2006 by alogis                                   |
//------------------------------------------------------------------------------|
// Filename       : j_menu.js                                                   |
// Version        : 1.00                                                        |
// Autor          : urs pluess                                                  |
// created at     : 03/23/2006                                                  |
// Description    :                                                             |
//------------------------------------------------------------------------------|

//var which stores the last visited menu-node
j_menu.lastOpenNodes = "0";
j_menu.aNodes = [];
j_menu.offsetX = 0;
j_menu.offsetY = 0;

// -----------------------------------------------------------------------------| 
// constructor j_menu()
//------------------------------------------------------------------------------|
function j_menu() {
	this.aNodes = [];
	this.m_act;
};//End j_menu()

function menu_node(objName,objParent) {
	this.m_name = objName;
	this.m_parent = objParent;
	this.m_x;
	this.m_y;
	this.m_height;
	this.ispPrepared=false;	
};//End struct menu_node

j_menu.prototype.add = function(objName,objParent) {
	j_menu.aNodes[j_menu.aNodes.length] = new menu_node(objName,objParent);
};//End add()

j_menu.prototype.show = function(o){


		var ispPrepared = false;
		
		j_menu.hideAll('m_')
		
		if(o==""){return false}
		
		actM = document.getElementById(o);
		
		//gewünschtes Object suchen
		for(i=0;i< j_menu.aNodes.length;i++){
			if(o == j_menu.aNodes[i].m_name){
				this.m_act = document.getElementById(j_menu.aNodes[i].m_parent);
				//schauen ob dieses Menu schon prepariert wurde
				if(!j_menu.aNodes[i].ispPrepared){
					j_menu.setAFunct(actM);
					j_menu.setIdName(actM,"m_" + i);
					j_menu.aNodes[i].ispPrepared=true;
				}				
			}
		}
		
		actM.style.top = j_menu.getY(this.m_act)
		actM.style.left = j_menu.getX(this.m_act)
		actM.style.visibility = "visible"

}//End show()



j_menu.setAFunct = function(obj){

	for(var i = 0;i< obj.childNodes.length;i++){
		if(obj.childNodes[i].nodeName=="A"){
			j_menu.addFunct(obj.childNodes[i]);
		}else if(obj.childNodes[i].nodeName=="DIV"){
			j_menu.setAFunct(obj.childNodes[i]);
		}
	}

}//End setAFunct()

j_menu.setIdName = function(obj,nr){
	var newNr = nr;
	
	for(var i=0;i< obj.childNodes.length;i++){
		if(obj.childNodes[i].nodeName=="DIV"){
			if(obj.childNodes[i].childNodes.length>1){
				var nr = newNr + "_" + i
				obj.childNodes[i].id=nr
				j_menu.setIdName(obj.childNodes[i],nr)
			}else{
				obj.childNodes[i].id=newNr + "_" +i
			}
		}
	}
}//End setIdName

j_menu.hideSubMenu = function(actNode){
	var d = document.getElementsByTagName("DIV");
	
	for(var i=0;i<d.length;i++){
		var s = d[i].id;
		
		if((s.indexOf(actNode)==0) && (actNode != s)){
			var o = document.getElementById(s);
			if((o.childNodes.length>1) && (o.id.length>3) && (o.childNodes[0].nodeName !="A")){
				o.style.visibility = "hidden";
			}
		}
	}
}//End hideSuMenu()

j_menu.over = function(pNode){
	if(pNode.parentNode.id.indexOf(j_menu.lastOpenNodes)==-1){
		
		s = j_menu.lastOpenNodes;
		if((pNode.parentNode.id !=j_menu.lastOpenNodes.substring(0,pNode.parentNode.id.length)) && (j_menu.lastOpenNodes.length>3)){
			j_menu.hideSubMenu(j_menu.lastOpenNodes.substring(0,pNode.parentNode.id.length));
		}else{
			j_menu.hideSubMenu(j_menu.lastOpenNodes);
		}
	}
	
	if(pNode.parentNode.childNodes.length>1){
		j_menu.subShow(pNode.parentNode,pNode);
	}
	
	j_menu.lastOpenNodes = pNode.parentNode.id
	
}//End over

j_menu.subShow = function(pNode,mother){

	var newX = pNode.offsetLeft + pNode.offsetWidth+1;
	
	for(i=0;i<pNode.childNodes.length;i++){
		if(pNode.childNodes[i].nodeName=="DIV"){
			pNode.childNodes[i].style.visibility = "visible"
			pNode.childNodes[i].style.left = newX;
			pNode.childNodes[i].style.top  = mother.offsetTop-1;
		}		
	}
}//End subShow()

j_menu.addFunct = function(actElement){
	actElement.onmouseover = function(){j_menu.over(this)}
	//actElement.setAttribute("onmouseover", "j_menu.over(this)");
}//End addFunct()

j_menu.hideAll = function(node){
	for(i=0;i< j_menu.aNodes.length;i++){
		document.getElementById(j_menu.aNodes[i].m_name).style.visibility = "hidden";
	}
	
	j_menu.hideSubMenu(node);
}//End hideAll()

j_menu.getX = function(myObj){
	var noend = true;
	var x = 0;
	var lastLeft = 0

	el = myObj;
	x+= j_menu.offsetX;
	
	while(noend){
		if(el.nodeName != "BODY"){
			if(lastLeft!=el.offsetLeft){
				x += el.offsetLeft;
			}
			lastLeft = el.offsetLeft;
			el = el.parentNode;
		}else{
			noend = false;
		}		
	}
	
	return x;
	
}//End getX()

j_menu.getY = function(myObj){
	var noend = true;
	var lastTop = 0;
	y = 0;

	el = myObj;
	y= el.offsetHeight;
	if(navigator.userAgent.indexOf("Firefox")>=0){
		y+= 2;
	}
	
	while(noend){
		if(el.nodeName != "BODY"){
			if(lastTop!=el.offsetTop){
				y += el.offsetTop;
				//alert(el.nodeName + ":" + el.offsetTop)
			}
			lastTop=el.offsetTop;
			el = el.parentNode;
		}else{
			noend = false;
		}
	}	
	
	return y;
		
}//End getY()
