// --------------------------------------------------------------------------------
// ow_navigation.js
// Matthew Hogg 20-Aug-2004
// Drives the dropdown navigation for the front-end of a site.
// --------------------------------------------------------------------------------

var ow_navDistribute = false;
var ow_navBrowserOK = true;
var ow_navigation = null
var ow_navCloseSub = true;
var ow_navActiveSub = "";
var ow_navOffset = 0;
var ow_navBound = false;

if (navigator.platform.toLowerCase().indexOf("win") != -1) ow_f_AppendLoadEvent(ow_NavInit);

// --------------------------------------------------------------------------------
// ow_NavMoveX()
// Determine horizontal positioning of submenu.
// --------------------------------------------------------------------------------
// Arguments:
//	- subid: id of submenu to calculate for [string]
// Returns:
//	- desired x-coordinate for submenu [integer]
// --------------------------------------------------------------------------------
function ow_NavMoveX(subid) {
	var smwidth = parseInt((ow_f_GetElementStyle(document.getElementById(subid), "width")).replace("px", ""), 10);
	var mmwidth = parseInt(ow_navigation.offsetWidth, 10);

	var leftX = ow_f_GetTotalOffset(document.getElementById((subid).replace("ow_mainNav_sub", "ow_mainNav_")), "offsetLeft");
	var menuOffset = ow_f_GetTotalOffset(ow_navigation, "offsetLeft");

	if (document.all && navigator.platform.toLowerCase().indexOf("win") == -1) {
		var padding = parseInt((ow_f_GetElementStyle(document.getElementById((subid).replace("ow_mainNav_sub", "ow_mainNav_")), "padding-left")).replace("px", ""), 10);
		if (isNaN(padding)) padding = 0;
		var bod = parseInt((ow_f_GetElementStyle(document.getElementsByTagName("body")[0], "margin-left")).replace("px", ""), 10);
		
		if (ow_navigation.offsetParent.tagName.toLowerCase() == "body") {
			return leftX - padding + bod;
		} else {
			return leftX - padding;		
		}
	} else {
		if ( ((leftX - menuOffset) + smwidth > mmwidth) && ow_navBound) {
			return leftX - ((leftX - menuOffset) + smwidth) + mmwidth;
		} else {
			return leftX;
		}
	}	
}

// --------------------------------------------------------------------------------
// ow_NavMoveY()
// Determine vertical positioning of submenu.
// --------------------------------------------------------------------------------
// Arguments:
//	- subid: id of submenu to calculate for [string]
// Returns:
//	- desired y-coordinate for submenu [integer]
// --------------------------------------------------------------------------------
function ow_NavMoveY(subid) {
	var menuOffset = ow_f_GetTotalOffset(ow_navigation, "offsetTop");
	
	if (document.all && navigator.platform.toLowerCase().indexOf("win") == -1) {
		var bod = parseInt((ow_f_GetElementStyle(document.getElementsByTagName("body")[0], "margin-top")).replace("px", ""), 10);
		if (ow_navigation.offsetParent.tagName.toLowerCase() == "body") {
			return menuOffset + ow_navigation.offsetHeight + ow_navOffset + bod;
		} else {
			return ow_navigation.offsetHeight + ow_navOffset;
		}
	} else {
		return menuOffset + ow_navigation.offsetHeight + ow_navOffset;
	}
}

// --------------------------------------------------------------------------------
// ow_NavOverlap()
// Determine if two elements are overlapping.
// --------------------------------------------------------------------------------
// Arguments:
//	- a: element 1 [object]
//	- b: element 2 [object]
// Returns:
//	- are elements overlapping? [boolean]
// --------------------------------------------------------------------------------
function ow_NavOverlap(a, b) {
	var ax = ow_f_GetTotalOffset(a, "offsetLeft");
	var ay = ow_f_GetTotalOffset(a, "offsetTop");
	var aw = a.offsetWidth;
	var ah = a.offsetHeight;
	var bx = ow_f_GetTotalOffset(b, "offsetLeft");
	var by = ow_f_GetTotalOffset(b, "offsetTop");
	var bw = b.offsetWidth;
	var bh = b.offsetHeight;

    if (((ax + aw) < bx) || (ax > (bx + bw)) || ((ay + ah) < by) || (ay > (by + bh))) return false;
    else return true;
}

