

window.onload = function()
{
	/*loop over all elements*/
	var o = document.getElementsByTagName('*');
	var o_count = o.length;
	var o_index = 0;
	var oi = null;
	var ov = null;
	var oi_child = null;
	
	while(o_index < o_count)
	{
		oi = o[o_index];
		
if(oi.className.match(/(^|\s)menubar(\s|$)/))
{

}
/*check if this is a menubutton*/
if(attr_match(oi,'className','menubutton'))
{
	oi.level = $level = 1;
	
	if(attr_match(oi,'href','^CMS_UNLOCALIZED_PATH',true))
	{
		oi.className = oi.className.replace('menubutton','menubutton-active');
	}
	
	//associate / instanciate a menupanel and show it
	addEvent(oi,'mouseover',function($e)
	{
		//make sure IE also provides the event parameter
		if(typeof top['event'] != 'undefined')
		{
			$e = event;
			$e.target = event.srcElement;
		}
		
		var $this = $e.target;


		//check for other open menus and forcefully close them
		if(top['cms_ui_active_menupopup'])
		{
			top['cms_ui_active_menupopup'].hide(true);
		} 


		//just in time binding of menupopup
		if(typeof $this.menupopup == 'undefined')
		{
					$this.menupopup = nextSibling($this,'div');
					
					$this.menupopup.button = $this;
					
					$this.menupopup.active = true;
																					
					$this.menupopup.show = function()
					{
						var style = this.style;
						var $p = posdim(this.button);
						if(this.firstChild)
						{
							this.active = true;
							style.display = 'block';
							style.left = ($p[0]+0)+'px';
							style.top = ($p[1]+$p[3]+3)+'px';
						}
					};
					
					$this.menupopup.hide = function($force)
					{
						var style = this.style;
						var $p = posdim(this.button);
						if((!this.active) || (typeof $force != 'undefined' && $force))
						{
							style.display = 'none';
							top['cms_ui_active_menupopup'] = null;
						}
					};
					
					$this.menupopup.scheduleTimeout = function()
					{
						if(this.active)
						{
							top['cms_ui_active_menupopup'] = this;
							this.active = false;
							this.timeout = setTimeout(function(){if(top['cms_ui_active_menupopup']) top['cms_ui_active_menupopup'].hide();},400);
						}
					};
					
					$this.menupopup.onmouseover=function($e)
					{
						this.active = true;//cancel timeout
					};
					
					$this.menupopup.onmouseout=function($e)
					{
						this.scheduleTimeout();
					};
		}
		
		$this.menupopup.show();
	});
	
	
	//schedule a timeout as soon as the pointer leaves a menubutton
	addEvent(oi,'mouseout',function($e)
	{
		//make sure IE also provides the event parameter
		if(typeof top['event'] != 'undefined')
		{
			$e = event;
			$e.target = event.srcElement;
		}
		
		var $this = $e.target;
		$this.menupopup.scheduleTimeout();
	});
}

if(oi.className.match(/(^|\s)menulist(\s|$)/))
{
	//add a conventional "hidden input" to hold the value if the target element has a name
	if(typeof oi.name != 'undefined')
	{
		ov = document.createElement('input');
		ov.type='hidden';
		ov.name=oi.name;
		oi.parentNode.insertBefore(ov,oi);
	}
	
	//add show/hide aka. expand/collapse function
	oi.toggleState = 0;
	oi.toggle = function()
	{
		var p = posdim(this);
		var style = this.style;
		
		
		//just in time binding of menupopup element
		if(typeof this.menu == 'undefined')
		{
			style.width = (p[2]-23)+'px';/*the content will be positioned absolute so preserve width*/
			this.menu = document.getElementsByTagName('body')[0].appendChild(this.getElementsByTagName('div')[0].cloneNode(true));
			this.menu.className = 'menupopup';	
			
			style = this.menu.style;
			style.position = 'absolute';
			style.width = (p[2]-4)+'px';
			style.border = '1px solid #7299C6';
			style.borderTop = '0px none';
			style.padding = '0px 1px';
			style.zIndex = 1000;
			
			style = this.style;
		}
		
		if(this.toggleState)//hide
		{
			this.toggleState = 0;
			style.backgroundPosition='right -18px';/*normal button style*/
			this.menu.style.display='none';
		}
		else//show
		{
			this.toggleState = 1;
			
			style.backgroundPosition='right -54px';/*pressed button style*/
			
			
			style = this.menu.style;
			style.left = p[0]+'px';
			style.top = (p[1]+p[3]-1)+'px';
			style.display = 'block';
		}
	};
	
	//add mouseover handler
	oi.onmouseover=function(e)
	{
		if(typeof e == 'undefined'){e=event;e.target=e.srcElement;}
		if(!this.className.match(/(^|\s)menulist-active(\s|$)/))
		{
			this.style.backgroundPosition='right -36px';
		}
	};
	
	//add click handler
	oi.onclick=function(e)
	{
		if(typeof e == 'undefined'){e=event;e.target=e.srcElement;}
		

		this.toggle();

		
		if(this.className.match(/(^|\s)menulist(\s|$)/))
		{
			
			
			
			/*in order to allow the dropdown to expand beyond its container, it must be positioned absolutely*/
			//style.position='absolute';
			//style.left=p[0]+'px';
			//style.top=p[1]+'px';	
			
			
			
			
			



			//this.className=this.className.replace(/(^|\s)menulist(\s|$)/,'menulist-active');
		}
		else
		{
			this.className=this.className.replace(/(^|\s)menulist-active(\s|$)/,'menulist');
			this.style.backgroundPosition='right -36px';
		}
		return true;
	};
	
	
	oi.onmouseout=function(e)
	{
		if(typeof e == 'undefined'){e=event;e.target=e.srcElement;}
		if(!this.className.match(/(^|\s)menulist-active(\s|$)/))
		{
			this.style.backgroundPosition='right -18px';
		}
		//this.className=this.className.replace(/(^|\s)menulist-active(\s|$)/,'menulist');
	};
}

		++o_index;
	}
};



