/* To use these functions, here's the HTML syntax you need.

* An example paragraph:

	<p>... <br />
		<a id="purchase-full-doc-combo-financing-show-link" class="show-link" href="#purchase-full-doc-combo-financing" onclick="return showLink(this.href);">Show Details about Full Doc Combo Financing &gt;</a>
		<a id="purchase-full-doc-combo-financing-hide-link" class="hide-link" href="#purchase-full-doc-combo-financing" onclick="return hideLink(this.href);">Hide Details about Full Doc Combo Financing &gt;</a>
	</p>

* On the respective sections:

	<div id="purchase-full-doc-combo-financing" class="section">
	[...]
	</div>

*/

/* This runs right when the document loads */
function hideshow_initialize()
{
	//hideAllSections();

	displayShowLinks();

	/* Just to be sure, display the section specified in the URL (such as "#foo-bar"), if there is one. */
	fixDisplay();

	// Uncomment the below line for if you want to see what the browser thinks the URL is (for debugging)
	// alert("Current URL is: " + document.location.href);

}

/* Just to be sure, display the section specified in the URL (such as "#foo-bar"), if there is one.
	   Of note: I needed to test whether there was an section specified at all. So, that's why I get
	   the length of the getSectionID -- which is 0/false if there is none.

	   This is called by a setInterval method since some browsers don't re-trigger onload (and hence
	   inituialize) when moving back and forward through history. */
function fixDisplay()
{
	if (getSectionID(document.location.href)) showLink(document.location.href);
}

/* This hides all sections */
function hideAllSections()
{
	var sectionsArray = document.getElementsByTagName("div");

	/* Loop through all the divs and hide the "section" divs */
	for (var divNum = 0; divNum < sectionsArray.length; divNum++)
	{
		if ( sectionsArray[divNum].className.match("map-section") )
		{
			Element.addClassName(sectionsArray[divNum], "map-section-collapsed");
		}
	} // end of for-loop


} // end of hideAllSections


/* This displays the "show" links since they've hidden by default (so that
	users without JavaScript don't even see the links) */
function displayShowLinks()
{
	var showLinksArray = document.getElementsByTagName("a");

	/* Loop through all the anchors and display the "show-link" ones */
	for (var anchorNum = 0; anchorNum < showLinksArray.length; anchorNum++)
	{
		if ( showLinksArray[anchorNum].className.match("show-link") )
		{
			showLinksArray[anchorNum].style.display = "block";
		}
	} // end of for-loop
}

/* This accepts a URL as a parameter and returns the intra-page link.
   For example: "http://www.foo.bar/page.html#identity" -> "identity" */
function getSectionID(theURL)
{
	/*  The parameter fed in is imperfect. The HTML looks something like this:

		<a href="#identity" onclick="showLink(this.href);">Identity</a>

	So, what's going to be fed in is "#identity", and the octothrope ("#") needs
	to be trimmed. */

	// This gets the substring. For example: "http://www.foo.bar/page.html#identity" -> "identity"

	octothorpeIndex = theURL.indexOf("#");
	if (octothorpeIndex == -1) return ""; // If it's not in there, return the empty string

	return theURL.substring(octothorpeIndex + 1,theURL.length);
}

/* This displays the section specified */
function showLink(sectionHREF)
{
	// This gets the substring. For example: "http://www.foo.bar/page.html#identity" -> "identity"
	sectionID = getSectionID(sectionHREF);

	/* Make the section viewable */

	sectionDIV = document.getElementById(sectionID);
	Element.removeClassName(sectionDIV, "map-section-collapsed");

	/* After showing the section, hide the "show" link */
	showLinkID = sectionID + "-show-link";
	showLinkAnchor = document.getElementById(showLinkID);

	/* Hide the "show" link */
	showLinkAnchor.style.display = "none";

	/* ... then, show the "hide" link */

	hideLinkID = sectionID + "-hide-link";
	hideLinkAnchor = document.getElementById(hideLinkID);

	/* Show the "hide" link */
	hideLinkAnchor.style.display = "block";

	// Section id is also map name, so insert the map.
	showMap(sectionID);
	
	// This prevents the "<a href='...'></a>" from firing
	return false;
}

function showMap(mapId) {
	var iframe = document.getElementById(mapId + "Frame");
	if(iframe == null) {
		var mapLink = document.getElementById(mapId + "MapUrl");
		if (mapLink) {
			var iframe = document.createElement("iframe");
			iframe.setAttribute("id", mapId + "Frame");
			iframe.setAttribute("frameBorder", "0");
			iframe.setAttribute("vspace", "0");
			iframe.setAttribute("hspace", "0");
			iframe.setAttribute("marginWidth", "0");
			iframe.setAttribute("marginHeight", "0");
			iframe.setAttribute("width", "488");
			iframe.setAttribute("height", "182");
			iframe.setAttribute("scrolling", "no");
			iframe.setAttribute("src", mapLink.href );
	
			var div = document.getElementById(mapId);
			div.appendChild(iframe);
		}
	}
	iframe.style.display = "block";
}

/* This hides the section specified */
function hideLink(sectionHREF)
{
	// This gets the substring. For example: "http://www.foo.bar/page.html#identity" -> "identity"
	sectionID = getSectionID(sectionHREF);

	/* Make the section hidden */

	sectionDIV = document.getElementById(sectionID);
	Element.addClassName(sectionDIV, "map-section-collapsed");

	/* After hiding the section, hide the "hide" link */

	hideLinkID = sectionID + "-hide-link";
	hideLinkAnchor = document.getElementById(hideLinkID);

	/* Hide the "hide" link */
	hideLinkAnchor.style.display = "none";

	/* ... then, show the "show" link */

	showLinkID = sectionID + "-show-link";
	showLinkAnchor = document.getElementById(showLinkID);

	/* Show the "show" link */
	showLinkAnchor.style.display = "block";

	// This prevents the "<a href='...'></a>" from firing
	return false;
}
