/*****************************************************************
 *
 * $.fn.anchorNav()
 * by rebecca murphey
 * rmurphey gmail com
 * http://blog.rebeccamurphey.com/2007/12/24/anchor-based-url-navigation-jquery-plugin/
 *
 * Call this function on an element that contains 
 * "panels" which, in turn, contain anchors. The container
 * element should contain ONLY related panels, as this plugin
 * will show only one first-child element of the container at once.
 *
 * Options:
 *
 *   showFn: function to show the current panel
 *   hideFn: function to hide the current panel's siblings
 *   nav: selector for the page's navigation 
 *   currentNavFn: function to run on the link in the nav
 *               that is associated with the anchor
 *   anchorClass: class assigned to anchor tags; setting this will
 *                improve the speed on pages with lots of links
 *
 * See below for default option values. See the URL above for 
 * detailed documentation.
 * 
 * modifications by kev burns
 * kevburnsjr gmail com
 *
 ************************************************************************/

(function($) {
  $.fn.anchorNav = function(userOptions) {
    var options = {
      nav: null,
      panels: null,
      anchorClass: null,
      showFn: function() { $(this).show(); },
      hideFn: function() { $(this).hide(); },
      currentNavFn: function() { 
        $(this).parent().siblings().removeClass('selected'); 
        $(this).parent().addClass('selected'); 
      }
    };

	// Override options if appropriate
    if (userOptions) { $.extend(true,options,userOptions); }

	// Collect containers and panels
    var $container = $(this);
    if (options.anchorClass) {
      var $containerAnchors = 
        $container.
        find('a.' + options.anchorClass);
    } else {
      var $containerAnchors = 
        $container.
        find('a[name]');
    }
	$panels = $containerAnchors.parent();
	$navLinks = $(options.nav).find('a');
	
	// Set Default Nav Item and Panel
    var url = document.location.toString();
    if (url.match('#')) {
		var urlAnchor = url.split('#')[1];
		var $currentPanel = $containerAnchors.filter('[name="' + urlAnchor +'"]').parent();
		var $currentNav = $navLinks.filter('[href="#' + urlAnchor + '"]');
    } else { 
		var $currentPanel = $panels.eq(0);
		var $currentNav = $navLinks.eq(0);
	}
	
	// Activate default nav and panel
	$panels.each(options.hideFn);
	$currentPanel.each(options.showFn);
    $currentNav.each(options.currentNavFn);

	// Add callbacks to nav links
    if (options.nav) {
      $containerAnchors.each(function() {
        var $panel = $(this).parent();
        var name = $(this).attr('name');
        var $nav = $(options.nav);
        $nav.
          find('a[href="#' + name + '"]').
          click(function() {
            $panels.each(options.hideFn);
            $panel.each(options.showFn);
            $(this).each(options.currentNavFn);
	    // return false;
          });
      });
    }

    // prevent "jumping" when clicking on an anchor link
    $containerAnchors.remove();

    return $container;

  }

})(jQuery);
