var SigrieMenus = function() {
	var that = this
	
	// Config
	this.offsetTop = 5
	this.offsetLeft = -8
	this.hideDelay = 300
	// End config
	
	this.init = function() {
		this.menus = [] // Currently shown menus, in order from parent to child
		this.hideTimeout = -1
		
		this.findMenus()
	}
	
	this.findMenus = function() {
		var elements = document.getElementsByTagName("a")
		var buffer = [] // Ugh, live arrays
		
		for (var i in elements) {
			if (typeof(elements[i]) == "object") buffer.push(elements[i]);
		}
		
		for (var i in buffer) {
			var element = buffer[i]
			var menu = element.getAttribute("menu")
			
			if ((typeof(menu) == "string") && (typeof(sgMenus[menu]) == "object")) {
				var parent = document.createElement("span")
				element.parentNode.replaceChild(parent, element)
				parent.appendChild(element)
				
				element.menu = menu
				element.onmouseover = function() { that.showMenu(this.menu, 0, this.parentNode) }
			}
		}
	}
	
	this.showMenu = function(type, depth, parent, button) {
		if (typeof(button) != "object") button = parent;
		
		this.clearMenus(depth)
		
		var menu = {
			"type": type,
			"parent": parent,
			"button": button,
			"list": document.createElement("ul"),
			"depth": depth
		}
		var menuid = this.menus.push(menu) - 1
		
		addClass(button, "selected")
		addClass(menu.list, "menuitem")
		
		parent.appendChild(menu.list)
		
		this.populateMenu(menuid)
		
		menu.list.style.position = "absolute"
		
		if (depth == 0) {
			menu.list.style.top = (button.offsetTop + button.offsetHeight + this.offsetTop) + "px"
			menu.list.style.left = (button.offsetLeft + this.offsetLeft) + "px"
		}
		else {
			menu.list.style.top = (button.offsetTop) + "px"
			menu.list.style.left = (button.offsetLeft + button.offsetWidth) + "px"
		}
		
		parent.onmouseout = function() { that.resetHideTimer() }
		parent.onmouseover = function() { that.stopHideTimer() }
		
		this.stopHideTimer()
	}
	
	this.clearMenus = function(depth) {
		while (this.menus.length > depth) {
			var menu = this.menus.pop()
			removeClass(menu.button, "selected")
			
			while (menu.list.lastChild) {
				menu.list.removeChild(menu.list.lastChild)
			}
			
			menu.parent.removeChild(menu.list)
		}
	}
	
	this.populateMenu = function(menuid) {
		var menu = this.menus[menuid]
		var type = sgMenus[menu.type]
		
		if (typeof(type) != "object") return;
		
		for (var i in type) {
			var li = document.createElement("li")
			var a = document.createElement("a")
			
			if ((typeof(type[i].header) == "boolean") && (type[i].header === true)) {
				addClass(li, "menuheader")
			}
			
			if ((typeof(type[i].submenu) == "string") && (typeof(sgMenus[type[i].submenu]) == "object")) {
				addClass(li, "submenu")
				
				li.subtype = type[i].submenu
				li.onmouseover = function() { that.showMenu(this.subtype, this.menu.depth + 1, this.menu.list, this) }
			}
			else {
				li.onmouseover = function() { that.clearMenus(this.menu.depth + 1) }
			}
			
			a.textContent = a.innerText = type[i].name
			li.menu = menu
			
			if (typeof(type[i].href) == "string") {
				a.href = type[i].href
			}
			else {
				a.href = "#"
				a.onclick = function() { return false }
			}
			
			li.appendChild(a)
			menu.list.appendChild(li)
		}
	}
	
	this.resetHideTimer = function() {
		if (this.hideTimeout >= 0) window.clearTimeout(this.hideTimeout)
		this.hideTimeout = window.setTimeout(function() { that.hideTimer() }, this.hideDelay)
	}
	
	this.stopHideTimer = function() {
		window.clearTimeout(this.hideTimeout)
		this.hideTimeout = -1
	}
	
	this.hideTimer = function() {
		this.stopHideTimer()
		this.clearMenus(0)
	}
}

sigrieMenus = new SigrieMenus()
addLoadEvent(function(){sigrieMenus.init()})