// --------------------------------------------------------------------------------
// ow_NavFixSelects()
// Make visible any currently hidden select elements.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavFixSelects() {
	if (document.all) {
		var selects = document.getElementsByTagName("select");
		for (var i = 0; i < selects.length; i++) {
			if (selects[i].style.visibility == "hidden") {
				selects[i].style.visibility = "visible";
			}
		}
	}
}

// --------------------------------------------------------------------------------
// ow_NavShowSub()
// Show submenu for menu item that fired a mouseover event.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavShowSub() {
	var elm = this;
	if (window.event) elm = event.srcElement;
	
	if (ow_navActiveSub != "") {
		document.getElementById(ow_navActiveSub).style.display = "none";
		ow_NavFixSelects();
	}
	var subm = document.getElementById((elm.id).replace("ow_mainNav_", "ow_mainNav_sub"))
	
	if (subm) {
		if (subm.getElementsByTagName("li").length > 0) {
	
			subm.style.left = ow_NavMoveX(subm.id) + "px";
			subm.style.top = ow_NavMoveY(subm.id) + "px";
			subm.style.display = "block";
			
			// hide and select elements that may be under this submenu
			if (document.all) {
				var selects = document.getElementsByTagName("select");
				for (var i = 0; i < selects.length; i++) {
					if (ow_NavOverlap(subm, selects[i])) { selects[i].style.visibility = "hidden"; }
				}
			}
			
			// re-hide this submenu if embedded video is underneath (fails in Opera)
			if (!document.all) {
				var flash = document.getElementsByTagName("embed");
				for (var i = 0; i < flash.length; i++) {
					if (ow_NavOverlap(subm, flash[i])) {
						subm.style.display = "none";
						break;
					}
				}
			}

			if (subm.style.display != "none") {
				ow_f_AddEvent(subm, "mouseover", ow_NavAlive, false);
				ow_f_AddEvent(subm, "mouseout", ow_NavTryHideSub, false);
				ow_navActiveSub = subm.id;
			} else {
				ow_navActiveSub = "";
			}
		}
	} else {
		ow_navActiveSub = "";
	}
	
	ow_navCloseSub = false;
}

// --------------------------------------------------------------------------------
// ow_NavTryHideSub()
// Trigger the death of a submenu.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavTryHideSub() {
	ow_navCloseSub = true;
	setTimeout("ow_NavHideSub('" + ow_navActiveSub + "')", 550);
}

// --------------------------------------------------------------------------------
// ow_NavHideSub()
// Hide the current submenu.
// --------------------------------------------------------------------------------
// Arguments:
//	- subid - id of the submenu to be hidden [string]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavHideSub(subid) {
	var elm = this;
	if (window.event) elm = event.srcElement;
	if (ow_navCloseSub && document.getElementById(subid)) {
		document.getElementById(subid).style.display = "none";
		ow_NavFixSelects();
	}
}

// --------------------------------------------------------------------------------
// ow_NavAlive()
// Keep the current submenu alive.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavAlive() { ow_navCloseSub = false; }

