(function ($) {

	// Plugin definition.
	$.fn.tabbedNav = function (options) {

		// Build options.
		var o = $.extend({}, $.fn.tabbedNav.defaults, options);

		// Iterate through and prepare each tabbed nav.
		return this.each(function () {

			// Get required elements.
			var $tabbedNav = $(this);
			var $tabs = $('.' + o.tabClass + '', $tabbedNav);
			var $panels = $('[class*="' + o.panelClassPrefix + '"]', $tabbedNav);

			// When a tab is clicked...
			$tabs.click(function (e) {

				// Prevent default click behaviour.
				e.preventDefault();

				// Determine which content panel it relates to, throwing an error if no indication is given.
				rel = $(this).attr('rel');
				if (rel === '') {
					throw new Error('tab requires a rel attribute indicating related panel');
				}

				// Hide previously selected tab and content.
				$tabs.removeClass(o.activeTabClass);
				$panels.removeClass(o.activePanelClass);

				// Show newly selected tab and content by adding relevant active tab class.
				$tabs.filter('[rel="' + rel + '"]').addClass(o.activeTabClass);
				$panels.filter('.' + o.panelClassPrefix + rel).addClass(o.activePanelClass);
			});
		});
	};

	// Plugin defaults.
	$.fn.tabbedNav.defaults = {
		activePanelClass:	'active',
		activeTabClass:		'active',
		panelClassPrefix:	'tabbedNavPanel',
		tabClass:					'tabbedNavTab'
	};
})(jQuery);