var menuObjects = new Array();

function MenuBar()
{
	
	var ext = ".gif";
	var normal = "_norm";
	var over = "_roll";
	var rightItem = "a";
	var leftItem = "b";
	var bothItems = "c";
	var imgDir = "images/"
	
	var NORMALSTATE = 1;
	var ROLLOVER = 2;
	var ROLLLEFT = 3;
	var ROLLRIGHT = 4;
	var ROLLWITHLOCKED = 5;
	
	
	this.allImages = new Array();
	this.menuHeight = 28;
	this.subItemHeight = 23;
	this.AddMenuToDocument = AddMenuToDocument;
	this.menuReady = false;
	
	this.AddMenu = AddMenu;
	this.AddSeperator = AddSeperator;
	
	this.Items = new Array();
	this.ShowMenu = ShowMenu;
	this.SubTimeout = null;
	this.MenuParent = null;
	
	this.ActiveMenu = null;
	this.MenuIndex = menuObjects.length;
	
	this.checkCompleted = checkCompleted;
	this.visible = false;
	
	menuObjects[menuObjects.length] = this;

	return this;
	
	function AddMenu(imgName, action, locked)
	{
		
		this.Items[this.Items.length] = new MenuItem(imgName, action, locked);
		this.Items[this.Items.length-1].subMenu = new SubMenu();
		this.Items[this.Items.length-1].type = 0;
		this.Items[this.Items.length-1].activeSub = false;
	}
	
	function AddSeperator(imgName)
	{
		this.Items[this.Items.length] = new MenuSeperator(imgName);
	}
	
	function MenuItem(imgName, action, locked)
	{
		this.image = imgName;
		this.action = action;
		this.locked = locked;
		this.type = null;
		this.subMenu = null;
		this.node = null;
		this.activeSub = null; 
		
		this.AddSubItem = AddSubItem;
		
		function AddSubItem(imgName,action,locked)
		{
			this.subMenu.AddSubItem(imgName,action,locked);
		}
		return this;
	}
		
	function MenuSeperator(imgName)
	{
		var rslt = new MenuItem(imgName);
		rslt.type = 1;
		return rslt;
	}
	
	function SubMenu()
	{
			
		this.SubItems = new Array();
		this.AddSubItem = AddSubItem;
		this.node = null;
		this.locked = null;
		
		function AddSubItem(imgName, action,locked)
		{
			this.SubItems[this.SubItems.length] = new MenuItem(imgName, action);
			this.SubItems[this.SubItems.length - 1].type = 2;
			this.SubItems[this.SubItems.length - 1].locked = locked;
		}
		
		return this;
	}
	
	function GetPos(obj)
	{
		this.x = 0;
		this.y = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				this.x += obj.offsetLeft;
				this.y += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
		{
			this.x += obj.x;
			this.y += obj.y;
		}
		return this;
	}

	function ShowMenu(menuParent)
	{

		var i,z;
		this.MenuParent = menuParent;
		if(typeof document.readyState != "undefined")
		{	for(i=0;i<this.Items.length;i++)
			{
				this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].image,NORMALSTATE));
				
				if(this.Items[i].type==0)
					this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].image,ROLLOVER));
				else if (this.Items[i].type==1)
				{
					
					if(i>0)this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].image,ROLLRIGHT));
					if(i<this.Items.length-1)this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].image,ROLLLEFT));
				}
				if(this.Items[i].subMenu && this.Items[i].subMenu.SubItems.length > 0)
				{
					for(z=0; z<this.Items[i].subMenu.SubItems.length;z++)
					{
						this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].subMenu.SubItems[z].image,NORMALSTATE));
						this.allImages[this.allImages.length] = newImage(BuildImageName(this.Items[i].subMenu.SubItems[z].image,ROLLOVER));
					}
				}
			}
			this.checkCompleted();
		}else
		{
			this.menuReady = true;
		}
		this.AddMenuToDocument();
	}
	
	function AddMenuToDocument()
	{

		if(!this.menuReady) return window.setTimeout("menuObjects["+this.MenuIndex+"].AddMenuToDocument();",500);
		menuParent = this.MenuParent;
		
		var i,z;
		var subTable;
		var subRow;
		menuParent.innerHTML = "";
		var newImage = null;
			
		
		for(i=0;i<this.Items.length;i++)
		{
			
			var curTag = document.createElement("SPAN");
			menuParent.appendChild(curTag);
			
			if(this.Items[i].locked == null)
			{
				if(i == (this.Items.length - 1)  &&  this.Items[i-1] && this.Items[i-1].locked == 1 )
				{
					curTag.innerHTML = "<IMG src='"+ BuildImageName(this.Items[i].image,ROLLRIGHT) +"' border='0'>";;
				}
				else
				{
					curTag.innerHTML = "<IMG src='"+ BuildImageName(this.Items[i].image,NORMALSTATE) +"' border='0'>";
				}
			}
			else
			{
				if(this.Items[i].locked == 0)
				{
					curTag.innerHTML = "<IMG src='"+ BuildImageName(this.Items[i].image,NORMALSTATE) +"' border='0'>";
				}
				else
				{
					curTag.innerHTML = "<IMG src='"+ BuildImageName(this.Items[i].image,ROLLOVER) +"' border='0'>";
				}
			}
			newImage = curTag.firstChild;
			this.Items[i].node = newImage;
			AssignButtonEvents(newImage,this.Items,i,this.menuHeight);
			
			
			if((this.Items[i].locked == 1 && this.Items[i-1]) && (this.Items[i-2] && this.Items[i-2].locked == 1))
			{
				this.Items[i-1].node.src =  BuildImageName(this.Items[i-1].image,ROLLWITHLOCKED);
			}
			else if(this.Items[i].locked == 1 && this.Items[i-1])
			{
				this.Items[i-1].node.src =  BuildImageName(this.Items[i-1].image,ROLLLEFT);
			}
			else if(this.Items[i-2] && this.Items[i-2].locked == 1)
			{
				this.Items[i-1].node.src =  BuildImageName(this.Items[i-1].image,ROLLRIGHT);
			}
			
			
					
			if(this.Items[i].subMenu && this.Items[i].subMenu.SubItems.length > 0)
			{
				var subBox = document.createElement("SPAN");
				
				
				for(z=0; z<this.Items[i].subMenu.SubItems.length;z++)
				{
					var subElement = document.createElement("SPAN");
					subBox.appendChild(subElement);
					newImage = document.createElement("IMG");
					this.Items[i].subMenu.SubItems[z].node = newImage;					
					if(this.Items[i].subMenu.SubItems[z].locked != 1)
					{
						newImage.src = BuildImageName(this.Items[i].subMenu.SubItems[z].image,NORMALSTATE);	
					}
					else
					{
						newImage.src = BuildImageName(this.Items[i].subMenu.SubItems[z].image,ROLLOVER);
					}
					
					newImage.border = 0;
					subElement.appendChild(newImage);
					
					AssignButtonEvents(newImage,this.Items[i].subMenu.SubItems,z);
					if(z<this.Items[i].subMenu.SubItems.length-1)subElement.appendChild(document.createElement("BR"));
				}
					
				document.body.appendChild(subBox);
				
				this.Items[i].subMenu.node = subBox;
				subBox.style.display = "none";
			}
		}
	
		this.visible = true;
	}
	
	function AssignButtonEvents(btnObj, itemList, index, mnuHeight)
	{
		if(itemList[index].type == 1) return false;
		var mnuObj = this;
		
		if(itemList[index].locked == null)
		{
			btnObj.onmouseover = rollOver;
			btnObj.onmouseout = rollOut;
		}
		
		if(itemList[index].action != null)
		{
			btnObj.onclick = Function(itemList[index].action);
			btnObj.style.cursor = "hand";
		}
		if(itemList[index].type==0)
		{
			btnObj.onmousedown = showSub;
			btnObj.style.cursor = "hand";
		}
		
		else btnObj.onmousedown = CancelBubble;
		
		
		
		function rollOver()
		{
			window.clearTimeout(mnuObj.SubTimeout);
			if(itemList[index]==mnuObj.ActiveMenu) return false;
			if(itemList[index].type==0)HideSub();
			
			if(itemList[index].type==0 || itemList[index].type==2)this.src = BuildImageName(itemList[index].image, ROLLOVER);
			
			
			if(itemList[index].type==0)
			{
				if(index > 0 && itemList[index-1].type == 1)
				{
					if(itemList[index-2] && itemList[index-2].locked == 1)
					{
						if(itemList[index-1].type == 1)itemList[index-1].node.src = BuildImageName(itemList[index-1].image, ROLLWITHLOCKED);
					}
					else
					{
						if(itemList[index-1].type == 1)itemList[index-1].node.src = BuildImageName(itemList[index-1].image, ROLLLEFT);
					}
					
				}
				
				if(index < itemList.length-1 && itemList[index+1].type == 1)
				{
					if(itemList[index+2] && itemList[index+2].locked == 1)
					{
						itemList[index+1].node.src = BuildImageName(itemList[index+1].image, ROLLWITHLOCKED);	
					}
					else
					{
						itemList[index+1].node.src = BuildImageName(itemList[index+1].image, ROLLRIGHT);	
					}
					
				}
			}
			
		
		}
		
		function rollOut()
		{
			
			if(itemList[index]==mnuObj.ActiveMenu) return false;
			
			this.src = BuildImageName(itemList[index].image, NORMALSTATE);
			
			if(mnuObj.ActiveMenu) mnuObj.SubTimeout = window.setTimeout("document.body.onmousedown();",800);
			
			if(itemList[index].type==0)
			{
				if(index > 0 && itemList[index-1].type == 1)
				{
					if(itemList[index-2] && itemList[index-2].locked == 1)
					{
						itemList[index-1].node.src = BuildImageName(itemList[index-1].image, ROLLRIGHT);
					}
					else
					{
						itemList[index-1].node.src = BuildImageName(itemList[index-1].image, NORMALSTATE);
					}
				}
				
				if(index < itemList.length-1 && itemList[index+1].type == 1)
				{
					if(itemList[index+2] && itemList[index+2].locked == 1)
					{
						itemList[index+1].node.src = BuildImageName(itemList[index+1].image, ROLLLEFT);	
					}
					else
					{
						itemList[index+1].node.src = BuildImageName(itemList[index+1].image, NORMALSTATE);	
					}
				}
			}
		}
		
		
		function showSub(e)
		{
			CancelBubble(e);
			if(itemList[index]==mnuObj.ActiveMenu) return false;
			if(!itemList[index].subMenu.node) return true;
			
			var itemPos;
			if(index>0 && itemList[index-1].type == 1) itemPos = GetPos(itemList[index-1].node)
			else itemPos = GetPos(itemList[index].node);
			
			itemPos = GetPos(itemList[index].node);
			itemList[index].subMenu.node.style.display = "inline";
			itemList[index].subMenu.node.style.position = 'absolute';
			itemList[index].subMenu.node.style.left = itemPos.x + 'px';
			itemList[index].subMenu.node.style.top = itemPos.y + mnuHeight + 'px';
			
			
			mnuObj.ActiveMenu = itemList[index];
			window.document.body.onmousedown = HideSub;
		}
		
		function HideSub()
		{
			var actMenu = mnuObj.ActiveMenu;
			mnuObj.ActiveMenu = null;
			if(actMenu)
			{
				actMenu.subMenu.node.style.display = "none";
				actMenu.node.onmouseout();
				window.clearTimeout(mnuObj.SubTimeout);
				
			}
		}
		
	}
	
	function CancelBubble(e)
	{
		if(e)e.cancelBubble = true;
		else event.cancelBubble = true;
	}
	
	function BuildImageName(imgName,imgState)
	{
		switch(imgState)
		{
			case NORMALSTATE: return imgDir + imgName + normal + ext;
			case ROLLOVER: return imgDir + imgName + over + ext;
			case ROLLLEFT: return imgDir + imgName + over + leftItem + ext;
			case ROLLRIGHT: return imgDir + imgName + over + rightItem + ext;
			case ROLLWITHLOCKED: return imgDir + imgName + over + bothItems + ext;
		}
	
	}
	
	function checkCompleted()
	{
		var numCompleted = 0;
		var i;
		for(i=0;i<this.allImages.length;i++){
			if (this.allImages[i].complete) numCompleted++;
		}
		
		if(numCompleted == this.allImages.length){
			this.menuReady = true;
		}else{
			window.setTimeout("menuObjects["+this.MenuIndex+"].checkCompleted()",500);
		}
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
			


