// dropdown
Dropdownmanager=
{
	ds:[],
	add:function(d,p)
	{
		if(p!=undefined && p.add != undefined)
		{
			return p.add(d);
		}
		d.manager=this;
		this.ds.push(d);
		return d;
	},
	closeall:function()
	{
		for(var i=0;i<this.ds.length;i++)
		{
			if(this.ds[i].isopen && !this.ds[i].mouseover)
				this.ds[i].close(true);
		}
	}
};
function Dropdown(element_li)
{
	this.issub=false;
	this.ds=[];
	this.add=function(d)
	{
		this.hassub=true;
		var as=d.dropdown.parentNode.parentNode.getElementsByTagName('a');
		as[as.length-1].className='hassub';
		d.issub=true;
		d.manager=this;
		this.ds.push(d);
		return d;
	}
	this.closeall=function()
	{
		for(var i=0;i<this.ds.length;i++)
		{
			if(this.ds[i].isopen && !this.ds[i].mouseover)
				this.ds[i].close(true);
		}
	}
	
	this.op=element_li;
	var as=this.op.getElementsByTagName('a');
	this.but=as[0];
	this.outclass=this.but.className;
	this.dropdown=this.op.getElementsByTagName('ul')[0];
	this.dstyle=this.dropdown.style;
	
	var p=this;
	for(var i=0;i<as.length;i++)
	{
		as[i].onmouseover=function()
		{
			p.open();
		}
		as[i].onmouseout=function()
		{
			p.close();
		}
	}
	
	this.running=false;
	this.isopen=false;
	this.mouseover=false;
}
$pr=Dropdown.prototype;
$pr.open=function()
{
	this.mouseover=true;
	this.but.className=this.outclass+' hover';
	this.dropdown.className='over';
	if(this.issub)
		this.op.lastChild.previousSibling.className+=' over';
	if(!this.isopen)
	{
		this.isopen=true;
		
		var h = this.dropdown.offsetHeight;
		this.dstyle.height='1px';
		if(typeof phocus == 'object')
			this.run=phocus.Runtime.addrun(this,'runclose',[1,h],0,10,this,'endopen');
		else
			this.endopen();
		this.manager.closeall();
		this.dstyle.zIndex='101';
	} else
	{
		this.endopen();
	}
	clearInterval(this.closer);
}
$pr.close=function(override,interval)
{
	this.mouseover=false;
	if(this.issub)
		this.manager.close(true,500);
	
	if(override)
	{
		clearInterval(this.closer);
		this.run.escape();
	}
	
	if(this.isopen)
	{
		var _doclose=this.doclose;
		var _obj=this;
		this.closer=setInterval(function(){_doclose.apply(_obj)},interval ? interval : override ? 10 : 400);
		this.dstyle.zIndex='100';
	}
}
$pr.doclose=function()
{
	if(!this.mouseover)
	{
		this.dstyle.overflow='hidden';
		var h = this.dropdown.offsetHeight;
		if(typeof phocus == 'object')
			this.run=phocus.Runtime.addrun(this,'runclose',[h,1-h],0,4,this,'endclose');
		else
			this.endclose();
		this.but.className=this.outclass;
		this.dropdown.className='out';
	}
	clearInterval(this.closer);
}
$pr.runclose=function(start,ch,time,runtime)
{
	this.running=true;
	this.dstyle.height=Math.floor(start+(ch/runtime*(runtime-time+1)))+'px';
}
$pr.endclose=function()
{
	if(this.issub)
		this.op.lastChild.previousSibling.className=this.op.lastChild.previousSibling.className.split(' ')[0];
	this.running=false;
	this.dstyle.height='';
	this.isopen=false;
	this.dropdown.className='';
}
$pr.endopen=function()
{
	if(this.issub && this.mouseover)
		this.manager.open();
	this.running=false;
	this.dstyle.height='';
	this.dstyle.overflow='visible';
	this.run.escape();
}


delete $pr;

window.onload=function()
{
	var mainmenu=document.getElementById('mainmenu');
	var lis=mainmenu.childNodes;
	// looping through the initial level of menu items
	for(var i=0;i<lis.length;i++)
	{
		var op=lis[i];
		if(op.tagName != "LI")
			continue;
		if(op.getElementsByTagName('ul').length)
			var menu=Dropdownmanager.add(new Dropdown(op));
		else
			continue;
		// looping through any sub-sub-menus that may exist
		var sublist=op.getElementsByTagName('ul')[0];
		var childer=sublist.childNodes;
		for(var j=0;j<childer.length;j++)
		{
			var subop=childer[j];
			if(subop == undefined || subop.tagName != "LI")
				continue;
			if(subop.getElementsByTagName('ul').length)
				Dropdownmanager.add(new Dropdown(subop),menu);
		}
	}
}