//
// MenuBarStyle - Geometric attributes of a menu bar
//
function MenuBarStyle(id, width, height, itemWidth, layerIndex, xMenuStartPosition, yMenuStartPosition)
{
	this.id = id
	this.width = width
	this.height = height
	this.itemWidth = itemWidth
	this.layerIndex = layerIndex
	this.xMenuStartPosition = xMenuStartPosition
	this.yMenuStartPosition = yMenuStartPosition
}

//
// Brush - Color and text size
//
function Brush(id, backgroundColor, textColor, textSize)
{
	this.id = id
	this.backgroundColor = backgroundColor
	this.textColor = textColor
	this.textSize = textSize
}

//
// MenuStyle object - Geometric attributes of a menu
//
function MenuStyle(id, itemWidth, internalSpacing, frameWidth, frameColor, nsMenuItemHeight)
{
	this.id = id
	this.itemWidth = itemWidth
	this.internalSpacing = internalSpacing
	this.frameWidth = frameWidth
	this.frameColor = frameColor
	this.nsMenuItemHeight = nsMenuItemHeight
}

//
// MenuBarsEngine - generates menu bars
//
function MenuBarsEngine(id)
{
	if (document.menuBarsEngine != null)
	{
		alert("Engine should be built only once")
		return document.menuBarsEngine
	}
	else
	{
		document.menuBarsEngine = this
	}


	this.id = id
	this.menuBars = new Array()
	this.createMenuBar = createMenuBar
	this.onClick = onClick
	this.onresize = this.onResize
	
	if (is_nav4)
	{
		document.onclick = this.onClick
		document.captureEvents(Event.CLICK)
		window.onresize = this.onResize
	}
	
	function createMenuBar(id, style, brush, menuStyle, menuItemBrush, selectedMenuItemBrush, leftMenuStyle)
	{
		var nextEmpty = this.menuBars.length
		this.menuBars[nextEmpty] = new MenuBar(id, style, brush, menuStyle, menuItemBrush, selectedMenuItemBrush, leftMenuStyle)
		
		return this.menuBars[nextEmpty] 
	}
	
	function getMenuBar(id)
	{
		var i
		
		for(i=0; i < this.menuBars.length; i++)
		{
			if (this.menuBars[i].getId() == id)
			{
				return this.menuBars[i]
			}
		}
		
		alert("Menu bar \"" + id + "\" not found")
		return null
	}
	
	function onClick(event)
	{
		var i
		
		var menuBarsEngine = document.menuBarsEngine
		if (is_nav4)
		{
			for(i=0; i < menuBarsEngine.menuBars.length; i++)
			{
				menuBarsEngine.menuBars[i].nsClick()
			}
		}
		
		return document.routeEvent(event)
	}
	
	function onResize()
	{
		location.reload(true)
	}

	function generate()
	{
		var i
		
		for(i=0; i < this.menuBars.length; i++)
		{
			this.menuBars[i].generate()
		}
	}
}

//
// LeftMenuStyle - Left menu style attributes
//
function LeftMenuStyle(id, width, imagesPath, textClass, selectedTextClass)
{
	this.id = id
	this.width = width
	this.imagesPath = imagesPath
	this.textClass = textClass
	this.selectedTextClass = selectedTextClass
}


