jQuery.fn.nTabs = function(options) {
	
	//tabs
	jQuery(this).each(function(){
							   
		var settings = jQuery.extend({
			tab : '.js-tab',
			content : '.js-tab-content',
			activeContentClass : 'js-active-content',
			activeTabClass : 'js-active-tab',
			uniformHeight : false,
			minHeight : 100
		},options||{});					   
			
		var currModuleNode = jQuery(this);
		var contentChildren = jQuery(this).find(settings.content + " *"); // finds all child nodes of tab-content nodes, to be removed from tab and content sets as they may contain nested tabs
		var firstContent = currModuleNode.find(settings.content).eq(0);	// gets first content node in source order - can safely be assumed to be the 'top level' in case there are nested tabs			
		var contentSiblings = firstContent.siblings(settings.content);

		//create an array of the top level content nodes in the correct source order
		var contentSetArray = [];
		contentSetArray.push(firstContent.get(0));
		contentSiblings.each(function(){
			contentSetArray.push(jQuery(this).get(0));
		});
		
		//create contentSet jquery object
		var contentSet = jQuery(contentSetArray);

		var getTabSet = function(){
			var tabSetExternal = currModuleNode.find(settings.tab).not(contentChildren);//find tabs that are outside content nodes
			var tabSetReturn;
			
			if (tabSetExternal.length > 0){
				tabSetReturn = tabSetExternal;
			} else {
				tabSetReturn = [];
				for (var i in contentSetArray){
					//for each top level content node, get only the first top level tab node
					tabSetReturn.push(contentSet.eq(i).find(settings.tab).get(0));					
				}
				tabSetReturn = jQuery(tabSetReturn);
			} 
			
			return tabSetReturn;
		}
		
		//create tabSet jQuery object
		var tabSet = getTabSet();		
		
		//auto add active classes to first tab and content els
		tabSet.eq(0).addClass(settings.activeTabClass);
		contentSet.eq(0).addClass(settings.activeContentClass);
		
		//set the height of all content items to the height of the tallest
		if (settings.uniformHeight == true){
			var getContentHeight = function(){                                                             
				var highNum = settings.minHeight || 0;
				contentSet.each(function(){
					if (jQuery(this).height() > highNum){					
						highNum = jQuery(this).height();
					}
				});
				
				return highNum;
			}

			contentSet.each(function(){
				jQuery(this).css("height", getContentHeight() + "px"); 
			});
		}
		
		tabSet.click(function(){
			//remove active classes
			tabSet.removeClass(settings.activeTabClass);
			contentSet.removeClass(settings.activeContentClass);
			
			//set index var
			var index = tabSet.index(this);
	
			//add active class to clicked el and corresponding content item
			contentSet.eq(index).addClass(settings.activeContentClass);
			jQuery(this).addClass(settings.activeTabClass);
			
			return false;
		});

	}); // end each loop
	
} // end plugin