function showMenu(parent,submenu) {
	submenu.style.visibility = 'visible';
	submenu.style.left = parent.offsetLeft + 'px';
	if(document.all) {
		var trueTop = 0;
		var sumNode = parent;
		while(sumNode.offsetParent) {
			trueTop += sumNode.offsetTop;
			sumNode = sumNode.offsetParent;
		}
		submenu.style.top = trueTop + parent.offsetHeight + 'px';
	} else {
		submenu.style.top = parent.offsetTop + parent.offsetHeight + 'px';
	}
}

function menuIsVisible(submenu) {
	if(submenu.style.visibility == 'visible') return true;
	return false;
}

function showSubMenu(parent, submenu) {
	submenu.style.visibility = 'visible';
	submenu.style.left = parent.offsetLeft + parent.offsetWidth + 'px';
	submenu.style.top = parent.offsetTop + 'px';
}

function hideMenu(submenu) {
	submenu.style.visibility = 'hidden';
}



function Menubar() {
	var Tree;
	var Parent;

	this.createBranch = function(xml) {
		var items = xml.getElementsByTagName('item');	
		var branch = document.createElement('ul');
		branch.className = 'submenu';
		for(var i = 0; i < items.length; i++) {
			if(items[i].parentNode == xml) {
				var item = document.createElement('li');
				var itemLink = document.createElement('a');
				if(items[i].getAttribute('href')) {
					itemLink.href = items[i].getAttribute('href');
				} else {
					itemLink.href = '#';
				}

				if(items[i].getAttribute('target')) {
					itemLink.target = items[i].getAttribute('target');
				}

				if(items[i].getAttribute('id')) {
					itemLink.id = items[i].getAttribute('id');
				}
				itemLink.onclick = function () {
					hideMenu(branch);
				}
				itemLink.appendChild(document.createTextNode(items[i].getAttribute('title')));
				item.appendChild(itemLink);
				if(items[i].getAttribute('toggle') && items[i].getAttribute('toggle').match(/true/i)) {
					var checkBox = document.createElement('input');
					checkBox.type = 'checkbox';
					checkBox.disabled = true;
					checkBox.style.position = 'absolute';
					checkBox.style.right = '2px';
					itemLink.appendChild(checkBox);
					if(items[i].getAttribute('default') && items[i].getAttribute('default').match(/true/i)) {
						checkBox.defaultChecked = true;
					} else {
						checkBox.defaultChecked = false;
					}
					itemLink.onclick = function () {
						checkBox.checked = !checkBox.checked;
					}
				}

				
				branch.appendChild(item);
				var submenus = items[i].getElementsByTagName('item');
				for(var sb = 0; sb <  submenus.length; sb++) {
					var subBranch = this.createBranch(items[i]);
					item.appendChild(subBranch);
					
					item.onmouseover = function() {
						showSubMenu(this, this.getElementsByTagName('ul')[0]);
					}
					item.onmouseout = function() {
						hideMenu(this.getElementsByTagName('ul')[0]);
					}
				}
			
				if(document.all) {
					menuElements[menuElements.length] = item;
				}
			}
		}
		return branch;

	}

	var menuElements = new Array();

	var MySelf = this;
	this.populateMenuBar = function(xml) {
		var root = document.createElement('ul');
		root.className = 'menubar';
		root.hideAll = function (event) {
			if(window.event) { event = window.event; }
			var allMenus = root.getElementsByTagName('ul');
			for(var m = 0; m < allMenus.length; m++) {
				hideMenu(allMenus[m]);
			}
		}
		var menus = xml.getElementsByTagName('menu');
		for(var m = 0; m < menus.length; m++) {
			var menu = document.createElement('li');
			menu.className = 'menu';
			var menuLink = document.createElement('a');
			menuLink.appendChild(document.createTextNode(menus[m].getAttribute('title')));
			if(menus[m].getAttribute('href')) {
				menuLink.href = menus[m].getAttribute('href');
			} else {
				menuLink.href = '#';
			}
			
			if(menus[m].getAttribute('target')) {
				menuLink.target = menus[m].getAttribute('target');
			}

			if(menus[m].getAttribute('id')) {
				menuLink.id = menus[m].getAttribute('id');
			}

			menu.appendChild(menuLink);
			root.appendChild(menu);
			var branch = this.createBranch(menus[m]);
			menu.appendChild(branch);
			menu.onfocus = function (event) {
				if(window.event) { event = window.event; }
			//	root.hideAll();
				if(this.getElementsByTagName('ul')[0]) {
					var subItems = this.getElementsByTagName('ul')[0].getElementsByTagName('li');
					if(subItems.length > 0) {
						showMenu(this, this.getElementsByTagName('ul')[0]);
					}
				}
			}
			menu.onblur = function () {
				hideMenu(this.getElementsByTagName('ul')[0]);
				if(this.onmouseover) {
					this.onmouseover = function () { };
				}
			}

			

			if(document.all) {
				menu.onclick = function () {
					/*this.onmouseover = this.onfocus;
					this.onmouseover();
					this.onmouseout = this.onblur;*/
					root.hideAll();
					this.onfocus();

				}
				menuElements[menuElements.length] = menu;
			}


		}

		if(document.all) {
			document.attachEvent('onclick',IEonBlurSim);
		}
		if(Parent) {
			Parent.appendChild(root);
			var checkBoxes = root.getElementsByTagName('input');
			for(var i = 0; i < checkBoxes.length; i++) {
				if(checkBoxes[i].defaultChecked) {
					checkBoxes[i].checked = checkBoxes[i].defaultChecked;
				}
			}
		}
		return root;

	}
	
	function IEonBlurSim(event) {
		if(window.event) { event=  window.event; }
		var menuItem = event.srcElement.parentNode;
		for(var i = 0; i < menuElements.length; i++) {
			if(menuItem != menuElements[i]) {
				if(menuElements[i].onblur) {
					menuElements[i].onblur();
				}
			}
		}
	}
	
	this.setParent = function(p) {
		Parent = p;
		if(Tree) {
			Parent.appendChild(Tree);
			var checkBoxes = Tree.getElementsByTagName('input');
			for(var i = 0; i < checkBoxes.length; i++) {
				if(checkBoxes[i].defaultChecked) {
					checkBoxes[i].checked = checkBoxes[i].defaultChecked;
				}
			}
		}
	}
}
