/*
This file is designed to build/write the left-side navigational menu for the ### section of federalreserve.gov
8.23.2006 - Mag.P.
*/

/* In the event of any JavaScript error, the following runs */
window.onerror = fallout;
function fallout() {
	document.write("This feature is currently unavailable.<br /><a href='/resources/reportingleftnav.htm'>Menu</a>");
}

/* The following is a listing of identifiers for the first level headings followed by a list of valued pairs that indicate each menu item.  First level headings have a value of '%FIRSTLEVELHEADING', while links residing in the first level are identified by a dollar sign preceding the URL/HREF like so: '$/folder/file.html'. The dollar sign and percentage sign are necessary. In the event a link ends in default.htm or index.htm or default.cfm or default.aspx, the URL/HREF should end with a / this is a signifier to the javascript to check all the possible file names of this URL/HREF to verify if it is a match with the current page. */
/* linx[ menuItem ] = "URL for menuItem", mHl = menuHeadingLevel */
var mHl = new Array("menuOne", "menuTwo", "menuThr", "menuFou", "menuFiv", "menuSix", "menuSev");
var defPages = new Array("default.htm", "default.cfm", "default.aspx", "default.asp", "", "default.html", "index.htm", "index.html", "index.cfm");
var linx = new Object();
var specialOpen = new Boolean();


/* REPORTING FORMS */
specialOpen = true;
linx["Forms by Category"] = "%FIRSTLEVELHEADING";
linx["Financial Statements"] = "/reportforms/CategoryIndex.cfm?WhichCategory=1";
linx["Applications/Structure Change"] = "/reportforms/CategoryIndex.cfm?WhichCategory=2";
linx["FFIEC"] = "/reportforms/CategoryIndex.cfm?WhichCategory=3";
linx["Monetary Policy"] = "/reportforms/CategoryIndex.cfm?WhichCategory=4";
linx["Research"] = "/reportforms/CategoryIndex.cfm?WhichCategory=5";
linx["Business/Consumer Credit"] = "/reportforms/CategoryIndex.cfm?WhichCategory=6";
linx["Securities Exchange Act of 1934"] = "/reportforms/CategoryIndex.cfm?WhichCategory=7";
linx["Municipal and Government Securities"] = "/reportforms/CategoryIndex.cfm?WhichCategory=8";
linx["Activities Monitoring"] = "/reportforms/CategoryIndex.cfm?WhichCategory=9";


/* The following function builds and writes the menu.  It selects the type of list and image to write by first looking at the first character of the link.  It then checks whether it should highlight the page it's on in reference to the list of links.  This is accomplished using the next function (menuItemHighlighter()). */
function buildMenu() {
	var theMenu = new String('');
	var firstLevelCounter = new Number(0);
	var firstLevelHighlight = new Boolean(true);
	var closeTag = new Boolean();
	for (var i in linx) {
		switch(linx[i].charAt(0)) {
			case '%':
				if (closeTag) { theMenu += '</span></ul>'; };
				theMenu += '<ul><a id="menuFirstLevelHeading" title="Expand ' + i + '" onmouseover="(window.status=' + "'" + i + "'" + '); return true;" onmouseout="(window.status=' + "'" + "'" + '); return true;" href="javascript:void(0)" onclick="toggle(' + "'" + mHl[firstLevelCounter] + "'" + '); return false;"><img src="/gifjpg/menu_closed.gif" alt="Expand ' + i + '" border="0" />' + i + '</a><span id="' + mHl[firstLevelCounter] + '">';
				firstLevelCounter++;
				closeTag = true;
			break;
			case '$':
				if (closeTag) { theMenu += '</span></ul>'; };
				if (menuItemHighlighter(linx[i].slice(1))) {
					theMenu += '<ul id="menuLink"><img src="/gifjpg/menu_link.gif" alt="' + i + '" border="0"><a id="menuHighlight" href="' + linx[i].slice(1) + '">' + i + '</a></ul>';
					firstLevelHighlight = false;
				} else {
					theMenu += '<ul id="menuLink"><img src="/gifjpg/menu_link.gif" alt="' + i + '" border="0"><a href="' + linx[i].slice(1) + '">' + i + '</a></ul>';
				}
				closeTag = false;
			break;
			default:
				if (menuItemHighlighter(linx[i])) {
					theMenu += '<li id="menuHighlight">' + i.link(linx[i]) + '</li>';
				} else {
					theMenu += '<li>' + i.link(linx[i]) + '</li>';
				}
				closeTag = true;
			break;
		}
	}
		
	if (closeTag) { theMenu += '</span></ul>'; };
	document.write(theMenu);
	/*if (specialOpen && menuItemHighlighter("/reportforms/")) { toggle("menuOne"); };*/
	if (firstLevelHighlight || specialOpen) { toggle("menuOne"); };
}