//
// MenuBar - Menus container
//
function MenuBar(id, style, brush, menuStyle, menuItemBrush, selectedMenuItemBrush, leftMenuStyle)
{
	this.style = style
	this.brush = brush
	this.menuStyle = menuStyle
	this.menuItemBrush = menuItemBrush
	this.selectedMenuItemBrush = selectedMenuItemBrush
	this.leftMenuStyle = leftMenuStyle
	
	this.childMenus = new Array()
	this.childMenusNames = new Array()
	this.openMenu = null
	this.id = id
	this.getId = getId
	this.addMenu = addMenu
	this.hideAllMenus = hideAllMenus
	this.getMenu = getMenu
	this.showMenu = showMenu
	this.hideMenu = hideMenu
	this.hideInactiveMenus = hideInactiveMenus
	this.generate = generate
	this.overMenuItem = overMenuItem
	this.leavingMenuItem = leavingMenuItem
	this.getMenuPosition = getMenuPosition
	this.nsClick = nsClick
	this.generateStyles = generateStyles
	this.draw = draw
	
	function addMenu(id, name, url, imageUrl)
	{
		var nextEmpty = this.childMenus.length
		this.childMenus[nextEmpty] = new Menu(id, name, this, url, imageUrl)
		this.childMenusNames[nextEmpty] = name
		
		return this.childMenus[nextEmpty]
	}
	
	function generate()
	{
		var i
		
		this.generateStyles()
		
		for(i=0; i < this.childMenus.length; i++)	
		{
			this.childMenus[i].generate()
		}
	}
	
	function generateStyles()
	{
		//
		// Generate 
		//
		var htmlCode;
		var menu
		
		menu = this.childMenus[0]
		htmlCode = "<style>" + 
			".menu {" + 
			"position:absolute;" + 
			"z-index:" + this.style.layerIndex + ";" + 
			"} " + 
			".menulink {" + 
			"text-decoration: none;" + 
			"font-face: Arial;" + 
			"font-size:" + this.brush.textSize + ";" + 
			"color:" + this.brush.textColor + ";" + 
			"} " + 
			".menuitem {" + 
			"text-decoration:none;" + 
			"cursor:hand;" + 
			"font-face: Arial;" + 
			"font-size:" + this.menuItemBrush.textSize + ";" + 
			"background-color:" + this.menuItemBrush.backgroundColor + ";" + 
			"color:" + this.menuItemBrush.textColor + ";" + 
			"} " + 
			".menuitemns {" + 
			"text-decoration:none;" + 
			"cursor:hand;" + 
			"font-face: Arial;" + 
			"font-size:" + this.selectedMenuItemBrush.textSize + ";" + 
			"background-color:" + this.selectedMenuItemBrush.backgroundColor + ";" + 
			"color:" + this.selectedMenuItemBrush.textColor + ";" + 
			"} " + 
			"</style>"
		document.write(htmlCode)
	}
	
	function getId()
	{
		return this.id
	}
	
	function hideAllMenus()
	{
		var i
		
		for(i=0; i < this.childMenus.length; i++)	
		{
			this.childMenus[i].hide()
		}
	}

	function hideInactiveMenus()
	{
		var i

		for(i=0; i < this.childMenus.length; i++)	
		{
			if (this.openMenu == null || this.openMenu != this.childMenus[i].id)
			{
				this.childMenus[i].hide()
			}
		}
	}


	function getMenu(id)
	{
		var menuPosition = this.getMenuPosition(id)
		if (menuPosition != null)
		{
			return this.childMenus[menuPosition]
		}
		else
		{
			return null
		}
	}
	
	function getMenuPosition(id)
	{
		var i
		for(i=0; i < this.childMenus.length; i++)
		{
			if (this.childMenus[i].id == id)
			{
				return i
			}
		}
		
		alert("Menu \"" + id + "\" not found")
		return null
	}
	
	function overMenuItem(menu, item)
	{
		this.showMenu(menu)
		this.getMenu(menu).overItem(item)
	}

	function leavingMenuItem(menu, item)
	{
		this.getMenu(menu).leavingItem(item)
		this.hideMenu(menu)
	}
	
	function showMenu(id)
	{
		var menuPosition = this.getMenuPosition(id)
		
		if (menuPosition == null)
		{
			alert("Menu \"" + id + "\" not found - could not be displayed")	
			return
		}

		this.hideAllMenus()	
		this.childMenus[menuPosition].show(menuPosition)
		this.openMenu = id
	}
	
	function hideMenu(id)
	{
		setTimeout(this.id + ".hideInactiveMenus()", 500)
		this.openMenu = null
	}
	
	function nsClick()
	{
		var	activeMenu = this.openMenu
		if (activeMenu != null)
		{
			this.getMenu(activeMenu).navigate()
		}
	}
	
	function draw()
	{
		var	htmlCode
		var 	i
		var	url

		htmlCode = "<table border=\"0\" align=\"center\" width=\"" +
			this.style.width + "\" cellspacing=\"0\">" +
			"<tr><td bgcolor=\"" + this.brush.backgroundColor + "\" "+
			"height=\"" + this.style.height + "\">" +
			"<table cellspacing=\"0\" cellpadding=\"5\" border=\"0\" " +
			"width=\"" + this.childMenus.length * this.style.itemWidth + "\"><tr>"
		
		for (i=0; i < this.childMenus.length; i++)
		{
			url = (this.childMenus[i].url == null ? "#" : this.childMenus[i].url)
			htmlCode += "<td width=\"" + this.style.itemWidth + "\">" +
				"<a class=\"menulink\" href=\"" + url + "\"" +
				" onMouseOver=\"" + this.id + ".showMenu('" + 
				this.childMenus[i].id + "')\"" +
				" onMouseOut=\"" + this.id + ".hideMenu('" + 
				this.childMenus[i].id + "')\">" +
				this.childMenusNames[i] + "</a>" +
				"</td>"
		}
		htmlCode += "</tr></table></td></tr></table>"

		document.write(htmlCode)
	}
	
}


