
// all treemnus
//var treeMenus=null;
//var treeMenu=null;

function getTMChildren()
{
 var cn=this.children;
 return cn;
}	

function setTMChildren(childrenP)
{
 this.children=childrenP;

}	



// obj treemenu constructor
function TreeMenu (_parentId, _treeId, _label, _link, _target, _children) 
{

	this.children=_children;
	this.selected=false;
    this.treeId=_treeId;
    this.parentId=_parentId;
    this.link=_link;
    this.label=_label;
    this.target=_target;
	
    this.getChildren=getTMChildren;
    this.setChildren=setTMChildren;
	this.getChildMenuByTreeId=getTMChildMenuByTreeId;
	this.addChild=addTMChild;
}
	

function getTMChildMenuByTreeId(treeId)
    {
    	if (treeId==null) return null;

       	var rm=null;
    	var xm=this;
    	for (i=0; xm.children!=null && i<xm.children.length; i++ )
    	{
    		var xchild=xm.children[i];
    		if (treeId==xchild.treeId )
    				{		rm=xchild;
    						break;
    				}	
    		rm=xchild.getChildMenuByTreeId(treeId);
    		if (rm!=null) break;
    		
    	}
    	
    	return rm;
    }	

    
function addTMChild(child)
    {
        if (this.children==null)
            this.children=new Array();
        this.children.push(child);
    }	

// put the menus into tree
function initializeTree(treeMenu, treeMenus)
{
	// treeMenus are initialized by server side generated script (data)
	// treeMenu is initialized by server side 
	var done=0;
	var fail=0;
	while(treeMenu!=null && treeMenus!=null && treeMenus.length>0)
		{
			var orphan=treeMenus.shift();
			var ptm=treeMenu.getChildMenuByTreeId(orphan.parentId);
			if (ptm!=null) 
				{ ptm.addChild(orphan);
					fail=0;
				}
			else { 	treeMenus.push(orphan);
					fail++;
					}
		
			// if consequetive failures will be gt than size of orphans
			// then no more parents could not be found
			// and those left are real orphans
			if (fail>treeMenus.length) break;
		
		}

return treeMenu;
}



// second edition
var knownMenus=new Array();
var timerShow=null;

function showMenu(menuid, objid, prntid)
{
	var menu=new Array();
	menu[0]=menuid;
	menu[1]=objid;
	menu[2]='show';
	menu[3]=prntid;
	
	var menuset=false;
	for (i=0; knownMenus!=null && i<knownMenus.length; i++)
	{
		var menux=knownMenus[i];
		if (menux[0]==menuid)
		{	menux[1]=objid;
			menux[2]='show';
			menuset=true;
			break;
		}
	}	
	if (!menuset) 
		knownMenus[knownMenus.length]=menu;
	
	if (timerShow!=null) { clearTimeout(timerShow); timerShow=null;}
	timerShow=setTimeout("refreshMenus();",400);
}

function hideMenu(menuid)
{
	for (i=0; knownMenus!=null && i<knownMenus.length; i++)
	{
		var menux=knownMenus[i];
		if (menux[0]==menuid)//(lastlevel<clevel)
		{
		  menux[2]='hide';
  	  	if (menux[3]!=null) 
  	  	{ return hideMenu(menux[3]);
	  	}
		  
		}  
	}	

	if (timerShow!=null) { clearTimeout(timerShow); timerShow=null;}
	timerShow=setTimeout("refreshMenus();",2000);

}


function onMenu(menuid)
{
	for (i=0; knownMenus!=null && i<knownMenus.length; i++)
	{
		var menux=knownMenus[i];

		if (menux[0]==menuid)//(lastlevel<clevel)
		{
		  menux[2]='show'
		  if (menux[3]!=null) 
		  { return onMenu(menux[3]);

	  	  }
		  break;
		}  
	}	

}


function outMenu(menuid)
{
	hideMenu(menuid)
}


function debug(msg)
{
var debugar=document.getElementById('debug');
debugar.innerHTML=msg;
}


function refreshMenus()
{
//var msg="";
	for (i=0; i<knownMenus.length; i++)
	{

		var menux=knownMenus[i];
		//msg=msg+"["+menux.toString()+"] ";
		var menuid=menux[0];
		var objid=menux[1];
		var show=menux[2];
		var menuel=document.getElementById(menuid);
		var obj=document.getElementById(objid);
		if (menuel!=null) 
		{
		if (show=='show') {
				x=0;
				y=0;
				if (obj!=null)
				{ 	objwidth=obj.offsetWidth
					x=findPosX(obj)+objwidth+1;
					y=findPosY(obj);
				}	
				
				menuel.style.position="absolute";
				menuel.style.left=x+"px";
				menuel.style.top=y+"px";
				//menuel.style.backgroundColor="yellow"
				menuel.style.zIndex=1;
				menuel.style.visibility = "visible";

			}
		else menuel.style.visibility = "hidden";
		}
	}
	

	//	debug("refreshmenus: "+msg);
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	
	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	//var browser=navigator.userAgent.toLowerCase();
	//if (browser!=null && browser.indexOf('Internet Explorer'))
	//curtop=curtop+4;
	
			
	return curtop;
}

function nolink()
{
	
}
