////////////////////////////////////////////////////////////////////////////////
//
// Menu Management Module
// Copyright 2003-2010 Russell Warburton
//
// Filename: /js-bin/menu.js (javascript)
// Author: Russell Warburton (russell@warburnet.com.au)
//
// No portion of this computer program may be used without prior written 
// permission from the author. This computer program is protected by 
// international copyright laws.
//
// Created: --/--/--
// Updated: 12/09/10 - head comment section added
// Updated: 22/09/10 - updated preloadMenuImages with new image filenames
// Updated: 15/10/10 - fixed content item link detection for firefox
// Updated: 18/10/10 - fixed showRelatedItems function for firefox
// Updated: 18/10/10 - added showChildItems function on prolonged mouse hover
// Updated: 20/10/10 - added support for top_menu and bottom_menu
// Updated: 21/10/10 - added browserSupported function
// Updated: 31/10/10 - removed hiding of items deeper than level 2
//
////////////////////////////////////////////////////////////////////////////////

var mouseHoverTimer;

function browserSupported()
{
	var browserSupportsDHTML = false;
	var browserSupportsW3CDOM = false;

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

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

	return browserSupportsDHTML && browserSupportsW3CDOM;
}

function createSiteMenus()
{

	if (browserSupported())
	{
		// Create the side menu
		createMenu('left_menu', true);

		// Create the top menu
		createMenu('top_menu', false);

		// Create the bottom menu
		createMenu('bottom_menu', false);
	}
}

function createMenu(theMenuIdString, theShowRelatedFlag)
{
	var rootMenuItem = null;

	// Save the root menu item
	rootMenuItem = eval(theMenuIdString);

	var selectedItem = null;

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

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

function createChildMenu(theMenuItem, theMenuLevel, theSelectedItem)
{
	// Retrieve the array of child items from the parent item
	var childItems = theMenuItem.childNodes;

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

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

				// Retrieve the array of content items from the parent item
				var contentItems = menuItem.childNodes;

				for (var contentIndex = 0; contentIndex < contentItems.length; contentIndex++)
				{
					// Extract a content item from the array
					var contentItem = contentItems[contentIndex];

					// 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";
							menuItem.className = "selected";

							// Save the selected item
							theSelectedItem = menuItem;
						}

						// Set the attributes of this element
						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) + ")");
					}
				}

				// Create another child menu
				theSelectedItem = createChildMenu(menuItem, theMenuLevel + 1, theSelectedItem);
			}
		}
	}

	// Return the selected item
	return theSelectedItem;
}

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();
	}

	// Start the mouse hover timer for this menu item
	mouseHoverTimer = setTimeout("showChildItems(" + String(menuItem.id) + ")", 1000);

}

function eventMouseOut(theMenuItemId)
{
	// Clear the mouse hover timer
	clearTimeout(mouseHoverTimer);

	// 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 showChildItems(theMenuItemId)
{

	var parentItem = eval(theMenuItemId);

	// Get the next parent item - quick fix - 18/10/10
	parentItem = parentItem.parentNode;

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

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

			// Check if this item is a division
			if (menuItem.nodeName == "DIV")
			{
				// Retrieve the array of content items from the parent item
				var contentItems = menuItem.childNodes;

				for (var contentIndex = 0; contentIndex < contentItems.length; contentIndex++)
				{
					// Extract a content item from the array
					var contentItem = contentItems[contentIndex];

					// Check if the content item is a link
					if (contentItem.nodeName == "A")
					{
						// Show the menu item
//						contentItem.style.display = "block";
						menuItem.style.display = "block";
					}
				}
			}
		}
	}
}

// 18/10/10 - function no longer in use
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 childIndex = 0; childIndex < childItems.length; childIndex++)
		{
			// Extract a child item from the array
			var menuItem = childItems[childIndex];

			// Check if this item is a division
			if (menuItem.nodeName == "DIV")
			{
				// Retrieve the array of content items from the parent item
				var contentItems = menuItem.childNodes;

				for (var contentIndex = 0; contentIndex < contentItems.length; contentIndex++)
				{
					// Extract a content item from the array
					var contentItem = contentItems[contentIndex];

					// Check if the content item is a link
					if (contentItem.nodeName == "A")
					{
						if (nextParent == theMenuItem)
						{
							// Change the class to the related class
//							contentItem.className = "related";
							menuItem.className = "related";
						}

						// Show the menu item
//						contentItem.style.display = "block";
						menuItem.style.display = "block";
					}
				}
			}
		}

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

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