//
// Menu - Menu items container
//
function Menu(id, name, menuBar, url, imageUrl)
{
	this.id = id
	this.name = name
	this.menuBar = menuBar
	this.url = url
	this.imageUrl = imageUrl
	

	this.menuItems = new Array()
	this.menuItemsText = new Array()
	this.menuItemsUrl = new Array()
	this.menuItemsLineCount = new Array()
	this.activeItem = null
	this.hide = hide
	this.show = show
	this.draw = draw
	this.generate = generate
	this.addMenuItem = addMenuItem
	this.overItem = overItem
	this.leavingItem = leavingItem
	this.getItemPosition = getItemPosition
	this.navigate = navigate
	this.totalLineCount = 0
	
	function addMenuItem(id, text, url, lineCount)
	{
		var nextEmpty = this.menuItems.length
		this.menuItems[nextEmpty] = id
		this.menuItemsText[nextEmpty] = text
		this.menuItemsUrl[nextEmpty] = url
		if (lineCount != null)
		{
			this.menuItemsLineCount[nextEmpty] = lineCount
		}
		else
		{
			this.menuItemsLineCount[nextEmpty] = 1
		}
		this.totalLineCount += this.menuItemsLineCount[nextEmpty]
	}
	
	function generate()
	{
		var	i
		var	item
		var	text
		var htmlCode
		var url
		var menuBackgroundWidth
		var menuBackgroundHeight
		var lineCount
		var nsMenuItemHeight
		
		htmlCode = ""
	
		//
		// Generate the menu
		//	
		if (is_nav4)
		{
			menuBackgroundWidth = this.menuBar.menuStyle.itemWidth + (2 * this.menuBar.menuStyle.frameWidth)
			menuBackgroundHeight = (this.menuBar.menuStyle.nsMenuItemHeight + this.menuBar.menuStyle.frameWidth) * this.totalLineCount
			htmlCode += "<layer id=\"" + this.id + "\"" + 
				" width=\"" + menuBackgroundWidth + "\"" +
				" height=\"" + menuBackgroundHeight + "\"" +
				" bgColor=\"" + this.menuBar.menuStyle.frameColor + "\"" +
				" z-index=\"" + this.menuBar.style.layerIndex + "\"" +
				" visibility=\"hide\" class=\"menu\"></layer>"
		}
		else
		{
			htmlCode += "<div id=\"" + this.id + "\" class=\"menu\"  style=\"visibility:hidden\">" +
				"<table bgcolor=\"" + this.menuBar.menuStyle.frameColor + "\" cellspacing=\"" + 
				this.menuBar.menuStyle.frameWidth + "\" cellpadding=\"" + 
				this.menuBar.menuStyle.internalSpacing + "\" width=\"" + this.menuBar.menuStyle.itemWidth + "\">"
		}
			
		//
		// Generate menu items
		//
		for(i=0; i < this.menuItems.length; i++)
		{
			item = this.menuItems[i]
			text = this.menuItemsText[i]
			url = this.menuItemsUrl[i]
			lineCount = this.menuItemsLineCount[i]
			nsMenuItemHeight = ((this.menuBar.menuStyle.nsMenuItemHeight + 
				this.menuBar.menuStyle.frameWidth)* (lineCount - 1)) + 
				this.menuBar.menuStyle.nsMenuItemHeight
			if (is_nav4)
			{
				htmlCode += "<layer id=\"" + item + "\" width=\"" + 
					this.menuBar.menuStyle.itemWidth + "\" class=\"menu\"" + 
					" height=\"" + nsMenuItemHeight + "\"" +
					" z-index=\"" + (this.menuBar.style.layerIndex) + "\"" +
					" visibility=\"hide\" bgColor=\"" + this.menuBar.menuItemBrush.backgroundColor + "\"" +
				    " onMouseOver=\"" + this.menuBar.getId() + ".overMenuItem('" + this.id + "','" + item + "')\"" +
				    " onMouseOut=\"" + this.menuBar.getId() + ".leavingMenuItem('" + this.id + "','" + item + "')\"" +
				    " clip=\"0,0," + this.menuBar.menuStyle.itemWidth + "," + nsMenuItemHeight + "\"" +
				    ">" +
				    "&nbsp;<a href=\"" + url + "\" class=\"menuitem\">" + 
				    text + "</a>" +
				    "</layer>"
			}
			else
			{
				htmlCode += "<tr>" +
					"<td id=\"" + item + "\" class=\"menuitem\"" + 
					" onMouseOver=\"" + this.menuBar.getId() + ".overMenuItem('" + this.id + "','" + item + "')\"" +
					" onMouseOut=\"" + this.menuBar.getId() + ".leavingMenuItem('" + this.id + "','" + item + "')\"" +
					" onClick=\"window.location='" + url +"'\"" +
					">" + text + "</td>" +
					"</tr>"			
			}
		}

		if (!is_nav4)
		{		
			htmlCode += "</table></div>"
		}
		document.write(htmlCode)
		
		//
		// Set the cursor
		//
		for(i=0; i < this.menuItems.length; i++)
		{
			if (is_nav)
			{
				getStyle(this.menuItems[i]).cursor = "pointer"
			}
			else if (is_ie)
			{
				getStyle(this.menuItems[i]).cursor = "hand"
			}
		}
	}

	function hide()
	{
		getStyle(this.id).visibility = "hidden"
		if (is_nav4)
		{
			for(i=0; i < this.menuItems.length; i++)
			{
				getStyle(this.menuItems[i]).bgColor = this.menuBar.menuItemBrush.backgroundColor
				getStyle(this.menuItems[i]).visibility = "hide"
			}			
		}
	}

	function show(position)
	{
		var xPos = (position * this.menuBar.style.itemWidth) + this.menuBar.style.xMenuStartPosition
		var yPos = this.menuBar.style.yMenuStartPosition
		var itemId
		var nextItemPosition

		if (is_ie)
		{
			xPos += getElementById("main").offsetLeft
		}
		else if (is_nav)
		{
			xPos += (window.innerWidth - (this.menuBar.style.width + this.menuBar.style.xMenuStartPosition))/2
		}

		getStyle(this.id).top = yPos
		getStyle(this.id).left = xPos
		getStyle(this.id).visibility = "visible"
		if (is_nav4)
		{
			nextItemPosition = 0
			getElementById(this.id).bgColor = this.menuBar.menuStyle.frameColor
			for(i=0; i < this.menuItems.length; i++)
			{
				itemId = this.menuItems[i]
				getStyle(itemId).bgColor = this.menuBar.menuItemBrush.backgroundColor
				getStyle(itemId).top = yPos + nextItemPosition
				getStyle(itemId).left = xPos + 1
				getStyle(itemId).visibility = "show"
				nextItemPosition += this.menuItemsLineCount[i] * (this.menuBar.menuStyle.nsMenuItemHeight + this.menuBar.menuStyle.frameWidth)
			}			
		}
	}
	
	function overItem(item)
	{
		this.activeItem = item
		if (is_nav4)
		{
			getStyle(item).bgColor = this.menuBar.selectedMenuItemBrush.backgroundColor
			getElementById(item).document.write("&nbsp;<a href=\"#\" class=\"menuitemns\">" + 
			    this.menuItemsText[this.getItemPosition(item)] + "</a>")
			getElementById(item).document.close()
		}	
		else
		{
			getStyle(item).backgroundColor = this.menuBar.selectedMenuItemBrush.backgroundColor
			getStyle(item).color = this.menuBar.selectedMenuItemBrush.textColor	
		}
		
	}

	function leavingItem(item)
	{
		if (is_nav4)
		{
			getStyle(item).bgColor = this.menuBar.menuItemBrush.backgroundColor
			getElementById(item).document.write("&nbsp;<a href=\"#\" class=\"menuitem\">" + 
			    this.menuItemsText[this.getItemPosition(item)] + "</a>")
			getElementById(item).document.close()
		}	
		else
		{
			getStyle(item).backgroundColor = this.menuBar.menuItemBrush.backgroundColor
			getStyle(item).color = this.menuBar.menuItemBrush.textColor
		}
		this.activeItem = null
	}

	function getItemPosition(id)
	{
		var i
		for(i=0; i < this.menuItemsText.length; i++)
		{
			if (this.menuItems[i] == id)
			{
				return i
			}
		}

		alert("Could not find item:" + id)
		return null
	}
	
	function navigate()
	{
		var activeItem = this.activeItem
		if (activeItem != null)
		{
			window.location = this.menuItemsUrl[this.getItemPosition(activeItem)]
		}
	}
	
	function draw(selectedItem)
	{
		var	htmlCode
		var i
		var	url
		var textClass
		
		htmlCode = "<table valign=\"top\" width=\"" + this.menuBar.leftMenuStyle.width + "\" cellspacing=\"0\"" +
			       " cellpadding=\"0\" border=\"0\" bgcolor=\"#ffffff\">" +
       			   "<tr height=\"2\"><td>&nbsp;</td></tr>" +
			       "<tr><td valign=\"top\" bgcolor=\"white\">" +
				   "<a href=\"" + this.url + "\"><img src=\"" + 
				   this.menuBar.leftMenuStyle.imagesPath + this.imageUrl +"\" border=\"0\"></a>" +
				   "</td></tr><tr height=10></tr>"

		
		for (i=0; i < this.menuItemsText.length; i++)
		{
			url = (this.menuItemsUrl[i] == null ? "#" : this.menuItemsUrl[i])
			textClass = "leftMenu"
			if (this.menuItems[i] == selectedItem)
			{
				textClass = this.menuBar.leftMenuStyle.selectedTextClass
			}
			else
			{
				textClass = this.menuBar.leftMenuStyle.textClass
			}
			
			htmlCode += "<tr height=\"10\"></tr><tr><td valign=\"top\" bgcolor=\"white\">" +
						"&nbsp;&nbsp;&nbsp;<a class=\"" + textClass + "\"" +
						 "href=\"" + this.menuItemsUrl[i] + "\">" +
						  this.menuItemsText[i] + "</a></td></tr>"
		}
		htmlCode += "</table>"

		document.write(htmlCode)
	}
	
}


				
