window.onload = function()
{
	// Preload images (a hold over from Macromedia).
	MM_preloadImages('../../IMAGES/nav_menu2.gif',
			 '../../IMAGES/nav_feature2b.gif',
			 '../../IMAGES/a_sitemap2b.gif',
			 '../../IMAGES/a_home2b.gif',
			 '../../IMAGES/R_ARROW.GIF',
			 '../../IMAGES/R_ARROW2.GIF');
			 
	// Pull up the cookie values.
	var intLastLayer = GetCookie( GetCookiePrefix() + "LAYER" );
	var intLastImgLink = GetCookie( GetCookiePrefix() + "IMAGE" );

	// (Set default cookie values, if they're null.)
	if ( intLastLayer==null )
		intLastLayer = 0;

	// Set event handlers.
	GetSetNavLinks();
	SetSwapPicEvents();

	SetLinksThatDoNotUseCookies();

	// Initialize the onload state.

	InitLayer(intLastLayer);
	InitSwapPic(intLastLayer, intLastImgLink);
};

function SetSwapPicEvents()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Sets the onmouseover event             |
	   | for all anchors of the class "swapPic" |
	   | to execute the SwapPic() function.     |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	var anchors = GetObjsByClass(document, "a", "swapPic");

	for ( var i=0; i < anchors.length; i++ )
	{
		anchors[i].onmouseover = function()
			{ SwapPic(this); };
	}
}

function InitSwapPic(intLastLayer, intLastImgLink)
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Initializes the current layer's swapPics. |
	   | Does this by using stored cookie values   |
	   | to help simulate a mouse rollover         |
	   | that displays the proper image.           |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	// If the cookie holding the position 
	// of last displayed "swapPic" image isn't null...
	if ( intLastImgLink )
	{

		// ...gather up all the contentLayers...
		var contentLayers = GetObjsByClass(document, "div", "content");

		// ...single out the last displayed layer
		// and gather up its "swapPic" anchors...
		var swapPicLinks = GetObjsByClass(contentLayers[intLastLayer], "a", "swapPic");

		// ...single out the anchor corresponding 
		// to last displayed "swapPic" image
		// and simulate a mouse rollover.
		if ( swapPicLinks[intLastImgLink] )
			swapPicLinks[intLastImgLink].onmouseover();
	}
}

function SetLinksThatDoNotUseCookies()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Sets the onclick event                             |
	   | for all anchors that should ignore cookie settings |
	   | to execute the ResetCookies() function.            |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	// Single out the the link in the titleSection <div>
	// that points to the forKids index page.
	var titleLink = document.getElementById("titleSection").getElementsByTagName("a")[0];

	titleLink.onclick = function()
		{ ResetCookies(); };

	// Gather up all the links in the tabSection <div>.
	var tabAnchors = document.getElementById("tabSection").getElementsByTagName("a");

	for ( var i=0; i < tabAnchors.length; i++ )
		tabAnchors[i].onclick = function()
			{ ResetCookies(); };
}

function ResetCookies()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | "Zeroes out" the cookies that track   |
	   | your current layer and swapPic image  |
	   | to their default,                     |
	   | "first layer, first swapPic" setting. |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	document.cookie = GetCookiePrefix() + "LAYER=0;";
	document.cookie = GetCookiePrefix() + "IMAGE=0;";
}

function InitLayer(intLastLayer)
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Initializes the current layer.           |
	   | Does this by using a stored cookie value |
	   | to help simulate a mouse click           |
	   | that displays the proper layer.          |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	// Gather up all content layers.
	var contentRightLayers = GetObjsByClass(document, "div", "contentRight");
	
	// You can't single out the "first" link on the first layer, 
	// because there is no "first" link for the first layer.
	// To counteract this, always begin on the NEXT layer.
	// (To wrap around after you reach the last layer, use the modulo operator.)
	var nextLayer = ( parseInt(intLastLayer)+1 ) % (contentRightLayers.length-1);

	// Using the cookie that points to the last displayed layer,
	// single out the layer NEXT TO it, 
	// and gather up its content layer nav anchors.
	var links = contentRightLayers[nextLayer].getElementsByTagName("li");

	// Single out the anchor corresponding 
	// to the cookie
	// and simulate a mouse click.
	links[intLastLayer].getElementsByTagName("a")[0].onclick();
}

function GetCookiePrefix()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Creates and returns a prefix                        |
	   | that will get applied to the current page's cookies |
	   | using the current page's file name.                 |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	// Apply a regular expression to the URL
	// that reduces it to file name without a prefix.
	// Each page in forKids will have a unique set of cookies
	// by virtue of the fact that 
	// each file in a directory must have a unique name.
	 return location.href.match(/[\/][\w]+[\.]/).toString().match(/[\w]+/);
}


