//	Script to hide and switch "tab" divs.
//	Based on this tutorial:
//	http://apricotstudios.wordpress.com/2008/08/29/jquery-tabs-tutorial/ 

function showTab(tab) {
	tabid = "#" + tab;
	// Remove active class from all links
	$('ul.tabnav li').removeClass('active'); 

	// Set the link button class to active
	var links = $('ul.tabnav li a');
	// Go through the links looking for the one that matches this id.
	// Can this be improved using jquery? I couldn't do it.
	for (var i=0; i<links.length; i++) {
		if ($(links[i]).attr('href') == tabid) {
			$(links[i]).parent().addClass('active');
		}
	}
	$('#tabs .tabdiv').hide(); 
	$(tabid).show();
}

$(document).ready(function(){
	// hide all divs
	$('#tabs .tabdiv').hide();
		
	// look to see if a specific tab has been linked to in the url as #foo
	var url = document.location.toString();
	if (url.match('#')) {
		// if #foo exists in url, show that tab
		var anchor = url.split('#')[1];
		showTab(anchor);
	}
    // if the page has set the js variable 'firstTab', then we will show that
    // tab (see donate.php for example)
    else if (typeof(firstTab) != "undefined" && firstTab != "")
    {
        showTab(firstTab);
    }
	else {
		// show first tab and set first link to active
		$('#tabs .tabdiv:first').show(); 
		$('ul.tabnav li:first').addClass('active'); 
	}

	// When any tabnav link is clicked, show that tab
	$('ul.tabnav li a').click(function(){ 
		// tab name should not include '#', although link href should
		var newTab = $(this).attr('href').split('#')[1]; 
		showTab(newTab);
	});
	
	// When a link in the text with class 'tablink' is clicked, show that tab
	$('a.tablink').click( function() {
		// tab name should not include '#', although link href should
		var newTab = $(this).attr('href').split('#')[1]; 
		showTab(newTab);
	});
});

