// JavaScript Document

/*
Author: Alex Boyce
Copyright: 2009
Date: 4/20/09

Creative Commons License
*/

(function($) {
	$.fn.nav = function(options) {
		var defaultSettings = {
			b_imageBack: true,
			backgroundDefaultY: "0",
			backgroundHoverY: ($("li", this).height() * -2) + "px",
			activeClass: "active",
			inactiveClass: "inactive",
			homeID: "#home"
		},
		settings = $.extend({}, defaultSettings, options),
		arr_local = location.href.split("/"),
		page = arr_local[arr_local.length - 1],
		parent;
		
		// Set all items inactive
		$("li", this).addClass(settings.inactiveClass);
		
		// Check so see if were on the home page
		if (page == "" || page == null)
			$(settings.homeID).removeClass(settings.inactiveClass).addClass(settings.activeClass);
		
		// Apply the background image setting is checked and apply classes appropriately
		if (settings.b_imageBack) {
			$(this).children("li[id]").each(function() {
				parent = "#" + $(this).attr("id");
				$("a", parent).each(function(i) {
					if ($(this).attr("href") == page) {
							$(parent).removeClass(settings.inactiveClass).addClass(settings.activeClass);
							$(this).removeClass(settings.inactiveClass).addClass(settings.activeClass);
					}
					});
				});
		}
		else {
			$(this).children("li").each(function() {
				$("a", this).each(function() {
					if ($(this).attr("href") == page)
							$(this).addClass(settings.activeClass);
					});
				});
		}
		
		// This fixes the IE id + class bug with hovering
		if (settings.b_imageBack) {
			$("li[class=inactive]", this).hover(function() {
					$(this).css({backgroundPositionY: settings.backgroundHoverY});
				}, function() {
					$(this).css({backgroundPositionY: settings.backgroundDefaultY});
				});
		}
		
		return this;
		}
	})(jQuery);