// --------------------------------------------------------------------------------
// ow_NavInit()
// Initialize the dropdown menus by wiring up the mouse events and sizing items.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_NavInit() {
	if (document.getElementById) {
		ow_navigation = document.getElementById("ow_mainNav")
		if (ow_navigation) {
			var items = ow_navigation.getElementsByTagName("a")

			// clean up the "last" menu item
			//if (items.length > 0) {
			//	items[items.length - 1].style.borderRightWidth = "0";
			//	items[items.length - 1].style.marginRight = "0";
			//}

			// evenly distribute menu items
			if (ow_navDistribute) {
				var widest = -1;
				var maxwidth = 0;
				var mmwidth = parseInt(ow_navigation.offsetWidth, 10);
				var mminitial = 0;
				var mmused = 0;

				// determine some required numbers
				for (var i = 0; i < items.length; i++) {
					mminitial += parseInt(items[i].offsetWidth, 10);
					if (parseInt(items[i].offsetWidth, 10) > maxwidth) {
						maxwidth = parseInt(items[i].offsetWidth, 10);
						widest = i;
					}
				}
				
				// scale up each menu item's width
				for (var i = 0; i < items.length; i++) {
					var boxmodel = 0;
					var perc = parseInt(items[i].offsetWidth, 10) / mminitial;
					
					// adjust for the box model (borders and padding)
					boxmodel += parseInt((ow_f_GetElementStyle(items[i], "padding-left")).replace("px", ""), 10);
					boxmodel += parseInt((ow_f_GetElementStyle(items[i], "padding-right")).replace("px", ""), 10);
					if (ow_f_GetElementStyle(items[i], "border-right-width").indexOf("px") != -1) boxmodel += parseInt((ow_f_GetElementStyle(items[i], "border-right-width")).replace("px", ""), 10);
					if (ow_f_GetElementStyle(items[i], "border-left-width").indexOf("px") != -1) boxmodel += parseInt((ow_f_GetElementStyle(items[i], "border-left-width")).replace("px", ""), 10);
					
					items[i].style.width = Math.floor(perc * mmwidth) - boxmodel + "px";

					// older version of IE, correct for the box model
					if (document.all && document.expando && parseInt((ow_f_GetElementStyle(items[i], "width")).replace("px", ""), 10) == items[i].offsetWidth) {
						items[i].style.width = Math.floor(perc * mmwidth) + "px";
					}
					
					mmused += Math.floor(perc * mmwidth)
				}
			}
		
			// attach mouse events
			for (var i = 0; i < items.length; i++) {
				var subm = document.getElementById(items[i].id);
				ow_f_AddEvent(subm, "mouseover", ow_NavShowSub, false);
				ow_f_AddEvent(subm, "mouseout", ow_NavTryHideSub, false);

			}
			
		}
	}
}

var DDNav;

