//include AFTER presentation.js
var relatedLinksHeaderIsExpandedKey = 'HeaderIsExpanded';
var loadingPlaceholderId = 'LoadingPlaceholder';
var relatedLinksSubHeaderKey = 'RelatedLinksSubHeader';
var relatedLinksSubHeaderIsExpandedKey = 'SubHeaderIsExpanded';

// fired by window.onLoad
function connectRelatedLinksEvents()
{
	var btns = $$(".tlrnz_expandRelatedLinks");
	btns.each(function(btn){btn.observe("click", expandRelatedLinks);});
}
document.observe("dom:loaded",connectRelatedLinksEvents);

// For expanding heading nodes of the related links component.
function expandRelatedLinks(evt)
{
	var button = $(this);
	if (!button.hasClassName("tlrnz_expandRelatedLinks"))
		button = $(this).up(".tlrnz_expandRelatedLinks");
	// For use in the callback.
	sib = button.next();
	var url = button.getElementsBySelector("a")[0].href + '&' + relatedLinksHeaderIsExpandedKey + '=' + (!button.hasClassName("expanded")).toString();

	// Does this button have a div element following it? (Is this the First Time?)
	if (!sib || (sib.tagName.toLowerCase() != 'div'))
	{
		// Only Perform this the First Time for this Data!
		// 'Loading' message
		var loadingTxt = "<div class='loadingPlaceholder'>Loading...</div>";
		if (sib)
		{
			sib.insert({before:loadingTxt});
		}
		else
		{
			button.up().insert(loadingTxt);
		}

		//make ajax request with specified callbacks
		new Ajax.Request(url,{method:"get",
							  onSuccess: expandRelatedLinksOutput,
							  onFailure: expandRelatedLinksError,
							  currentTarget: button});
	}
	expandRelatedWithoutSession(button); //Show/Hide on Screen.
}

// For the expansion of related link subheadings. Must store expansion state in session.
function expandRelatedLinksSubheading(btn, subheadingId)
{
	var button = $(btn);
	expandRelatedWithoutSession(button);
	var url = $(button).up().previous().getElementsBySelector("a")[0].href +
		'&' + relatedLinksSubHeaderKey + '=' + subheadingId +
		'&' + relatedLinksSubHeaderIsExpandedKey + '=' + (button.hasClassName("expanded")).toString();

	new Ajax.Request(url,{method:"get"});
}

// Show/Hide the "Related-Links Item" or "Related-Links Sub-Item".
function expandRelatedWithoutSession(activeItem)
{
	var activeEl = $(activeItem);
	if ( activeEl.hasClassName("expanded") ) 
	{
		activeEl.removeClassName("expanded");
		activeEl.next().setStyle({'display': 'none'});    // hide
	}
	else 
	{
		activeEl.addClassName("expanded");
		activeEl.next().setStyle({'display': 'block'});   // show
	}
}

// Outputs related link child text to the page.
function expandRelatedLinksError(transport)
{
	var activeEl = transport.request.options.currentTarget;
	alert("A problem was encountered retrieving the information. Please try again.");
	expandRelatedWithoutSession(activeEl);
	removeRelatedLinksLoadingPlaceholder(activeEl.parentNode);
}

// Outputs related link child text to the page.
function expandRelatedLinksOutput(transport)
{
	// Note: "Loading Placeholder" only Exists the first time the "Related Link" is Expanded!
	//       There will be no call to Replace the "Loading Placeholder" HTML with Ajax responce HTML on subsequent calls!
	var activeEl = transport.request.options.currentTarget; // Get the Object Passed through the Ajax Request.
	// Replace the "Loading Placeholder" Element with the Ajax Response Text (ie. New HTML)
	var placeholders = $(activeEl).up().getElementsByClassName("loadingPlaceholder");
	if (placeholders != null)
		$(activeEl).up().getElementsByClassName("loadingPlaceholder")[0].replace(transport.responseText);
}

function removeRelatedLinksLoadingPlaceholder(parentOfLoadingElement)
{
	if (!parentOfLoadingElement)
		return;
	
	var loadingElement = $(parentOfLoadingElement).getElementsByClassName("loadingPlaceholder");

	if (!loadingElement || loadingElement.length < 1)
		return;
		
	parentOfLoadingElement.removeChild(loadingElement[0]);
}
