// JavaScript Document

// VARIABLES
// is this the homepage?
var pageIndex = false;
var folderName;
// name of the xml that has the navigation
var listName = "navigation.xml";
var subMenuTimer;
// the sub menu name
var menuName = "none";
// main nav button name
var mainNavName;

// FUNCTIONS
window.onload = function(){
	if (pageIndex == false) {
		getTheFolder();
	}
	getData(listName);
	getSideNav(listName);
}

//Get the name of the folder from the url
function getTheFolder() {
	// Get the URL
	var theUrl = window.location.pathname;
	// Set the pattern to find the slashes
	var pattern = /[/]/g;
	// find the slashes
	var slashes = (theUrl.match(pattern));
	// How many slashes are there?
	var slashLength = slashes.length;
	// covert the slash number from string to int
	var slashNum = parseInt(slashLength);
	// Split up the url by the number of slashes
	var slashSplit = (theUrl.split("/",slashNum));
	// Set the folder name to the last name after the slash
	folderName = slashSplit.pop();
	
	//Trace the name
	//$('debug').innerHTML += folderName;
}

// Get top Navigation from the XML
function getData(listName) {
	var listName = listName;
	$('topNavigation').innerHTML = "";
	new Ajax.Request('/_includes/'+ listName +'', {
		method: 'get',
		onSuccess: function(transport) {
				var xml = transport.responseXML; 
				var nav = xml.getElementsByTagName('nav');
				var navLength = nav.length;
				var html, html2, t, u;
				var title;
				var contentLength;

				// Get the main headers
				for (i = 0; i < navLength; i++) {
					t = (nav[i].getElementsByTagName('title')[0].childNodes.length) ? nav[i].getElementsByTagName('title')[0].childNodes[0].nodeValue: "";
					u = (nav[i].getElementsByTagName('url')[0].childNodes.length) ? nav[i].getElementsByTagName('url')[0].childNodes[0].nodeValue: "";
					html = '<li id="mainLi'+ i +'"><a onmouseover="showMenu('+ i +');" onmouseout="hideMenu('+ i +');" href="' + u + '">' + t + '</a><ul class="subMenu" onmouseover="showMenu('+ i +');" onmouseout="hideMenu('+ i +');"id="ulNav' + i + '"></ul></li>'
					$('topNavigation').innerHTML += html;
					// When you're finished - get the sub menu
					fillBox(listName,i);
				}
		  }
	  });	
}

function fillBox(listName,num) {
	var listName = listName;
	var num = num;
	new Ajax.Request('/_includes/'+ listName +'', {
		method: 'get',
		onSuccess: function(transport) {
			var xml = transport.responseXML;
			var nav = xml.getElementsByTagName('nav')[num];
			var title = nav.getElementsByTagName('title');
			var contentLength = title.length;
			var html, t, u;
			var htmlContent = "ulNav" + num;
			$(htmlContent).style.display = 'none';
			var finished = false;
			
			//Fill the box UL's with content
			for (i = 1; i < contentLength; i++) {
				t = (nav.getElementsByTagName('title')[i].childNodes.length) ? nav.getElementsByTagName('title')[i].childNodes[0].nodeValue: "";
				u = (nav.getElementsByTagName('url')[i].childNodes.length) ? nav.getElementsByTagName('url')[i].childNodes[0].nodeValue: "";
				html = '<li><a href="' + u + '">' + t + '</a></li>'
				$(htmlContent).innerHTML += html;

			}
		}
	});
}

// Create Drop Down Menu
function showMenu(num) {
	var num = num;
	// Name of the Menu that pops up
	var menu = "ulNav" + num;
	// name of the menu that needs the hover image
	var mainNav = "mainLi" + num;
	// hide the menu if you just move the mouse to the next top button
	if (menuName != "none") {
		$(menuName).style.display = "none";
		$(mainNavName).style.backgroundImage = "none";
		clearTimeout(subMenuTimer);
	}
	// show the menu and add the image
	$(menu).style.display = "block";
	$(mainNav).style.backgroundImage = "url(/images/navBG.jpg)";
	$(mainNav).style.backgroundPosition = "0 -34px";
	//$('debug').innerHTML += mainNav;
}

function hideMenu(num) {
	var num = num;
	var menu = "ulNav" + num;
	var mainNav = "mainLi" + num;
	// save the last menu name and last main nav name
	menuName = menu;
	mainNavName = mainNav;
	// set the timer to hide the menu
	subMenuTimer = setTimeout("hidMenu();",400);	
}

//hide the menu
function hidMenu() {
	$(mainNavName).style.backgroundImage = "none";
	$(menuName).style.display = "none";
}



// Create the side navigation
function getSideNav(listName) {
	var listName = listName;
	new Ajax.Request('/_includes/'+ listName +'', {
		method: 'get',
		onSuccess: function(transport) {
			var xml = transport.responseXML;
			var navL = xml.getElementsByTagName('nav');
			var navLength = navL.length;
			var name, nav, t, u, html;
			var finished = false;
			
			for (i = 0; i < navLength; i++) {
				if (finished == false) {
					name = xml.getElementsByTagName('nav')[i].getAttribute('name');
					if (name == folderName) {
						nav = xml.getElementsByTagName('nav')[i];
						var title = nav.getElementsByTagName('title');
						var contentLength = title.length;
						for (i = 1; i < contentLength; i++) {
							t = (nav.getElementsByTagName('title')[i].childNodes.length) ? nav.getElementsByTagName('title')[i].childNodes[0].nodeValue: "";
							u = (nav.getElementsByTagName('url')[i].childNodes.length) ? nav.getElementsByTagName('url')[i].childNodes[0].nodeValue: "";
							html = '<li><a href="' + u + '">' + t + '</a></li>'
							$('sideNavContent').innerHTML += html
							if (i == contentLength - 1) {
								finished = true;	
							}
						}
					}
				}
			}
		}
	});
}




//$('debug').innerHTML += listName;













