var firstTab = 0; 

function openTab(clickedTab) {
	var thisTab = jQuery(".tabbed-box .tabs a").index(clickedTab);
	var thisParentId = '#'+jQuery(".tabbed-box .tabs li:eq("+thisTab+")").parents("div:first").attr("id");

jQuery(".tabbed-box"+thisParentId+" .tabs li").removeClass("active");
	jQuery(".tabbed-box .tabs li:eq("+thisTab+")").addClass("active");
	
	
	jQuery(".tabbed-box"+thisParentId+" .tabbed-content:not(:eq("+thisTab+"))").hide();
	jQuery(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
	currentTab = thisTab;
}

function findTallest(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = jQuery(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	return tallest;
}

function equalHeight(group) {
	tallest = findTallest(group);
	group.height(tallest);
}

function centerText(group) {
	tallest = findTallest(group);
	group.each(function() {
		difference = (tallest - jQuery(this).height());
		jQuery(this).css("padding-top", (difference/2)+"px");
	});
}

jQuery(document).ready(function() {
								
	// This function resizes the tabs based on the amount of space available.
	jQuery('.tabbed-box').each(function(a) {
		box = jQuery('.tabbed-box:eq('+a+')');
		boxWidth = jQuery(box).innerWidth();
		numTabs = parseInt(jQuery(box).attr('id').substr(6));
		tabWidth = (boxWidth/numTabs).toFixed(0);
		borderPixels = boxWidth % numTabs;
		addPixels = false;
		// Transfer pixels from the tabs to the border until we have enough.
		if(borderPixels < (numTabs - 1)) {
			tabWidth--;
			borderPixels+=numTabs;
			addPixels = true;
		}
		jQuery('.tabbed-box:eq('+a+') .tabs li').each(function(b) {
			if((numTabs - 1) != b || (numTabs-1 == b && !addPixels)) {
				jQuery(this).width(tabWidth);
			} else {
				jQuery(this).width(tabWidth+borderPixels-(numTabs-1))
			}
		});
		
	});

	jQuery(".tabs li:eq(0)").addClass("first-child");

	jQuery(".tabbed-box .tabs li a").click(function() {
		openTab(jQuery(this)); return false;
	});

	jQuery('.tabbed-box').each(function(a) {
		jQuery(".tabbed-box:eq("+a+") .tabs li a:eq("+firstTab+")").click()
	});
	
	equalHeight(jQuery('.tabs li'));
	centerText(jQuery('.tabs li a'));
});