/** 
 * MOB.menu:
 * A simple dropdown menu constructed from an html list
 * Compatability:
 * Tested with IE 6, Opera 9, Firefox 1.5
 */
MOB.menu = function(id,type,dontUseClassName){
	
	if(!this.browserCapable())
	{
		return;
	}			
	this.CONTAINER = document.getElementById(id);
	this.TYPE = type;
	this.DONT_USE_CLASSNAME = dontUseClassName;
	this.initialise();
}
/**
 * Check that a browser is capable of drawing the menu
 */
MOB.menu.prototype.browserCapable = function(){
	if(navigator.userAgent.indexOf('Firefox')!=-1) return true;
	if(navigator.userAgent.indexOf('MSIE')!=-1 && navigator.userAgent.indexOf('Opera')==-1) return true;
	return true;
}
/**
 * Initialise the list items
 */
MOB.menu.prototype.initialise = function(){
	var LIs = this.CONTAINER.getElementsByTagName('LI');
	for(var i=0;i<LIs.length;i++)
	{
		LIs[i].START_CLASS = LIs[i].className;
		LIs[i].style.display = 'block';
		LIs[i].onmouseover = function(){				
			this.className = this.START_CLASS+'Over';
		}
		LIs[i].onmouseout = function(){
			this.className = this.START_CLASS;
		}
	}	
	var ULs = this.CONTAINER.getElementsByTagName('UL');
	
	if(!this.DONT_USE_CLASSNAME)
	{
		ULs[0].className = this.TYPE+'Menu';
	}
	for(var i=0;i<ULs.length;i++)
	{
		if(ULs[i].parentNode!=this.CONTAINER)
		{			
			
			ULs[i].parentNode.WIDTH = ULs[i].parentNode.parentNode.offsetWidth;
			ULs[i].style.display = 'none';
			ULs[i].style.position = 'absolute';
			ULs[i].style.visibility = 'hidden';
			ULs[i].parentNode.START_CLASS = ULs[i].parentNode.className;
			ULs[i].parentNode.MENU = this;
			ULs[i].parentNode.SUBMENU = ULs[i];
			ULs[i].parentNode.onmouseover = function(){		
				var sWidth = getScreenWidth();						
				this.SUBMENU.style.display = 'block';
				this.className = this.START_CLASS+'Over';
				if(this.parentNode.parentNode==this.MENU.CONTAINER && this.MENU.TYPE=='horizontal')
				{
					this.SUBMENU.style.left = getAbsoluteLeft(this)+'px';	
					if(document.all) this.SUBMENU.style.top = getAbsoluteTop(this)+this.offsetHeight+'px';			
				}
				else
				{																
					if(getAbsoluteLeft(this)+this.offsetWidth+this.SUBMENU.offsetWidth < sWidth)
					{ 
						if(document.all)
						{
							this.SUBMENU.style.marginLeft = (this.offsetWidth)+'px';
							this.SUBMENU.style.marginTop = '-'+(this.offsetHeight)+'px';
						}
						else
						{
							this.SUBMENU.style.marginLeft = (this.offsetWidth)+'px';
							this.SUBMENU.style.marginTop = '-'+(this.offsetHeight)+'px';
						}
					}
					else
					{
						if(document.all)
						{
							this.SUBMENU.style.marginLeft = '-'+(this.offsetWidth+2)+'px';
							this.SUBMENU.style.marginTop = '-'+(this.offsetHeight)+'px';
						}
						else
						{
							this.SUBMENU.style.marginLeft = '-'+(this.offsetWidth+2)+'px';
							this.SUBMENU.style.marginTop = '-'+(this.offsetHeight)+'px';
						}
					}
				}
				
				this.SUBMENU.style.visibility = 'visible';
			}
			ULs[i].parentNode.onmouseout = function(){
				this.className = this.START_CLASS;
				this.SUBMENU.style.visibility = 'hidden';
			}
		}
	}
}
