// JavaScript Document

/*
Author: Alex Boyce
Copyright: 2009
Date: 2/27/10
Modified: 3/8/10

Creative Commons License
*/

(function($) {
	$.fn.nav = function(options) {
		var defaultSettings = {
			applyToTags: "li, a",
			activeClass: "active",
			homeID: "#home"
		},
		settings = $.extend({}, defaultSettings, options),
		arr_local = location.href.split("/"),
		page = arr_local[arr_local.length - 1],
		parent;	
		
		// Check so see if we're on the home page
		if (page == "" || page == null)
			$(settings.homeID).addClass(settings.activeClass);
		
		if (page.search("/?") > 0)
			page = page.substr(0, page.search("/?"));
		
		// Apply classes appropriately
		parent = this;

		var count = $("a[href="+page+"]", parent).addClass(settings.activeClass).parentsUntil("div").filter(settings.applyToTags).addClass(settings.activeClass).size();
		if (count == 0) {
			$("a", parent).each(function(i) {
				var href = $(this).attr("href");
				if (href.search("#") > 0) {
					if ((href != page) && (page.search("#") > 0))
						page = page.substr(0, page.search("#"));
					}
				$("a[href="+page+"]", parent).addClass(settings.activeClass).parentsUntil("div").filter(settings.applyToTags).addClass(settings.activeClass);
				});
			}
		
		// Most browsers will increase performance by not reloading pages with hash links if you're already on the page. If the page isn't reloaded, our nav function
		// can't be called. The following function checks to see if the link is an inactive hash link and then makes the clicked link active.
		$("a", this).click(function(event) {
			if ((!$(this).hasClass(settings.activeClass)) && ($(this).attr("href").search("#") > 0)) {
				$(settings.applyToTags, parent).removeClass(settings.activeClass);
				$(this).addClass(settings.activeClass).parentsUntil("div").filter(settings.applyToTags).addClass(settings.activeClass);
				}
			});
		
		return this;
		}
	})(jQuery);