/*
 * menuExpandable2.js - implements an expandable menu based on a HTML list
 * Author: Dave Lindquist (dave@gazingus.org)
 */

if (!document.getElementById)
    document.getElementById = function() { return null; }

var fileName; //define fileName as a global variable

function loadMenu(file_level)
{
	// create the string to go up the necessary number of levels depending on what page you're on
	// root = 0;
	level='';
	
	for(i=0; i<file_level; i++)
		level += '../';
	
	// check to see if the menu was hidden on the last page
	// if cookie is set to hidden, then hide the menu. Otherwise, don't change.
	if (getCookie() == 'hidden')
		hideMenu();
		
	// call necessary functions to load the menu
	selectVisited();
	oddEven();
	buildMenu();
}

function initializeMenu(menuId, actuatorId, specificId) {
	var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

	if (menu.style.display == "block")
		actuator.parentNode.style.backgroundImage = "url(../images/minus.gif)";
	else
		actuator.parentNode.style.backgroundImage = "url(../images/plus.gif)";

    actuator.onclick = function()
	{
        var display = menu.style.display;
        this.parentNode.style.backgroundImage =
            (display == "block") ? "url("+ level + "images/plus.gif)" : "url("+ level + "images/minus.gif)";
        menu.style.display = (display == "block") ? "none" : "block";

        return false;
	}
}

function openBranch(Id)
{
	var menu = Id + "_Menu";

	if(document.getElementById(menu)) // Check to see if there is a menu for this page, otherwise skip it
	{
		var menu = document.getElementById(menu);
		menu.parentNode.style.backgroundImage = "url("+ level + "images/minus.gif)";
		menu.style.display = "block";
	}
}

function selectVisited()
{
  var nav = document.getElementById('Menu');
  var links = nav.getElementsByTagName('a');

  for (var i=0; i<links.length; i++)
    if (links[i].href == location.href)
    {
      links[i].className = "current";
	      break;
    }
}

function oddEven()
{
	var finalOutput;

	fileName=getPage(location.href);

	while(fileName.length > 8)
 {
	if (fileName.length == 20)
	{
			finalOutput = removeCharacters(fileName, fileName.length, 6);
			openBranch(finalOutput); 
			fileName = finalOutput + ".htm";
	}
	else
		if (fileName.length == 18 || fileName.length == 15 || fileName.length == 11) // This section is used to open the menu of the page you are on, if it exists
		{
			finalOutput = removeCharacters(fileName, fileName.length, 4);
			openBranch(finalOutput);
			fileName = finalOutput + ".htm";
		}
		if (fileName.length == 18)
		{
			finalOutput = removeCharacters(fileName, fileName.length, 7);
			openBranch(finalOutput);
			fileName = finalOutput + ".htm";
		}
		else
			if (fileName.length == 15)
			{
				finalOutput = removeCharacters(fileName, fileName.length, 8);
				openBranch(finalOutput);
				fileName = finalOutput;
			}
			else
				return;
 }
}



function getPage(fileName)
{
var fileName = new String(fileName);
var j = 0;

	for (i=0; fileName.charAt(fileName.length - i) != '/' && fileName.charAt(fileName.length - i) != '\\'; i++)
			j = i;
 
fileName = removeCharacters(fileName, j, 0);

return fileName;
}

function removeCharacters (inputString, s, e) 
{

	var returnString = new String(inputString);
	var start = returnString.length - s;
	var end = returnString.length - e;

	returnString = returnString.substring(start,end);

	return returnString;
}

function buildMenu()
{
	// Dynamically find all actuators and menus and initialize them
	var nav = document.getElementById('Menu');
	var actuators = nav.getElementsByTagName('a'); // Find all a tags
	var Menu = "";

	for (var i=0; i < actuators.length; i++){
		if (actuators[i].id.substring(0,3) == "fir") // if a tag's ID doesn't start with fir don't bother looking at it
		{
			//Menu = actuators[i].href;
			Menu = actuators[i].id.substring(0, actuators[i].id.length - 9); // Take actuator and create Menu name (the -9 is to remove "_Actuator")
			Menu += "_Menu"; // Append _Menu to the end of the current name so it matches
			//alert(Menu);
			//alert(actuators[i].id + " :: " + i);
			//alert(Menu + ", " + actuators[i].id);
			initializeMenu(Menu, actuators[i].id);
		}

		if (actuators[i].id.substring(0,13) == 'fir_Resources') //open resources branch if you are in the resources section
			openBranch('fir_Resources');
	}
}