// Sub-Navigation script
// Copyright 2007 Ken Dunnington

var nav = {
	name: "Navigation Functions",
	subMenus: [],
	currentElem: {},
	hideAll: function(except) {
		$(this.subMenus).not(except).each( function() {
			// Top Nav: $(this).parents("ul").length == 1
			// Submanu: $(this).parents("ul").length > 1
			// Has Submenus: $(this).children("li").children("ul").length > 0
			if ($(except).parents("ul").length == 1) {
				var visible = $(this).filter(":visible");
				visible.slideUp(nav.speed).removeClass("_open");
				$(this).siblings("a.click").text("+").add($(this).siblings("a")).removeClass("active");
			}
		});
	},
	toggle: function() {
		var mySub = $(this).siblings("ul")[0];
		
		// Hide all the other open menus
		nav.hideAll(mySub);
		$(mySub).toggle(nav.speed);
		$(this).toggleClass("active");
		$(this).text() == "+" ? $(this).text("-") : $(this).text("+");
		$(this).siblings("a").toggleClass("active");
		return false;
	},
	hoverOn: function(e) {
		// The containing LI
		var parentLi = $(this).parent("li")[0];
		// The submenu (unordered list)
		var mySub = $(this).siblings("ul")[0];
		// Hide any open menus
		nav.hideAll(mySub);
		// Set the container's class to mark it as open
		$(parentLi).addClass("_open");
		// Set the anchor tag's class to mark it as active
		$(this).addClass("active");
		// Show the submenu
		var config = {
			sensitivity: 3,
			interval: 100,
			over: null,
			out: nav.subHoverOff,
			timeout: 150
		};
		$(mySub).slideDown(nav.speed).hoverIntent(config);
		// $(mySub).hover(function() {},nav.subHoverOff);
		return false;
	},
	hoverOff: function(e) {
		var rel = e.relatedTarget;
		var parentLi = $(rel).parents("._open");
		var mySub = $(this).siblings("ul");
		if (parentLi.length == 0) {
			mySub.slideUp(nav.speed);
			$(this).removeClass("active");
		} else {
			// Otherwise, just remove the open class
			parentLi.removeClass("_open");
		}
	},
	subHoverOff: function(e) {
		var rel = e.relatedTarget;
		var sibAnchor = $(this).siblings(".hover")[0];
		var self = this;

		if (rel != sibAnchor) {
			$(self).parents("._open").removeClass("_open");
			$(self).slideUp(nav.speed);
		}
	}
};

$(document).ready(function() {
	nav.subMenus = $("a.click + ul").add("a.hover + ul");
	nav.subMenus.hide();

	var config = {
		sensitivity: 5,
		interval: 50,
		over: nav.hoverOn,
		out: nav.hoverOff,
		timeout: 50
	};
	$("a.click").click(nav.toggle).siblings("ul").hide();
	$("a.hover").hoverIntent(config);
	$("body").click(function(e) { 
		nav.subMenus.hide();
	});
	// Mac Firefox detection - turn off animations
	var browser = navigator.userAgent;
	var mac = browser.match(/Macintosh/);
	var ff = browser.match(/Firefox/);
	nav.speed = (mac && ff) ? 0 : 100;
});