var gBanner;
var gTabs;
var gCurrTab = '';
var gCurrLink = '';
var gCurrInfo;  // lave this udnefined at init
var gTabClicked = '';
var gIEVersion = '';

function Startup()
{
	// Start the banner
	gBanner = new Banner();
	gBanner.Init();

	// Satrt the tabs
	gTabs = new Tabs();
	gTabs.Init();

	// Set the timing interval so that we can find the history
	setInterval( CheckForNewLocation, 500 );

	// Find the browser version
	//	We only care about IE 8. Test for MSIE x.x;
/*	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
		gIEVersion = new Number( RegExp.$1 ) // capture x.x portion and store as a number
*/	

	// Now click on the first tab's link
	//		We have to check, in case someone pasted in a link into our tabs system
	if( location.hash.length == 0 )
		location.hash = '#Tab-0_link-0';
}

function CheckForNewLocation( event )
{
	// If we haven't finished initing don't do this
	//		This only happens during debug
	if( gTabs.tabList.length != gTotalSidebars )			
		return;
	
	// Distinguish whether the tab was clicked or the hash was changed
	var tab = '';
	var link = '';
	var info = '';
	if( gTabClicked.length > 0 ) 
	{
		tab = gTabClicked;
		link = 'link-0';
	}
	else
	{
		// These are guaranteed to be filled
		var linkList = CheckLocationHash().split( '_' );
		tab = linkList[0];
		link = linkList[1];
		info = linkList[2];
	}
	
	// At this point info can be '' or have a value.
	//		At init, gCurrInfo is undefined, but follows the last info otherwise.
	if( (tab == gCurrTab) && (link == gCurrLink) && (info == gCurrInfo) )
	{
		// If everything is the same, don't do anything
		return;
	}
	
	// See if the current tab is different than either the gCurrTab or the tab that has just been clicked
	if( tab != gCurrTab )
	{
		// Avoid an infinite loop. ?we don't want to clikc the tab if we originated there. 
		if( gTabClicked.length == 0 )
			// The tab was not clicked, so we have to click it here
			LoadTab( tab );
		
		gCurrTab = tab;
		
		// Clear so we don't have an infinite loop
		gTabClicked = '';
	}
	
	// Set this in place so that LoadLink can use it down deep.
	gCurrInfo = info;
	
	// The link now needs to be changed
	LoadLink( link );
	gCurrLink = link;
	
}

function CheckLocationHash()
{
	// This is in the form #Tab-x_link-y_info=z'
	var newLink = location.hash;
	if( newLink.length == 0 )
		return 'Tab-0_link-0_';
	
	// Make an array linkList = { #Tab-x, link-y, info=z }
	var linkList = newLink.split( '_' );
	
	// Split off the # sign in the first member of the link array, link[0]
	//		The second member of this new array, [1], is the Tab-x
	var tab = linkList[0].split( '#' )[1];
	if( tab == undefined || tab.length == 0 )
		return 'Tab-0_link-0_';
	
	// gCurrLink is in the form 'link-y'.
	//		LoadLink expects the form 'link-y', so strip off the '#'
	//		LoadLink assumes that LaodTab has been called
	var link = linkList[1];
	if( link == undefined || link.length == 0 )
		return 'Tab-0_link-0_';
	
	// Any additional info?
	var info = linkList[2];
	if( info != undefined && info.length > 0 )
	{
		// There is something in info
		// See if it does not have the required '=' sign followed by info
		info = info.split('=');
	
		// If info is has more than two parts, it is not real info; it is garbage.
		//		Make it an empty string
		info = (info.length == 2) ? info[1] : '';
	}
	
	// Make sure info is not undefined
	info = (info == undefined) ? '' : info;
	return (tab + '_' + link + '_' + info);
}
