$(function(){
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).hashchange( function(){
		var hash = location.hash;
		if ( $('#hash') ) { $('#hash').val( hash ); }
		if ( hash.split("&").length > 1 ) { // Check if hash contains more attributes
			var explode	= hash.split("&");
				hash	= explode[0];
			var subtab	= explode[1];
		}
		
		if ( hash == "" || hash == undefined || hash == null ) { hash = $('.menutabs ul li a').first().attr('href'); } // Get the first tab id if none selected
		if ( /#/i.test(hash) ) 	{ var id = hash.replace( /^#/, '#li-' ); } 
		else 					{ var id = $('.menutabs ul li').first().attr('href'); }

		if ( $('.extracontent') ) {
			if ( !subtab ) 		{ var subtab	= $('.subtabs ul li').first().attr('href'); }
		}

		// Iterate over all nav links, setting the "selected" class as-appropriate.
		$('.menutabs ul li').each(function(){
			if ( !$(id).hasClass("active") ) {
				var that = $(this);
				$(".menutabs ul li").removeClass("active"); //Remove any "active" class
				$(id).addClass("active"); //Add "active" class to selected tab
				$(".tab_content").hide(); //Hide all tab content
				if ( /#/i.test(hash) ) {
					var hash2 = hash;
					var tail = hash.substring(2);
					hash2 = '#'+hash2.charAt(1).toUpperCase() + tail;
					$(hash2).fadeIn(); //Fade in the active ID content
				}
			}
			if ( subtab ) {
				$(".subtabs li").removeClass("active"); //Remove any "active" class
				$('#subli-'+subtab).addClass("active"); // Add "active" class to selected subtab
				$('.extracontent').hide(); // Hide all subtabs
				var subtab2 = subtab;
				var tail2 = subtab.substring(1);
				subtab2 = '#'+subtab2.charAt(0).toUpperCase() + tail2;
				$(subtab2).fadeIn(); //Fade in the active ID content
			}
			return false;
		});
	  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).hashchange();
  
});

