/**
	Author: Gregory Heeren
	Thanks to: David Geilfus
	Automatically creates tabs from each specified element. All elements between the specified elements are considered as being tab content.
	There is no need for any extra HTML code to make this work
	Requires: JQuery 1.4 (for the nextUntil function)
	*/
	
(function($) {

	$.fn.extend({
	
		autoTabs: function(config) {
		
			var defaults = {
				tabSelector: 'h2' // selector to match the tabs, everything between these selectors is considered tab content
			}
			
			var options = $.extend(defaults, config);
			
			return this.each(function() {
			
				// create the UL for the tabs navigation
				var $nav = $('<ul />').insertBefore($(this).find(options.tabSelector).eq(0)).attr('class', 'tabnav');
				
				$(this).find(options.tabSelector).each(function(i) {
					// add an item to the tabs navigation
					$nav.append('<li><a href="#" title="' + $(this).text() + '">' + $(this).text() + '</a></li>');
					
					// hide the h2
					$(this).hide();

					// add a wrapper around the tab contents so we can more easily find them later
					var $tabContent = $(this).nextUntil(options.tabSelector);
					$tabContent.wrapAll($('<div class="tab" />'));
					
					// hide all content except first (we hide the wrapping div)
					if (i > 0) { $tabContent.parent().hide(); }
					
				});
				
				$nav.find('a').click(function() {
					var currentId = $nav.find('li').index($(this).parent('li'));
					$(this).parent('li').addClass('current').siblings('li').removeClass('current');
					$('div.tab').eq(currentId).show().siblings('div.tab').hide();
					return false;
				});
				
				// highlight first tab nav
				$nav.find('li:first').addClass("current");

			});
			
		}
		
	});
	
})(jQuery);
