// JavaScript Document

var currentTab = new String();

$(document).ready(function(){
	//	Attach listeners to tabs
	$('ul#tabs li a').each( function() {
		$(this).click( function(){
			switchTab(this);
			$(this).blur();
			//return false;
		});
	});
	
	//	Show the current DIV (ID'd by # in URL, or just the first tab)
	var r=  new RegExp('\\#([a-zA-Z0-9_\\-]+)');
	var res = r.exec(window.location.href);
	if( (res != null) && (res[1].length > 0) )//&& ($('ul#tabs #' + res[1]).length) )	// Test if a DIV exists
	{
		$('ul#tabs #' + res[1] + '-link').click();
	} else {
		$('ul#tabs li:first-child a').click();
	}
	
	// Hide all items marked for removal
	$('.tab-remove').addClass('hidden');
	
});


/**
 *	Set the active tab by showing the div and setting the active class to the tab
 *	@param		target		(string) The ID of the div of activate
 *	@returns	void
*/
function switchTab(obj)
{
	// Get the bookmark target, and the text to display
	target = getMark(obj);
	var tmp = target.split('-');
	var targetText = tmp[tmp.length-1];
	
	// Hide all sections
	$('.tab-section').hide();
	
	// Show the appripriate one
	$('div#' + target).show();
	
	// Remove current class from all 
	$('ul#tabs li').removeClass('current');
	
	// Add CURRENT class to the tab
	$('li#' + target + '-tab').addClass('current');
	
}



/**
 *	Get the bookmark portion of an anchor's href attribute (i.e. the part after the hash symbol ['#'])
 *	@param		obj		(obj) The object containing the url.
 *	@returns	string	The text after the hash symbol ('#')
*/
function getMark(obj)
{
	var url = new String();
	
	//	Extract the URL
	if( (obj.href) && (typeof(obj.href) == 'string') ) {
		url = obj.href;
	} else if( (obj.target) && (typeof(obj.target) == 'string') ) {
		url = obj.target;
	} else {
		return false;
	}
	
	//	Ensure there is a hash in the URL, otherwise return an empty string
	var hashAt = url.indexOf('#');
	if(hashAt == (-1)) {
		return '';
	}
	
	//	Return the page mark
	return url.substring(hashAt+1);
}
