// Defines the navigation the page should default to
// when a user is not using the navigation.  This should match
// the ID associated with the subnav UL element.  This variable
// should be overwritten in the head of each page.
var defaultNavID = "home";

// Length of time to delay the restoration of the nav
var delay = 500;

// Exceptions array defining special handling for certain urls
var specialNavURLs = [["/storeonline/default.aspx?orgId=13079", "shop"], ["?message", "member"], ["?schedule", "member"], ["?awards", "member"], ["?info", "member"],["storeonline/classSchedule.aspx?orgId=13079", "schedules" ]];

var activeNavID = null;
var timer = 0;

/**
 * Returns a reference to an object that has an id associated with it.
 *
 * @param	elmtID - String identifying the ID attribute for the element
 * @return	Object
 */
function toggleNav(navID) {
	clearTimeout(timer);

	// Toggle parent buttons
	if (activeNavID != null && navID != activeNavID) {
		imgOff(activeNavID + "B");
	}
	imgOn(navID + "B");

	// Toggle subnav
	if (activeNavID != null) {
		getElmtRef(activeNavID).style.display = "none";
	}
	getElmtRef(navID).style.display = "block";

	// Set the active navigation ID
	activeNavID = navID;
}

/**
 * Event handler for mousing off the subnav.
 * Stops the navigation system from returning to its default state.
 */
function holdNav() {
	clearTimeout(timer);
}

/**
 * Event handler for mousing off a parent button.
 * Sets a delayed command to restore the navigation to its
 * default state.
 */
function restoreNav() {
	timer = setTimeout(toggleNavToDefault, delay);
}

/**
 * Sets the navigation to its default state.  The default state is
 * defined by the defaultNavId.
 */
function toggleNavToDefault() {
	// Turn off the parent button and hide its menu
	if (activeNavID != null) {
		imgOff(activeNavID + "B");
		getElmtRef(activeNavID).style.display = "none";
	}

	// Turn on the default parent and display its menu
	imgOn(defaultNavID + "B");
	getElmtRef(defaultNavID).style.display = "block";

	// Set the active navigation ID
	activeNavID = defaultNavID;
}

/**
 * Intended to serve as the event handler for when the
 * navigation has loaded and the default nav button and
 * menu should be displayed.  This could be done explicitly
 * by calling the function immediately following the html
 * for the subnav menus.  It is currently done in the window
 * onload event handler, which might cause a delay.
 */
function onLoadNavHandler() {
	// Check for special handling of certain urls
	for (var i = 0; i < specialNavURLs.length; i++) {
		if (location.href.indexOf(specialNavURLs[i][0]) >= 0) {
			defaultNavID = specialNavURLs[i][1];
			break;
		}
	}

	// Set the navigation to its default state for the
	// current page.
	toggleNavToDefault();
}
window.onload = onLoadNavHandler;