//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
//------------------------------------------------------------------------------------------------//
/*
	Dynamic Menu Creation with mouseover, mouseout and selected images
	Author: Nathan Shower
	Version: 1.1
	Last Updated: December 6, 2006
	
	|-----------------------------------------------------------------------|
	| updates in 1.1														|
	|																		|
	| --the element that opens the submenu will now remain highlighted		|
	|	even when the submenu is open and another element is selected.		|
	|																		|
	|-----------------------------------------------------------------------|

	These functions create a navigation menu width mouse events using images.
	
	The menu structure in the include file should be built as if it were on the homepage of the
	website (index.html), in the root directory. The include file should also be located in the
	root directory of the website (typically "httpdocs"). For example: index.html and menu_inc.html.
	
	This function will look at the "href" of each link in the navigation, as well as each image
	source and show the "selected" image if it is appropriate.
	
	The variable in the main function (different) is a boolean (true/false) variable which
	determines if the navigation has seperate images for the page that is "selected" or if it
	should use the "over" image in place of the "selected".
	
	The function also adds onmouseover and onmouseout events to the anchor (<a ...></a>) tags of
	the navigation, resulting in the image changing from "normal" to "highlighted" (or "over") to
	"selected" (if indeed this is the correct page) and back to "normal" once the cursor is moved.
	
	Also, this menu (if in the vertical position) can contain submenus on specific items. The
	layout for such a submenu is described below in the mock-up <html>.
	
	The stipulations for using this menu are (as of now):
		* Function call is made in the onload event of the <body> tag:
			- <body onload="setupMenu();">
		* All files utilizing this menu must be in the same directory.
		* All image files must be in the following format:
			- "normal" image ---> images/<imagename>.<extension>
			- "over" image -----> images/<imagename>_o.<extension>
			- "selected" image -> images/<imagename>_s.<extension>
		* The <style> and <script> tags must be on every page, and are not dynamic (may need "../").
		* <style> should include at least:
			- #navigation { width: [same as images]px; } 		//set the width
			- #navigation img { border: 0px; } 					//remove the link border from the images
		* If the menu is vertical, <style> should include:
			- #navigation a { display: block; } 				//create a newline without <br>s
	

*///The <html> menu must be in the following format (sample menu):
/*
	<head>
		<script type="text/javascript" src="script/menu.js"></script>
		<link href="css/style.css" rel="stylesheet" type="text/css">
	</head>
	
	<body onload="setupMenu();">
	<!-- begin menu -->
		<containing element (div, td) id="navigation">
			<a href="index.html"><img src="images/b_home.jpg" width="100" height="25" alt="home"></a>
			<a href="page1.html"><img src="images/b_page1.jpg" width="100" height="25" alt="page1"></a>
	<!-- begin subMenu -->
			<div style="display: none;">
				<a href="page1_1.html"><img src="images/b_page1_1.jpg" width="100" height="25" alt="page1_1"></a>
				<a href="page1_2.html"><img src="images/b_page1_2.jpg" width="100" height="25" alt="page1_2"></a>
			</div>
	<!-- end subMenu -->
			<a href="page2.html"><img src="images/b_page2.jpg" width="100" height="25" alt="page2"></a>
		</containing element>
	<!-- end menu -->
	</body>
*/
//------------------------------------------------------------------------------------------------//
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
// This function is called in the onload event (onload="setupMenu();") to create a menu that
function setupMenu() {
	var debug = 0;
	var different = false;
	
	var navigation = document.getElementById('navigation');		//find the navigation element
	var navImage = navigation.getElementsByTagName('img');		//array of all the images
	var navLink = navigation.getElementsByTagName('a');			//array of all the links
	var numImages = navImage.length;							//the length of (hopefully) both of the above arrays
	
	var overImage = new Array(numImages);						//
	var underImage = new Array(numImages);						//these three create blank arrays to preload the images
	var selectedImage = new Array(numImages);					//
	
	var imageHeight;		//height of the image in the loop (from html)
	var imageWidth;			//width of the image in the loop (from html)
	var oldSource;			//source of the image in the loop (from html)
	var temp;				//holds an array of all the parts of the filename, split around a "."
	var extension;			//gets the extension of the images
	var imageSource;		//the source to be put into the 3 arrays
	var last;				//the [length - 1] of the temp array for getting the file extension
	var thisImage;			//holds the id of the current image
	
	for (var i = 0; i < numImages; i++) {
		
//clear all variables at the beginning of each iteration
		imageHeight = 0;
		imageWidth = 0;
		imageSource = "";
		temp = "";
		extension = "";
		oldSource = "";
		last = 0;
		thisImage = "";
		
//get and set the width and height of the image		
		imageWidth = navImage[i].width;
		imageHeight = navImage[i].height;
		
//get the file extension of the image
		oldSource = navImage[i].src;
		temp = oldSource.split(".");
		last = temp.length - 1;
		extension = temp[last];
		
//put the filename back together
		for (var ii = 0; ii < last; ii++) {
			imageSource += temp[ii];
			if (ii != last - 1) {
				imageSource += ".";
			}
		}
		
//preload the images into the arrays
		overImage[i] = new Image(imageWidth,imageHeight);
		overImage[i].src = imageSource + "_o." + extension;
		underImage[i] = new Image(imageWidth,imageHeight);
		underImage[i].src = imageSource + "." + extension;
		selectedImage[i] = new Image(imageWidth, imageHeight);
		
//if the selected image is unique, then load it, otherwise, duplicate the selected array
		if (different) {
			selectedImage[i].src = imageSource + "_s." + extension;
		} else {
			selectedImage[i].src = overImage[i].src;
		}
		
//set an id on the images for easier manipulation
		navImage[i].id = "menuItem_" + i;
		
//hold the id so the function will be happy
		thisId = "menuItem_" + i;
		
//get the source of the "highlighted" button and set it in an onmouseover event
		thisImage = overImage[i];
		navImage[i].onmouseover = new Function("setImage('" + thisId + "', '" + thisImage.src + "', '" + thisImage.width + "', '" + thisImage.height + "');");
		
//get the source of the "normal" button and set it in an onmouseout event
		thisImage = underImage[i];
		navImage[i].onmouseout = new Function("setImage('" + thisId + "', '" + thisImage.src + "', '" + thisImage.width + "', '" + thisImage.height + "');");
	}
	
//call menu() to highlight the currently selected webpage
	menu(underImage, overImage, selectedImage);
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
// Get the current file in the browser (.html)
function getCurrentHref (href) {
	var temp = href.split("/");
	var last = temp.length;
	return temp[last-1];
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
// Sets the image source, width and height of the image (item)
function setImage (item, source, width, height) {
	document.getElementById(item).width = width;
	document.getElementById(item).height = height;
	document.getElementById(item).src = source;
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
// Fixes the links if the current file (.html) is not in the root directory
function fixLinkRoot (navLink, currentFolder) {
	var debug = 0; //number for on, 0 for off

	var temp = "";
	var ref = "";
	var tempRef = "";
	var newRef = "";
//loop through all the links
	for (var i = 0; i < navLink.length ; i++) {
//get the parts of the old href
		tempRef = navLink[i].href;
    	temp = tempRef.split("/");
    	last = temp.length;
    	ref = temp[last-3];
    	newRef = "";
//put it back together until it gets to the folder that shouldn't be there
		for (var ii = 0; ii < (last-3); ii++) {
			newRef += temp[ii];
			newRef += "/";
		}
//skip the folder, and add the rest
		if (ref == currentFolder) {
			newRef += temp[last-2];
			newRef += "/";
			newRef += temp[last-1];
		} else {
			newRef += temp[last-3];
			newRef += "/";
			newRef += temp[last-1];
		}
		navLink[i].href = newRef;
	}
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
// Fix the image sources (src="") if the current page is not in the root directory
function fixImageRoot (navImage, currentFolder) {
	var debug = 0; //number for on, 0 for off
	
	var temp = "";
	var ref = "";
	var tempSrc = "";
	var newSrc = "";
//loop through all the images
	for (var i = 0; i < navImage.length ; i++) {
//get the parts of the old source
		tempSrc = navImage[i].src;
    	temp = tempSrc.split("/");
    	last = temp.length;
    	ref = temp[last-3];
    	newSrc = "";
//put it back together until it gets to the folder that shouldn't be there
		for (var ii = 0; ii < (last-3); ii++) {
			newSrc += temp[ii];
			newSrc += "/";
		}
//skip the folder, and add the rest
		if (ref == currentFolder) {
			newSrc += temp[last-2];
			newSrc += "/";
			newSrc += temp[last-1];
		} else {
			newSrc += temp[last-3];
			newSrc += "/";
			newSrc += temp[last-1];
		}
		navImage[i].src = newSrc;
	}
	return navImage;
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
function menu(underImage, overImage, selectedImage) {
    var debug = 0; //number for on, 0 for off

    var currentPage = getCurrentHref(document.location.href);	//the current page
    
    var menuItem = "";				//the current <a>...</a>
    var navRef = "";				//the full href=""
    var currentHref = "";			//the current page (.html)
    var currentFolder = "";			//the current folder
    var testFolder = "";			//a folder to test against
    var notRoot = 0;				//if the folder is not the same as the root folder, set this to 1
    var parent = "";				//the <div> submenu container (for links in the <div>)
    var nextParent = "";			//the <div> submenu container (for the link immediately before the <div>)

    var navigation = document.getElementById('navigation');		//the navigation container <div> or <td>
    var navLink = navigation.getElementsByTagName('a');			//the links in the navigation
    var navImage = navigation.getElementsByTagName('img');		//the images in the navigation
    var outerNav = -1;											//the outer link with a submenu
   
    for (var i = 0; i < navLink.length; i++) {
        navRef = navLink[i].href;
        temp = navRef.split("/");
        last = temp.length;
        currentHref = temp[last-1];
		currentFolder = temp[last-2];
		testFolder = temp[last-3];
	
//strip the $_GET variables off the href strings for better comparison
		temp = currentHref.split("?");
		currentHref = temp[0];
		if (temp[1] != null) {
			hrefComp = temp[1][0];
		}
		
		temp = currentPage.split("?");
		currentPage = temp[0];
		if (temp[1] != null) {
			currentComp = temp[1][0];
		}

//test to see if this image should be selected (.html == .html ?)
        if (currentHref == currentPage) {
        	
//test to see if the file is in the root folder
        	if (currentFolder == testFolder) {
        		notRoot = 1;
        	}
        	
//if the link is in a submenu, display the submenu
            parent = navLink[i].parentNode;
			if (parent.style.display == 'none') {
                parent.style.display = '';
            }
//added 1.1
//also save the position of the outer menu, so it can get highlighted
			var ii = i;
            while (navLink[ii].parentNode.nodeName == "DIV") {
				ii--;
            }
            if (navLink[ii].parentNode.nodeName != "DIV") {
				outerNav = ii;
			}
			
//if the link is the one that is supposed to open the submenu (from a different page), display the submenu
			if (navLink[i+1]) {
				nextParent = navLink[i+1].parentNode;
				if (nextParent.style.display == 'none') {
					nextParent.style.display = '';
				}
			}
        }
    }
    if (notRoot) {
//repair the images and links
    	underImage = fixImageRoot(underImage, currentFolder);
    	overImage = fixImageRoot(overImage, currentFolder);
    	selectedImage = fixImageRoot(selectedImage, currentFolder);
    	fixLinkRoot(navLink, currentFolder);
    }
   	for (var i = 0; i < navLink.length; i++) {
   		navRef = navLink[i].href;
        temp = navRef.split("/");
        last = temp.length;
        currentHref = temp[last-1];
		currentFolder = temp[last-2];
		testFolder = temp[last-3];

//if we are on the current page or, if we're in a submenu and the top image has to be highlighted (added 1.1), make the image highlighted
   		if (currentHref == currentPage || outerNav == i) {
			menuItem = "menuItem_" + i;
			setImage(menuItem, selectedImage[i].src, selectedImage[i].width, selectedImage[i].height);
			navImage[i].onmouseover = new Function("setImage('" + menuItem + "', '" + overImage[i].src + "', '" + overImage[i].width + "', '" + overImage[i].height + "');");
			navImage[i].onmouseout = new Function("setImage('" + menuItem + "', '" + selectedImage[i].src + "', '" + selectedImage[i].width + "', '" + selectedImage[i].height + "');");
//otherwise, set the mouseover and mouseout
   		} else {
	   		menuItem = "menuItem_" + i;
			setImage(menuItem, underImage[i].src, underImage[i].width, underImage[i].height);
	   		navImage[i].onmouseover = new Function("setImage('" + menuItem + "', '" + overImage[i].src + "', '" + overImage[i].width + "', '" + overImage[i].height + "');");
			navImage[i].onmouseout = new Function("setImage('" + menuItem + "', '" + underImage[i].src + "', '" + underImage[i].width + "', '" + underImage[i].height + "');");
   		}
   	}
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
function getDirectory() {
    var location = window.location.href ;
    var temp = location.split("/");
    delete temp[(temp.length-1)];
    var currentDirectory = temp.join("/");
}
//--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==//
