var gTotalSidebars = 0;
function TabPanel( id )
{
	// This has the form 'Tab-x'
	this.id = id;
	this.xmlRequest = null;
	this.xmlDoc = null;
	
	this.htmlRequest = null;
	this.htmlDoc = null;
	
	this.sidebar = null;
}

TabPanel.prototype.Init = function()
{
	this.LoadStructure();

	// Request the panel sidebar
	//		This loads the banner for the panel, and the sidebar items
	var path = '../content/' + this.id + '_sidebar.xml';	
	this.xmlRequest = new XMLDocRequest( this, 'php/GetXMLFromFile.php?path=' + path, this.FillSidebarWithXML );
}
	
TabPanel.prototype.LoadStructure = function()
{
	// Load the divs
	text = '<div class="panel_sidebar" id="sidebar_' + this.id + '"></div><div class="panel_main" id="main_' + this.id + '"></div>';

	$("#" + this.id ).html( text );	
}

// This is the callback for the sidebar xmlRequest
//		the 'this' here is really the XMLHttpRequest'
TabPanel.prototype.FillSidebarWithXML = function()
{
	var xmlDoc = this.request.responseXML;
	if( xmlDoc == null )
		return;
	
	errors = xmlDoc.getElementsByTagName( "errorcode" );	
	if( errors[0] != null && errors[0].childNodes[0] != null && errors[0].childNodes[0].nodeValue == 0 )
	{
		this.caller.xmlDoc = xmlDoc;
		this.caller.FillSidebar();
	}
}

TabPanel.prototype.FillSidebar = function()
{
	try
	{
		var sidebarXML = this.xmlDoc.getElementsByTagName( 'sidebar' )[0];
		this.sidebar = new Sidebar( this.id );
		if( this.id == 'Tab-3' )
			stop = 2;
			
		this.sidebar.LoadInto( 'sidebar_' + this.id, sidebarXML );
		gTotalSidebars++;
	} catch(e)
	{}	
	
	if( gTabs.tabList.length == gTotalSidebars )
	{
		// initialize all scrollables 
		$("div.scrollable").scrollable({ vertical:true, size: 4 });
	}
}

TabPanel.prototype.LoadBanner = function( linkCtr )
{
	this.sidebar.LoadBanner( linkCtr );
}

// When a tab is clicked, this method is eventually called to load the html into this tabPanel's main content page
//   The html is already existent if it has been called before. If not, then it has to be fetched
//		'linkCtr' is the counter into the link list
TabPanel.prototype.LoadContent = function( linkCtr )
{
	// the sidebar will send back the content if it can
	var theHtml = this.sidebar.GetContent( linkCtr );	
	if( theHtml == undefined || theHtml == null )
	{
		// Request the panel content
		var linkPath = this.sidebar.GetLinkPath( linkCtr );
		var link = this.sidebar.GetLink( linkCtr );
		if( linkPath != null )
			this.htmlRequest = new XMLDocRequest( this, 'php/GetHtmlFromFile.php?path=' + linkPath + '&link=' + link, this.FillMainWithHtml );

		return;
	}

	// Fill Main
	this.FillMain( theHtml );
}	

// This is the callback for the main xmlRequest
//		the 'this' here is really the XMLHttpRequest
TabPanel.prototype.FillMainWithHtml = function()
{
	var xmlDoc = this.request.responseXML;
	if( xmlDoc == null )
		return;
	
	errors = xmlDoc.getElementsByTagName( "errorcode" );	
	if( errors[0] != null && errors[0].childNodes[0] != null && errors[0].childNodes[0].nodeValue == 0 )
	{
		// Put the xml into place
		this.caller.htmlDoc = xmlDoc;

		// First get the path of the html file; it has the tab id in it
		var linkPath = xmlDoc.getElementsByTagName( 'linkPath' )[0].childNodes[0].nodeValue;
		var link = xmlDoc.getElementsByTagName( 'link' )[0].childNodes[0].nodeValue;

		// Now get the html for this pane
		var main = xmlDoc.getElementsByTagName( 'htmlArray' );
		var content = '';
		var len = main.length;
		for( var i=0; i<len; i++)
			content += main[i].childNodes[0].nodeValue;

		content = URLDecode( content );

		// Add the content to the saved html in the sidebar 
		this.caller.sidebar.AddPathHtml( linkPath, content );
		
		// Fill the main content area with the html
		this.caller.FillMain( content );
	}
}

TabPanel.prototype.FillMain = function( contentHtml )
{
	try
	{
		$( "#main_" + this.id).html( contentHtml );			

	} catch(e)
	{
		$( "#main_" + this.id).html( "The file content for " + this.id + " was expected to fill the main portion of its panel." );	
	}
	
	$("#main_" + this.id).fadeTo( "fast", 1 );

	// Let Google analtyics know
//alert( 'gPageTracker: ' + gPageTracker );
//	gPageTracker._trackPageview( '/Tab_Link_Click/' + path );
}

TabPanel.prototype.FindLink = function( linkPath )
{
	return this.sidebar.FindLink( linkPath );
}