( function( $ ) {
	
	$.fn.nav = function( options ) {

		//allow user to override options
		var opts = $.extend( true, {}, $.fn.nav.defaults, options );
		
		var $ul = $( this );
		var $submenus = $ul.find( "ul" ).hide();
		var $topLinks = $ul.find( "li div a" );

		/* handle primary nav */
	
		$topLinks.click(function() {

			//grab object refs to current link and its submenu

			var $topLink = $( this );
	  		var $submenu = $topLink.parents( "li" ).find( "ul" );

			//don't do anything if a previous menu operation is in progress
			if (
				!$ul.data( opts.dataAttr.pending ) &&
				!$submenu.hasClass( opts.classes.topSelected )
			) {
		
				//set a magic class to stop concurrent menu operations
				$ul.data( opts.dataAttr.pending, true );

				//callback function
				var showMenu = function() {
					
					//find the default menu item by class or by position
					//and show it

					var $initialItem = $submenu.find( "a." + opts.classes.initial );
					if ( $initialItem.length === 0 ) {
						$initialItem = $submenu.find( "a:first" );
					}
					
					$initialItem.click();

					//show the new menu

					$submenu
						.slideDown( opts.showTime, function() {
							//remove the magic class to allow further nav
			  				$ul.removeData( opts.dataAttr.pending );

						} )
					;

				};

				var $prevTopLink = $topLinks.filter( "." + opts.classes.selected );
				var $prevmenu = $submenus.filter( "." + opts.classes.topSelected );

				$topLink.addClass( opts.classes.selected );
				$submenu.addClass( opts.classes.topSelected );

				if ( $prevmenu.length > 0 ) {

					//if a previous menu was visible hide it, then show the new one
					$prevmenu.slideUp( opts.hideTime, showMenu );
					$prevmenu.removeClass( opts.classes.topSelected );
					$prevTopLink.removeClass( opts.classes.selected );

				}
				else {
					//otherwise just show the new one
					showMenu();
				}
			}
			else {
				//do nothing
			}
	  
			return false;

		} );

		/* handle secondary nav */
	
		//grab a reference to all the secondary nav links
		
		var $navLinks = $ul.find( "li ul li a" );
		
		$navLinks.click( function() {

			//make sure no other clicks are in progress

			if ( !$ul.hasClass( opts.classes.click ) ) {

				//make sure no futher clicks can collide with this

				$ul.addClass( opts.classes.click );

				//toggle the "selected" class so currently selected link doesn't look like a link

				var $previousLink = $navLinks
					.filter( "." + opts.classes.selected )
					.removeClass( opts.classes.selected )
				;
				var $currentLink = $( this ).addClass( opts.classes.selected );

				//hide all tabs but the newly selected one

				var $newTab = $( $currentLink.attr( "href") );

				$newTab
					.siblings( ".tab" )
					.fadeOut( opts.hideTime, function() {

						$newTab.fadeIn( opts.showTime, function() {

							//remove the magic class to allow further clicks
							$ul.removeClass( opts.classes.click );
						} );
					} )
				;

			}

			return false;

		} );
		
		//find the initial menu and simulate a click on it
		
		var $initialMenu = $ul.find( "li." + opts.classes.initial );
		if ( $initialMenu.length === 0 && opts.forceInitialShow ) {
			$initialMenu = $ul.find( "li:first" );
		}
		
		if ( $initialMenu.length >= 0 ) {
			window.setTimeout( function() {
				$initialMenu.find( "div a" ).click();
			}, opts.showTime );
		}
		

	};
	
	$.fn.nav.defaults = {

		//guarantees that even if no menus are given the magic "initial" class,
		//we will always show a content panel when we first render

		forceInitialShow: true,

		//millisecond values for timers 

		hideTime: 350,
		showTime: 500,
		
		//CSS classes for selectors

		classes: {
			selected: "selected",
			topSelected: "activeMenu",
			pending: "pending",
			initial: "default"
		},
		
		dataAttr: {
			pending: "clickPending"
		}

	};

} )( jQuery );