function HideLayersByClass(strClass)
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | (Pretty self-explanatory)              |
	   | Styles all <div>s of the class         |
	   | that you pass as an argument           |
	   | to have a visibility that is "hidden." |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	var classedLayers = GetObjsByClass(document, "div", strClass);
	
	for ( var i=0; i < classedLayers.length; i++ )
		classedLayers[i].style.visibility = "hidden";
}

function GetSetNavLinks()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Gets all the links in contentRight <div>s, |
	   | as well each link's list position.         |
	   | (It also sets them here,                   |
	   |  but I don't want to make                  |
	   |  the name of this function                 |
	   |  anymore complicated than it already is.)  |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	var contentRightLayers = GetObjsByClass(document, "div", "contentRight");

	// Begin the process of setting the nav link's events.
	for ( var i=0; i < contentRightLayers.length; i++ )
	{
		var links = contentRightLayers[i].getElementsByTagName("li");
		
		for ( var j=0; j < links.length; j++ )
		{
			if ( links[j].getElementsByTagName("a")[0] )
			{
				var currentLink = links[j].getElementsByTagName("a")[0];

				SetNavLinkEvents( currentLink, j );
			}
		}
	}
}

function SetNavLinkEvents(objLink, intPosition)
{
	/* Each of the nav's onclick events
	   is dependent upon its list position,
	   so it each link's onclick event
	   must be handled dynamically.  */

	objLink.onclick = function()
	{
		// Hide everything.
		HideLayersByClass("contentLeft");
		HideLayersByClass("midBg");

		// Get midBg that corresponds to the position of the <li> that was clicked.
		var bkgdLayers = GetObjsByClass(document, "div", "midBg");
		
		// Get pic by ID of current midBg layer.
		var picLayers = GetObjsByClass(bkgdLayers[intPosition], "div", "contentLeft");

		// Show that layer and that pic.
		bkgdLayers[intPosition].style.visibility = "visible";
		
		if ( picLayers[0] )
			picLayers[0].style.visibility = "visible";	// (later, get ID of current visible picLayer, do the same)

		// Save the current layer position as a cookie.
		document.cookie = GetCookiePrefix() + "LAYER=" + intPosition + ";";
	};
}

function SwapPic(objLink)
{
// a function replacing the method Matt currently uses
// to rotate thru pictures on a layer.

	// go out from the link until you hit the element with a class of "content."
	var objContent = objLink.parentNode;
	
	while ( objContent.className != "content" )
		objContent = objContent.parentNode;

	// get all swapPics in content.
	var swapPicLinks = GetObjsByClass(objContent, "a", "swapPic");
	
	var intOrder = 0;
	// of all the swapPic links in the currently visible content,
	// find out the order number of the one that was rolled over.
	for ( var i=0; i < swapPicLinks.length; i++ )
	{
		if ( swapPicLinks[i]==objLink )
			intOrder = i;
	}

	// use this order number to
	// determine which image to show.

	// get all contentLeft layers in the currently visible midBg
	
	// first, pinpoint the currently visible midBg
	var objMidBg = objContent.parentNode;
	
	while ( objMidBg.className != "midBg" )
		objMidBg = objMidBg.parentNode;

	// get all image layers in this midBg
	var swapPicLayers = GetObjsByClass(objMidBg, "div", "contentLeft");

	// hide all image layer
	for ( var i=0; i < swapPicLayers.length; i++ )
		swapPicLayers[i].style.visibility = "hidden";

	// then show the image layer corresponding to the
	// link that was rolled over
	swapPicLayers[intOrder].style.visibility = "visible";
	
	// lastly, save the last visible image as a cookie
	document.cookie = GetCookiePrefix() + "IMAGE=" + intOrder + ";";
}

function GetObjsByClass(objSrc, strTag, strClass)
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Operates kind of like a GetElementByClass() function. |
	   | Searches inside the objSrc object for                 |
	   | all elements with a tag equal to strTag               |
	   | AND a class equal to strClass,                        |
	   | returning the result.                                 |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	var classedObjs = new Array();
	var counter = 0;
	
	var allSameTags = objSrc.getElementsByTagName(strTag);
	
	for ( var i=0; i < allSameTags.length; i++ )
	{
		if ( allSameTags[i].className==strClass )
		{
			// Every time an element meets both criteria,
			// store it in classedObjs.
			classedObjs[counter] = allSameTags[i];
			// counter keeps track of
			// the last occupied slot 
			// in the classedObjs[] array.
			counter++;
		}
	}

	return classedObjs;
}