//
// ************ 
//

var item_count=0;
var items=new Array(100);
var dbgc=0;

function dbg(txt)
{
	dbgc++;
	// document.getElementById("dbg").value=dbgc+": "+txt+"\n"+document.getElementById("dbg").value;
}

function menuItem(_parent,_id,_top,_left,_url,_status,_overstyle,_parentstyle) {

	this.id=_id;
	this.top=_top;
	this.left=_left;
	this.url=_url;
	this.status=_status;
	this.childArray=new Array(0);
	this.children=0;
	this.placed=0;
	this.timeout=0;
	this.overstyle=_overstyle;
	this.parentstyle=_parentstyle;
	this.visible=false;

	if (_parent!=0) {
		this.parent=_parent;
		this.parent.childArray[this.parent.children]=this;
		this.parent.children++;
	} else {
		this.parent=0;
		this.visible=false;
	}

	items[item_count]=this;
	item_count++;

}


function place(obj,index,level,height,width,delta)
{
	var i,tmp_element = document.getElementById(obj.id);
	
	if (tmp_element!=null && obj.placed==0) {
	
		tmp_element=tmp_element.cbe;
		
		if (obj.top==-1 && obj.left==-1) {
			obj.top=obj.parent.top+(index+delta)*height;
			obj.left=obj.parent.left+(level*width);
		}
		tmp_element.moveTo(obj.left,obj.top);
		
		if (level==0) tmp_element.show();
		
		tmp_element.addEventListener('mouseOver', mouseOver, false);	
		tmp_element.addEventListener('mouseOut', mouseOut, false);	
		tmp_element.addEventListener('click', mouseClick, false);	
		
		obj.placed=1;
		
		for(i=0;i<obj.children;i++)
		{
			place(obj.childArray[i],i,level+1,height,width,delta);
		}
	
	}				
	return true;
}

function findItem(oname)
{
	var i;
	for(i=0;i<item_count;i++)
	{
		if (items[i].id==oname) return items[i];
	}
	
	return 0;
}

function clearParentTimeout(obj)
{
	window.clearTimeout(obj.timeout);
	if (obj.parent!=0) 
	{

		clearParentTimeout(obj.parent);
	}	

}

function clearUpperTimeouts(obj)
{
	window.clearTimeout(obj.timeout);
	
	for (i=0;i<obj.children;i++)
	{

	//	tmp=document.getElementById(obj.childArray[i].id);
	//	tmp.style.backgroundColor="#00ff00";

		window.clearTimeout(obj.childArray[i].timeout);
	}
	
	if (obj.parent!=0) clearUpperTimeouts(obj.parent);
}

function mouseOver(item)
{
	var tmp,i,j,obj,parent;
	i=1;
	
	obj=findItem(item.target.id);
	
	if (obj!=0) {
		dbg("over "+item.target.id+" (parent="+obj.parent.id+")");
	
		tmp=document.getElementById(item.target.id);
		tmp.style.backgroundColor=obj.overstyle;
	
		if (obj.parent!=0) {
			tmp=document.getElementById(obj.parent.id);
			tmp.style.backgroundColor=obj.parentstyle;
		}
	
		if (obj!=0) {
		
			window.clearTimeout(obj.timeout);
		
			// parent should remain shown
			clearUpperTimeouts(obj)
					
			// show all children
			for (i=0;i<obj.children;i++)
			{
				window.clearTimeout(obj.childArray[i].timeout);
				tmp=document.getElementById(obj.childArray[i].id);
				tmp.cbe.show();
				obj.childArray[i].visible=true;
				dbg("showing "+obj.childArray[i].id);
			}
			
			window.status=obj.status;
			
		} else {
		
			window.status="mouseover target not found "+item.target.id;
		
		}
	}
}

function hideChildren(obj)
{
	var i,tmp;
	try {
		if (obj!=0) {
			for (i=0;i<obj.children;i++)
			{
				obj.childArray[i].timeout=window.setTimeout("document.getElementById('"+obj.childArray[i].id+"').cbe.hide();",300);
				
				if (obj.childArray[i]!=0) 
					hideChildren(obj.childArray[i]);
			}
		}
	} catch(e)
	{
	}
}

function mouseOut(item)
{
	var i,tmp,obj;
	dbg("out "+item.target.id);

	// hide all except top items
	if (item.target.id!='') {
	
		tmp=document.getElementById(item.target.id);
		tmp.style.backgroundColor="";

		dbg("out "+item.target.id);
	
		obj=findItem(item.target.id);
		if (obj.parent!=0) 
			hideChildren(obj.parent);
		else
			hideChildren(obj);
	}				
}

function mouseClick(item)
{
	obj=findItem(item.target.id);
	window.location.href=obj.url;
}