function DDNavigation(NodeTag) {
	
	this.NodeTag = NodeTag.split(",");
	this.NodeClass = "menu";
	this.Opened = "";
	this.Closing = true;
	this.Selects;
	
	this.GetElementsByClass = function(cls, tag) {
		var arr = new Array();
		var pattern = "(^" + cls + "$|\\s" + cls + "\\s|\\s" + cls + "\\s|^" + cls + "\\s|\\s" + cls + "$)";
		var j = 0;
		var i;
		var elm = document.getElementsByTagName(tag.toLowerCase());
		for (i = 0; i < elm.length; i++) {
			var rex = new RegExp(pattern, "gi");
			if (rex.test(elm[i].className)) arr[arr.length] = elm[i];
		}
		return arr;
	}
	
	this.SetEvent = function(elm, ev, fn) {
		if (elm.addEventListener) elm.addEventListener(ev, fn, false);
		else if (elm.attachEvent) elm.attachEvent("on" + ev, fn);
		else elm.setAttribute("on" + ev, fn);
	}
	
	this.GetStyle = function(elm, prop) {
		if (window.getComputedStyle) {
			return window.getComputedStyle(elm, null).getPropertyValue(prop);
		} else if (elm.currentStyle) {
			var ieProp = "";
			if (prop.indexOf("-") != -1) {
				for (var i = 0; i < prop.length; i++) {
					if (prop.charAt(i) == "-") {
						i++;
						ieProp += prop.charAt(i).toUpperCase();
					} else {
						ieProp += prop.charAt(i);
					}
				}
			} else {
				ieProp = prop;
			}
			return eval("elm.currentStyle." + ieProp);
		}
	}
	
	this.GetOffset = function(elm, dir) {
		var n = 0;
		var item = eval("elm");
		var off = "offsetTop";
		var padding = "padding-top";
		if (dir == "x") {
			off = "offsetLeft";
			padding = "padding-left";
		}
		do {
			n += eval("item." + off);
			item = eval("item.offsetParent");
		} while (item != null);
		return n;
	}
	
	this.PositionY = function(elm) { var y = DDNav.GetOffset(elm, "y"); y += elm.offsetHeight; return y; }

	this.PositionX = function(elm) { var x = DDNav.GetOffset(elm, "x"); return x; }
	
	this.Overlapping = function(a, b) {
		var ax = DDNav.GetOffset(a, "x");
		var ay = DDNav.GetOffset(a, "y");
		var aw = a.offsetWidth;
		var ah = a.offsetHeight;
		var bx = DDNav.GetOffset(b, "x");
		var by = DDNav.GetOffset(b, "y");
		var bw = b.offsetWidth;
		var bh = b.offsetHeight;
    if (((ax + aw) < bx) || (ax > (bx + bw)) || ((ay + ah) < by) || (ay > (by + bh))) return false;
    else return true;
	}
	
	this.CheckTag = function(t) {
		var found = false;
		for (var i = DDNav.NodeTag.length - 1; i >= 0; i--) {
			if (t.toLowerCase() == DDNav.NodeTag[i].toLowerCase()) {
				found = true;
				break;
			}
		}
		return found;
	}
	
	this.FixSelects = function() { if (document.all) for (var i = DDNav.Selects.length - 1; i >= 0; i--) if (DDNav.Selects[i].style.visibility == "hidden") DDNav.Selects[i].style.visibility = "visible"; }
	
	this.HuntMenu = function() { DDNav.Closing = true; setTimeout("DDNav.KillMenu('" + DDNav.Opened + "')", 550); }
	
	this.KillMenu = function(m) { if (DDNav.Closing && document.getElementById(m)) DDNav.CloseMenu(m); }
	
	this.CloseMenu = function(m) {
		var n = (m).replace("_menu", "");
		var menu = document.getElementById(m);
		var node = document.getElementById(n);
		node.className = (node.className).replace(" " + DDNav.NodeClass + "_on", "");
		menu.style.display = "none";
		DDNav.FixSelects();
	}
	
	this.MaintainMenu = function() { DDNav.Closing = false; }

	this.ShowMenu = function(e) {
		if (!e) var e = window.event;
		if (e.target) var elm = e.target;
		else var elm = e.srcElement;
		if (elm.nodeType == 3) elm = elm.parentNode; // defeat Safari bug
		while (!DDNav.CheckTag(elm.tagName)) elm = elm.parentNode;
		if (DDNav.Opened != "") DDNav.CloseMenu(DDNav.Opened);
		var menu = document.getElementById(elm.id + "_menu");
		if (menu) {
			if (menu.childNodes.length > 0) {
				elm.className = (elm.className).replace(" " + DDNav.NodeClass + "_on", "");
				elm.className = elm.className + " " + DDNav.NodeClass + "_on";
				var x = DDNav.PositionX(elm);
				var y = DDNav.PositionY(elm);
				menu.style.left = x + "px"
				menu.style.top = y + "px";
				menu.style.display = "block";
				if (document.all) for (var i = DDNav.Selects.length - 1; i >= 0; i--)	if (DDNav.Overlapping(menu, DDNav.Selects[i])) DDNav.Selects[i].style.visibility = "hidden";
				if (DDNav.GetStyle(menu, "display") != "none") {
					DDNav.SetEvent(menu, "mouseover", DDNav.MaintainMenu);
					DDNav.SetEvent(menu, "mouseout", DDNav.HuntMenu);
					DDNav.Opened = menu.id;
				} else DDNav.Opened = "";
			}
		} else DDNav.Opened = "";
		DDNav.Closing = false;
	}
	
	// INITIALIZATION.	
	
	if (document.getElementById) {
		var ieMac = (document.all && navigator.userAgent.indexOf("Mac") != -1);
		if (!ieMac) {
			var node = new Array();
			for (var i = this.NodeTag.length - 1; i >= 0; i--) {
				var arr = this.GetElementsByClass(this.NodeClass, this.NodeTag[i]);
				for (var j = arr.length - 1; j >= 0; j--) node.push(arr[j]);
			}
			for (var i = node.length - 1; i >= 0; i--) {
				this.SetEvent(node[i], "mouseover", this.ShowMenu);
				this.SetEvent(node[i], "mouseout", this.HuntMenu);
			}
			if (document.all) this.Selects = document.getElementsByTagName("select");
		}
	}
}

/*
-------------------------------------------------------------------
Initialize dropdown navigation when the page loads.
Adapt as needed depending on the DOM-loading technique of choice!!!
-------------------------------------------------------------------
*/

/*var OnLoad = window.onload;
if (typeof window.onload != "function") window.onload = StartDDNavigation;
else window.onload = function() { OnLoad(); StartDDNavigation(); };*/

ow_f_AppendLoadEvent(
	function StartDDNavigation() { DDNav = new DDNavigation("a"); }
);