/* Please view the previous function description for information on this function. 
First checks if the HREF ends in /, if not it compares the link and the documentURL, if so it compares the documentURL folders and the HREF, if these are the same it checks the possible default pages for a match.  If matched, highlight is active. */
function menuItemHighlighter(linxURL) {	
	var docURL = document.URL;
	var startPointU = docURL.indexOf(".gov/") + 4;
	var endPointU = docURL.lastIndexOf("/") + 1;
	var endLinx = linxURL.lastIndexOf("/") + 1;
	var uFolder = docURL.substring(startPointU, endPointU);
	var lFolder = linxURL.substring(0, endLinx);
	if (uFolder == lFolder) {
		var uFile = docURL.substring(endPointU);
		var lFile = linxURL.substring(endLinx);
		if (uFile == lFile) {
			return true;
		} else {
			var isUfileArray = new Boolean();
			var isLfileArray = new Boolean();
			var count = new Number(0);
			while (count < defPages.length) {
				if (uFile == defPages[count]) {
					isUfileArray = true;
				}
				if (lFile == defPages[count]) {
					isLfileArray = true;
				}
				count++;
			}
			if (isUfileArray & isLfileArray) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return false;
	}
}

/* This function toggles the expanded or collapsed state of the second level menu.  It also changes the link title, the image's alt text, and the image itself as well. */
function toggle(currentMenu) {
	if (currentMenu == "menuHighlight") {
		var checkIt = document.getElementById(currentMenu);
		if (checkIt) { var displayVal = document.getElementById(currentMenu).parentNode;
		} else { return false; };
	} else {
		var displayVal = document.getElementById(currentMenu);
	}
	if (displayVal.style.display == "block") { 
		displayVal.style.display = "none"; 
		var linkTitle = displayVal.parentNode.getElementsByTagName("a")[0].title;
		displayVal.parentNode.getElementsByTagName("a")[0].title = 'Expand ' + linkTitle.substring(linkTitle.indexOf(" ")+1);
		displayVal.parentNode.getElementsByTagName("img")[0].src = '/gifjpg/menu_closed.gif';
		displayVal.parentNode.getElementsByTagName("img")[0].alt = 'Expand ' + linkTitle.substring(linkTitle.indexOf(" ")+1);
	  } else { 
		displayVal.style.display = "block";
		var linkTitle = displayVal.parentNode.getElementsByTagName("a")[0].title;
		displayVal.parentNode.getElementsByTagName("a")[0].title = 'Collapse ' + linkTitle.substring(linkTitle.indexOf(" ")+1);
		displayVal.parentNode.getElementsByTagName("img")[0].src = '/gifjpg/menu_open.gif';
		displayVal.parentNode.getElementsByTagName("img")[0].alt = 'Collapse ' + linkTitle.substring(linkTitle.indexOf(" ")+1);
	}
}

/* I'm not sure we'll need this function */
function preloadImages() { 
	var d=document;
	if (d.images) { 
		if (!d.p) d.p=new Array();
		var i,j=d.p.length,a=preloadImages.arguments; 
		for (i=0; i<a.length; i++) {
			if (a[i].indexOf("#")!=0) { 
				d.p[j]=new Image; d.p[j++].src=a[i];
			}
		}
	}
}