/*jslint browser: true, eqeqeq: true, undef: true */
/*global $ */
/******************************************************************************
Lines above are for jslint, the JavaScript verifier.
http://www.jslint.com/
******************************************************************************/

if (!String.prototype.beginsWith) {
	String.prototype.beginsWith = function (prefix) {
		var result = ((prefix.length <= this.length) &&
			      (this.substring(0, prefix.length) === prefix));
		return result;
	};
}

var FriendForLife = {};

// given "/foo/bar/",         returns "/foo/bar/index.php"
// given "/foo/bar/blah.php", returns "/foo/bar/blah.php"
//
// Used for comparing page URLs modulo the "index.php" part.
FriendForLife.normalizeURL = function (url) {
	if (!/^\//.test(url)) {
		url = "/" + url;
	}
	if (/\/$/.test(url)) {
		url = url + "index.php";
	}
	return url;
};

// given "/foo/bar/" or "/foo/bar/blah.php", returns "/foo/bar/".
//
// Used for determining whether two URLs are in the same directory.
FriendForLife.directoryURL = function (url) {
	return url.replace(/[^\/]+$/, "");
};

FriendForLife.activateFlyouts = function () {
	$("#topNav table.navMenu td.navItem").each(function () {
		var flyout = $(this).find("ul.navMenu");
		if (flyout.length) {
			var hover = function () {
				flyout.show();
			};
			var unhover = function () {
				flyout.hide();
			};
			$(this).hover(hover, unhover);
		}
	});
};

FriendForLife.chooseSelectedTopNav = function () {
	var that = this;

	var page_href = this.normalizeURL(window.location.pathname);
	var selected;
	var $tds = $("#topNav table.navMenu td.navItem");

	// find a navItem with a link to the page the user is viewing.
	$tds.each(function () {
		var anchor = ($(this).children(".td_inner").
			      children("a").get(0));
		if (!anchor) {
			return true; // continues the loop
		}
		if (that.normalizeURL(anchor.pathname) === page_href) {
			selected = this;
			return false; // terminates the loop
		}
	});
	
	// if no such navItem is found, then given the user is viewing
	// /foo/bar/blah.php, find a link pointing to a page in
	// /foo/bar.
	if (!selected) {
		$tds.each(function () {
			var this_td = this;
			var anchor = ($(this).children(".td_inner").
				      children("a").get(0));
			if (!anchor) {
				return true; // continues the loop
			}
			if (that.directoryURL(page_href) === 
			    that.directoryURL(anchor.pathname)) {
				selected = this;
				return false; // terminates the loop
			}

			// also handle cases where page_href is
			// "/foo/bar/blah.php" and the navItem goes to
			// "/foo/".  This is a hack for stories in the
			// "/volunteers/stories/" category.
			if (that.directoryURL(anchor.pathname) !== "/" &&
			    page_href.beginsWith(that.directoryURL(anchor.pathname))) {
				selected = this;
				return false;
			}
		});
	}

	if (selected) {
		$(selected).addClass("selected");
	}
};

$(document).ready(function () {
	FriendForLife.activateFlyouts();
	FriendForLife.chooseSelectedTopNav();
});

