// Site Menu Javascript Code
// Copyright 2005-2010 Warburton Technology
//
// Last Updated: 03/08/10 - Added default class to anchor tags

var rootMenuItem;

var browserSupportsDHTML = false;
var browserSupportsW3CDOM = false;

var selectedItem = null;

var expandableMenus = true;

// Check browser supported functionality
checkBrowserSupport();

function checkBrowserSupport()
{
	// Check browser supports Dynamic HTML
	browserSupportsDHTML = (document.getElementById || document.all || document.layers);

	// Check browser supports W3C Doument Object Model
	browserSupportsW3CDOM = (document.getElementById && document.createElement);

	//browserSupportsDHTML = false;
	//browserSupportsW3CDOM = false;
}

function createSiteMenus()
{
	if (browserSupportsDHTML && browserSupportsW3CDOM)
	{
		// Create the side menu
		createSideMenu();

//		// Create the top menu
//		createTopMenu();
	}
}

function createSideMenu()
{
	// Save the root menu item
	rootMenuItem = eval("side_menu");

	// Create the child menu
	createChildMenu(rootMenuItem, 1);

	if (selectedItem != null)
	{
		// Show the selected item and all parents
		showRelatedItems(selectedItem);
	}
}

function createChildMenu(theMenuItem, theMenuLevel)
{

	// Retrieve the array of child items from the parent item
	var childItems = theMenuItem.childNodes;

	for (var index = 0; index < childItems.length; index++)
	{
		// Extract a child item from the array
		var menuItem = childItems[index];

		// Check if this item is a division
		if (menuItem.nodeName == "DIV")
		{
			// Set the attributes of this element
			menuItem.setAttribute("id", theMenuItem.id + "_" + index);		

			// Extract the content item from the parent item
			var contentItem = menuItem.firstChild;

			// Check if the content item is a link
			if (contentItem.nodeName == "A")
			{
				var linkPath = contentItem.href;

				if (linkPath.indexOf("//") != -1)
				{
					// Remove the protocol string from the link path
					linkPath = linkPath.split("//")[1];

					// Remove the domain from the link path
					linkPath = linkPath.substring(linkPath.indexOf("/") + 1, linkPath.length);
				}

				// Split the link path into separate folders
				var linkPathArray = linkPath.split("/");

				// Compare the link path with the document path
				var pathMatch = checkMatchingPaths(linkPathArray, documentPathArray);

				if (pathMatch == 0)
				{
					// Change the class to the selected class
					contentItem.className = "selected";

					// Save the selected item
					selectedItem = menuItem;
				}
				else
				{
					// Change the class to the default class
					contentItem.className = "default";
				}

				if (theMenuLevel > 2)
				{
					// Hide the menu item
					contentItem.style.display = "none";
				}


				// Set the attributes of this element
				//contentItem.setAttribute("id", theMenuItem.id + "_" + index);		
				contentItem.setAttribute("id", menuItem.id + "_content");		

				// Add event handlers to this item
				contentItem.onclick = new Function("eventMouseClick(" + String(contentItem.id) + ")");
				contentItem.onmouseover = new Function("eventMouseOver(" + String(contentItem.id) + ")");
				contentItem.onmouseout = new Function("eventMouseOut(" + String(contentItem.id) + ")");

			}

			// Check whether this item has child divisions
			if (getNumChildDivisions(menuItem) > 0)
			{
				// Create another child menu
				createChildMenu(menuItem, theMenuLevel + 1);
			}
		}
	}
}

function eventMouseClick(theMenuItemId)
{
	// Prevent this event from bubbling any further
	window.event.cancelBubble = true;

	var menuItem = eval(theMenuItemId);

}

function eventMouseOver(theMenuItemId)
{
	// Prevent this event from bubbling any further
	window.event.cancelBubble = true;

	var menuItem = eval(theMenuItemId);

	if(document.all && !window.opera)
	{
		menuItem.filters.blendTrans.apply();
		menuItem.filters.blendTrans.play();
	}
}

function eventMouseOut(theMenuItemId)
{
	// Prevent this event from bubbling any further
	window.event.cancelBubble = true;

	var menuItem = eval(theMenuItemId);

	if(document.all && !window.opera)
	{
		menuItem.filters.blendTrans.apply();
		menuItem.filters.blendTrans.play();
	}
}

function getNumChildDivisions(theMenuItem)
{
	var numChildDivisions = 0;

	// Get the first child from the menu item
	var nextChild = theMenuItem.firstChild;

	while (nextChild != null)
	{
		// Check if this item is a division
		if (nextChild.nodeName == "DIV")
		{
			// Increment the number of child divisions
			numChildDivisions = numChildDivisions + 1;
		}

		// Get the next child from the menu item
		nextChild = nextChild.nextSibling;
	}

	return numChildDivisions;
}

function showRelatedItems(theMenuItem)
{
	// Get the first parent item
	var nextParent = theMenuItem;

	while (nextParent != null)
	{
		// Retrieve the array of child items from the parent item
		var childItems = nextParent.childNodes;

		for (var index = 0; index < childItems.length; index++)
		{
			// Extract a child item from the array
			var menuItem = childItems[index];

			// Check if this item is a division
			if (menuItem.nodeName == "DIV")
			{
				// Extract the content item from the parent item
				var contentItem = menuItem.firstChild;

				// Check if the content item is a link
				if (contentItem.nodeName == "A")
				{
					if (nextParent == selectedItem)
					{
						// Change the class to the related class
						contentItem.className = "related";
					}
					
					// Show the menu item
					contentItem.style.display = "block";
				}
			}
		}

		// Get the next parent item
		nextParent = nextParent.parentNode;
	}
}

function preloadImages()
{
	for (var index = 1; index <= 4; index++)
	{
		document.write("<img class=\"preloaded_image\" src=\"/styles/current/menus/images/side_menu." + index + ".default.gif\" alt=\"\">");
		document.write("<img class=\"preloaded_image\" src=\"/styles/current/menus/images/side_menu." + index + ".default.hover.gif\" alt=\"\">");
		document.write("<img class=\"preloaded_image\" src=\"/styles/current/menus/images/side_menu." + index + ".related.gif\" alt=\"\">");
		document.write("<img class=\"preloaded_image\" src=\"/styles/current/menus/images/side_menu." + index + ".related.hover.gif\" alt=\"\">");
		document.write("<img class=\"preloaded_image\" src=\"/styles/current/menus/images/side_menu." + index + ".selected.gif\" alt=\"\">");
	}
}
