function alertTest(alertTestMessage) {
// Use this to activate or deactivate the alert messages
//	alert(alertTestMessage);
}

function loadPanel(panelID,templateFile) {
// Load the first panel in the section via AJAX. This function can be called with or without
// a URL as the templateFile. If there is no URL, it will look to the <A href> to 
// get the link to be loaded.

// If the function was passed a specific URL, then use it
	if ((panelID) && (templateFile)) {

// Control the loading message and graphic
		var thisTabLink;
		var thisTab;
		var allTabs = $('#' + panelID + ' div ul.paneltabrow li');

// loop through each of the links 
	   $(allTabs).find('a').each(function (){ 
	      // locate the tab / body paired match via the href stored in the tab and passed to the function
alertTest("LOOP ALL (this).attr('href') == " + $(this).attr('href'));
			if ($(this).attr('href') == templateFile) {
alertTest("href matches");
				thisTabLink = $(this);
				thisTab = $(this).parents('li');
				return false; // Exit loop
			}
	   }); 
alertTest("Exit loop");

		$(allTabs).find('a.loadingTabText').html('');
		$(thisTabLink).addClass('originalTabText'); // Set all tabs to the originalTabText class
		$(thisTabLink).hide(); // Hide the current tab link
//		$(thisTab).append('<a class="loadingTabText"><em>Loading...</em></a>');
//			$(thisTab).find('a.loadingTabText').addClass('ui-tabs-loading');
		$('<a class="loadingTabText ui-tabs-loading"><em>Loading...</em></a>').insertAfter($(thisTabLink)); // This method auto loads the class in the DOM

		
	// jQuery AJAX load tabbed panel bodies
		var thisPanelTabBody = $(("#" + panelID + "_tab_body span." + panelID));
// Handle the Filename / URL
		if (templateFile.charAt(0) == '#') {
		// In case there is a # in front of URL
		// Can use this to add inline vs. AJAX sniffer
//			templateFile = templateFile.substring(1);
		}

// Load the file into the tab_body div
// Then, turn off loading graphics and text
		thisPanelTabBody.load(templateFile, function(){
			$(thisTab).find('a.loadingTabText').remove();
			$(thisTabLink).show();
		});
	}
}

function isDiv(e) { return e && e.tagName.toLowerCase() == "div";  }

function loadAllPanels() {
// If the panel has been pre loaded via the CMS, leave it alone
	if ($('#multimedia_tab_body span.multimedia').html() == "") {
		// Call this to load the first tab's destination from each panel
		//    alertTest($('body').find('div.panelarea').each(loadAllPanelsRoutine).size());
		    $('body').find('div.panelarea').each(loadAllPanelsRoutine);
	}
} //end loadAllPanels

function loadAllPanelsRoutine() {
// Determine the destination using the first tab in the panel
    var e = this;

		var panelID = $(e).attr('id');
		var templateFile = $('#' + panelID + ' ul.paneltabrow li a').slice(0,1).attr('href');
		loadPanel(panelID,templateFile);

/*
    while(!isDiv(e) && e.parentNode) e = e.parentNode;
    if (isDiv(e)) {
// This will return the URL of the first tab's link in each panel
//		alertTest("This panelID = " + $(this).attr('id'));
//		alertTest("href = " + $('#' + panelID + ' ul.paneltabrow li a').eq(0).attr('href'));
		var panelID = $(e).attr('id');
		var templateFile = $('#' + panelID + ' ul.paneltabrow li a').slice(0,1).attr('href');
//		$(e).css({ border: "2px solid red"}); // To see affected areas for testing
		loadPanel(panelID,templateFile);
	}
*/
}


function showAllSubPanels(panelID) {

// Call this to load the first tab's destination from each subpanel
// Call this from each subpanel page as an onload event

// Find each subpanel in the current panel and act on it.
    $('body').find('div#' + panelID + ' div.subpanel').each(showFirstSubpanelTabContent);

}


function showFirstSubpanelTabContent(){
// Show the subpanel's first tab body content area
	alertTest('RUNNING showFirstSubpanelTabContent()');
    var e = this;
	var thisSubPanelTabBodyContent = $(e).find('.subpanel_body_content');
	showSubpanelBody(thisSubPanelTabBodyContent.slice(0,1)); // Show the subpanel and pass it the first subpanel_body_content element
}

function showSubpanelClickThis(thisSubpanelTabLink) {
// jQuery show hide tabbed SUBpanel bodies - Uses the THIS method to get passed arguments
	alertTest('showSubpanelClickThis() has been passed the following element (This is the ID): ' +  $(thisSubpanelTabLink).attr('id'));

/////////////////////
// FIND THE SIBLINGS OF THE CLICKED TAB
// FIND OUT THE INDEX OF THE CLICKED TAB WITHIN ITS SIBLINGS
// PASS THE INDEX OF THE CLICKED TAB THROUGH TO USE THAT AS THE INDEX OF THE TAB BODY
////////////////////
	var subpanelTabLinksAll = $(thisSubpanelTabLink).parents('ul.subtabrow').find('a');
	var thisSubpanelTabLinkIndex = subpanelTabLinksAll.index(thisSubpanelTabLink);
	var subpanelTabBodiesAll = $(thisSubpanelTabLink).parents('.subpanel').find('.subpanel_body_content');
	var thisSubPanelTabBodyContent = subpanelTabBodiesAll.slice(thisSubpanelTabLinkIndex,thisSubpanelTabLinkIndex+1);

	alertTest("RUNNING: showSubpanelClickThis(thisSubPanelTabContent) param = " + $(thisSubPanelTabBodyContent).attr('id'));
	
	showSubpanelBody(thisSubPanelTabBodyContent);
}

function showSubpanelBody(thisSubPanelTabBodyContent) {
// Show / Hide the correct subpanel body content divs

// Hide sibling div.subpanel_body_content 
		alertTest("RUNNING: showSubpanelBody(thisSubPanelTabBodyContent) param = " + $(thisSubPanelTabBodyContent).attr('id'));
		var subpanelTabBodyContentAll = $(thisSubPanelTabBodyContent).parents('div.subpanel_body').find('div.subpanel_body_content');
	
	// Hide all the subpanel content
		$(subpanelTabBodyContentAll).hide();
		$(subpanelTabBodyContentAll).css({
			'visibility' : 'hidden',
			'position' : 'absolute'
		});
	// show the current div.subpanel_body_content
		$(thisSubPanelTabBodyContent).show();
		$(thisSubPanelTabBodyContent).css({
			'visibility' : 'visible',
			'position' : 'relative'
		});
}



$(document).ready(function() {
// AJAX call to load all default panels
	loadAllPanels();

	// Load the file on click. Determine URL or destination via the clicked A href
	$('ul.paneltabrow li a').click(function(){
		var thisPanelID = $(this).parents('div.panelarea').attr('id');
		var templateFile = $(this).attr('href');
	// Pass the load function a panelID
		loadPanel(thisPanelID,templateFile);
	});
});


