/*
Copyright (c) 2009, Dan "Ducky" Little & GeoMOOSE.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

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 onoverShowSubMenu() {
	showSubMenu(this, this.getElementsByTagName('ul')[0]);
}

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

function toggleCheckMark() {
	var checkMark = this.checkMark;
	if(checkMark.style.width == '0px') {
		checkMark.style.width = 'auto';
	} else {
		checkMark.style.width = '0px';
	}
}

function hideDoubleParent() {
	hideMenu(this.parentNode.parentNode);
}

function hideFirstUL() {
	hideMenu(this.getElementsByTagName('ul')[0]);
}

var MENUBAR_INSTANCE = 0;
function Menubar() {
	var Tree;
	var Parent;
	var MenuId = 'Menu'+MENUBAR_INSTANCE;
	MENUBAR_INSTANCE++;

	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');

				branch.appendChild(item);

				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 = hideDoubleParent;
				itemLink.appendChild(document.createTextNode(items[i].getAttribute('title')));
				item.appendChild(itemLink);
				itemLink.style.marginLeft = '22px';
				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;
					}*/
					var checkMark = document.createElement('img');
					checkMark.src = MENU_TOGGLE_ICON;
					checkMark.style.verticalAlign = 'middle';
					checkMark.style.position = 'absolute';
					checkMark.style.left = '2px';
					//checkMark.style.marginTop = '4px';
					if(items[i].getAttribute('default') && items[i].getAttribute('default').match(/true/i)) {
						checkMark.style.width = 'auto';;
					} else {
						checkMark.style.width = '0px';
					}
					itemLink.checkMark = checkMark;
					itemLink.onclick = toggleCheckMark;
					itemLink.appendChild(checkMark);
				}

				
				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]);
					}*/
					item.onmouseover = onoverShowSubMenu;
					item.onmouseout = hideFirstUL;
				}
			
				if(document.all) {
					//menuElements[menuElements.length] = item;
				}
			}
		}
		return branch;

	}

	//var menuElements = new Array();

	var MySelf = this;

	function rootHideAll(event) {
		if(window.event) { event = window.event; }
		var allMenus = this.getElementsByTagName('ul');
		for(var m = 0; m < allMenus.length; m++) {
			hideMenu(allMenus[m]);
		}

	}

	this.populateMenuBar = function(xml) {
		var root = document.createElement('ul');
		root.className = 'menubar';
		root.hideAll = rootHideAll;
		root.setAttribute('id',MenuId);

		var menus = xml.getElementsByTagName('menu');
		for(var m = 0; m < menus.length; m++) {
			var menu = document.createElement('li');
			root.appendChild(menu);
			menu.className = 'menu';

			var menuLink = document.createElement('a');
			menu.appendChild(menuLink);
			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');
			}

			var branch = this.createBranch(menus[m]);
			menu.appendChild(branch);
			branch = null;
			function focusFunction(event) {
				if(window.event) { event = window.event; }
				if(this.getElementsByTagName('ul')[0]) {
					var subItems = this.getElementsByTagName('ul')[0].getElementsByTagName('li');
					if(subItems.length > 0) {
						showMenu(this, this.getElementsByTagName('ul')[0]);
					}
				}
			}

			menu.onfocus = focusFunction;
			menu.onblur = hideFirstUL; 
			
			if(document.all) {
				function f() {
					this.parentNode.hideAll();
					this.onfocus();
				}
				menu.onclick = f;
				//menuElements[menuElements.length] = menu;
			}
			menu = null;


		}

		if(document.all) {
			document.attachEvent('onclick',IEonBlurSim);
		}
		if(Parent) {
			document.getElementById(Parent).appendChild(root);
		}
		root = null;
	}
	
	function IEonBlurSim(event) {
		if(window.event) { event=  window.event; }
		var menuItem = event.srcElement.parentNode;
		var menuElements = document.getElementById(Parent).getElementsByTagName('li');
		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) {
			document.getElementById(Parent).appendChild(Tree);
		}
	}
}
