// touch.js
// Eric Knibbe
// on Webkit Mobile, makes drop-down menus behave by only enabling menu links if their submenus are visible.

if(RegExp(" Mobile\\b").test(navigator.userAgent) && RegExp(" AppleWebKit/").test(navigator.userAgent)) {
	var menuItems = document.getElementById('nav').getElementsByTagName('li');
	for (var item = menuItems[0]; item != null; item = item.nextSibling) {
		if (item.nodeName === "LI") {
			var itemChildren = item.childNodes;
			var itemAnchors = new Array();
			var itemDivs = new Array();
			for (var itemChild = itemChildren[0]; itemChild != null; itemChild = itemChild.nextSibling) {
				if (itemChild.nodeName === "A") {
					itemAnchors.push(itemChild);
				} else if (itemChild.nodeName === "DIV") {
					itemDivs.push(itemChild);
				}
			}
			if (itemAnchors.length > 0 && itemDivs.length > 0) {
				itemAnchors[0].href = '#' + itemAnchors[0].getAttribute('href');
				var defaultSubmenuStyle = getComputedStyle(itemDivs[0], '');
				itemDivs[0].defaultDisplay = defaultSubmenuStyle.getPropertyValue('display');
				itemDivs[0].defaultLeft = defaultSubmenuStyle.getPropertyValue('left');
				itemDivs[0].defaultOverflow = defaultSubmenuStyle.getPropertyValue('overflow');
				itemDivs[0].defaultRight = defaultSubmenuStyle.getPropertyValue('right');
				itemDivs[0].defaultVisibility = defaultSubmenuStyle.getPropertyValue('visibility');
				item.ontouchstart = function(){
					var itemAnchor = this.getElementsByTagName('a')[0];
					var itemDiv = this.getElementsByTagName('div')[0];
					var currentSubmenuStyle = getComputedStyle(itemDiv, '');
					if ((currentSubmenuStyle.getPropertyValue('display') !== itemDiv.defaultDisplay) ||
							(currentSubmenuStyle.getPropertyValue('left') !== itemDiv.defaultLeft) ||
							(currentSubmenuStyle.getPropertyValue('overflow') !== itemDiv.defaultOverflow) ||
							(currentSubmenuStyle.getPropertyValue('right') !== itemDiv.defaultRight) ||
							(currentSubmenuStyle.getPropertyValue('visibility') !== itemDiv.defaultVisibility)) {
						itemAnchor.href = itemAnchor.getAttribute('href').slice(1);
					}
				};
			}
		}
	}
}

