var loaded = false;
var taxTree;

// Method used to initialize with multiple tabs/capabilities that
// can be toggled.  This function looks for anchors/links with
// IDs prefixed with "tab_ctrl_" and assigns an "onClick" function
// that shows the taxonomy associated with the ID and hides 
// whatever is currently in the div with ID "application_tab_area"
function initHomeTabs() {
	// Get the Applications tab area
	var tabArea = $("application_tab_area");
	if (tabArea == null || tabArea == "undefined")
		alert('Could not find the Application tab area');
	else {
		// Create a basic element to insert whenever we need to add a tab pane.
		var rootTabPane=document.createElement("div");
		rootTabPane.className="tab_pane";
		rootTabPane.id='rootContentDiv';
		tabArea.appendChild(rootTabPane);
		rootTabPane.style.display="block";
	
		// Get tab Ids for all tab controls within the tab area and add onclick events to their <a>'s
		var selected = false;
		var allTabIds = new Array();
		allTabAs = tabArea.getElementsByTagName("a");
		for (i=0; i<allTabAs.length; i++) {
			if (typeof allTabAs[i].id != "undefined" && allTabAs[i].id != null) {
				
				if (allTabAs[i].id.indexOf("tab_ctrl_")==0) {
					
					// Get the id without the tab_ctrl prefix
					thisId=allTabAs[i].id.replace(/tab_ctrl_/, "");
					
					// Store the id in the tabs list.
					allTabIds[allTabIds.length]=thisId;
											
					allTabAs[i].onclick=function() {
						showHomePageTab(this, false);
						return false;
					}
				}
				
				// Determine from the current URL which tab is selected
				if (window.location.href.toString().indexOf(allTabAs[i]) > -1 && !loaded)
				{
					loadPriorState();
					selected = true;
					loaded = true;
				}
			}		
		}
		// if no tabs are selected default to the first one
		if (!selected)
		{
		    showHomePageTab(allTabAs[0], true);
		}
	}
}

// This method is used to initialize and load a single tab/capability
// given the ID of the area to populate and the tab/capability ID
function initAndLoadTab(tabAreaId, tabId, capability)
{
	// Get the Applications tab area
	var tabArea = $(tabAreaId);
	if (tabArea == null || tabArea == "undefined")
		alert('Could not find the Application tab area');
	else {
		// Create a basic element to insert whenever we need to add a tab pane.
		var rootTabPane=document.createElement("div");
		rootTabPane.className="tab_pane";
		rootTabPane.id='rootContentDiv';
		tabArea.appendChild(rootTabPane);
		rootTabPane.style.display="block";
		
		var taxArray = getTaxonomyPath().split("/");
		if (taxArray.length > 2)
		{
	    	taxTree = new TaxonomyTree(tabId, "rootContentDiv", "ajaxLoaderImg", capability, loadTreeNode, taxArray[0] + "/" + taxArray[1] + "/" + taxArray[2]);
		}
		else
		{
			taxTree = new TaxonomyTree(tabId, "rootContentDiv", "ajaxLoaderImg", capability);
		}
	}
}

// Method to fix back-button issue in Internet Explorer
var loadPriorState = function()
{	
	// verify browser is IE -- search state is preserved in Firefox
	// and this method will end up collapsing the taxonomy there
    if (navigator.appVersion.indexOf("MSIE") != -1)
    {
		var allTabAs = $$("div.tab_area ul.tab_controls li a");
		if (allTabAs == null || allTabAs == "undefined")
		{
	        alert('Could not find the Application tab area');
		}
	    else 
	    {
	    	var url = window.location.href.toString();
	    	
	    	// find the tab that needs to be opened and open it
	    	for (var i = 0; i < allTabAs.length; i++)
	    	{
	    		// Get the id without the tab_ctrl prefix
				thisId=allTabAs[i].id.replace(/tab_ctrl_/, "");
										
				allTabAs[i].onclick=function() 
				{
					showHomePageTab(this, false);
					return false;
				}
	    		if (url.indexOf(allTabAs[i]) > -1)
			    {
	    	        openTab(allTabAs[i]);
			    }
	    	}
	    }
    }
}

// This method opens a specific node in the taxonomy.  It is defined as a variable because it
// is is recursively passed to the taxonomyTree loadSubContents to be invoked only when the
// ancestor nodes have been completely loaded.
var loadTreeNode = function(path)
{
	var cur = path.split("/");
    var tot = getTaxonomyPath().split("/")
    
    if (cur.length < tot.length)
    {
        // recursively pass this method to load the sub-tree once this node has fully loaded
        taxTree.loadSubContents("taxonomyPanel_taxonomy:^" + path, loadTreeNode, path + "/" + tot[cur.length]);
    }
    else
    {
        taxTree.loadSubContents("taxonomyPanel_taxonomy:^" + path);
    }
}

// This method is used for opening a taxonomy tab.  This method is invoked only by a user
// clicking on a taxonomy tab
function showHomePageTab(linkRef, isDefault) {
    // rewrite the URL to include selected taxonomy (unless it's the default)
	if (!isDefault)
	{
	    window.location.href = linkRef;
	}
	
	openTab(linkRef);
}

// This method opens one of the taxonomy tabs.  It may be invoked either by the user clicking a
// tab or automatically when the page loads, given the user's prior search state
function openTab(linkRef)
{
	var tabId = linkRef.id.toString().replace(/tab_ctrl_/, "");

	//  First let the AJAX call run in the background
	var taxArray = getTaxonomyPath().split("/");
	if (taxArray.length > 2)
	{
	    taxTree = new TaxonomyTree(tabId, "rootContentDiv", "ajaxLoaderImg", "home", loadTreeNode, taxArray[0] + "/" + taxArray[1] + "/" + taxArray[2]);
	}
	else
	{
		taxTree = new TaxonomyTree(tabId, "rootContentDiv", "ajaxLoaderImg", "home");
	}
	
	var parentLI = linkRef.parentNode;
	var parentUL = parentLI.parentNode;
	var parentId = parentUL.parentNode;

	// Deselect previous tab and select new tab.
	allTabs=parentUL.getElementsByTagName("li");
	for (i=0; i<allTabs.length; i++) {
		allTabs[i].className="";
	}
	parentLI.className="current";
}

// This method determines the taxonomy path encoded in the URL
function getTaxonomyPath()
{
    var path = "";
    var currentUrl = window.location.href;
    var hashIndex = currentUrl.indexOf("#");
    if (hashIndex > 0 && currentUrl.length > hashIndex)
    {
        path = currentUrl.substring(currentUrl.indexOf("#") + 1);
    }
    return path;
}

