$(document).ready( function() { bind(); init(); } );

function bind()
{
	// bind to tabs
	$(".tabs a").click( 
		function() 
		{ 
			selectTab(this);  
			return false; 
		} 
	);
	
	// bind to any content links which change tab
	$("a.tablink").click( 
		function() 
		{ 
			selectTabLink(this);  
			return false; 
		} 
	);	
}

function init()
{
	// see if any anchor is specified in URL and switch to that tab if possible
	if( location && location.hash )
	{
		var tab = $("div.tabs/ul/li/a[@href="+location.hash+"]")[0];
		if( tab )
		{
			selectTab( tab );
		}	
	}
	else
	{
		var tab = $("div.tabs/ul/li/a")[0];
		if( tab )
		{
			selectTab( tab );
		}		
	}
}

function selectTab( el )
{
	var cls = $(el).attr( "href" ).substring(1);
	
	$(el).parents(".tabbedpanel")
		 .find( ".tabs li" )
		 .removeClass( "currenttab" );
		 
	$(el).parent()
		 .addClass("currenttab");
	
	$(el).parents(".tabbedpanel")
		 .find( ".tabcontent" )
		 .removeClass( "currenttabcontent" );
		 
	$(el).parents(".tabbedpanel")
		 .find( "." + cls )
		 .addClass( "currenttabcontent" );
}

function selectTabLink( el )
{
	var cls = $(el).attr( "href" ).substring(1);
	selectTabById( cls );
}

function selectTabById( id )
{
	selectTab( $("div.tabs/ul/li/a[@href=#"+id+"]")[0] );
}
