// Holds the object of the menu item which is currently highlighted
var current_highlight = -1;
var show_submenu;

// Put in here rather than onload so that the template files and existing code can remain the same
get_query_variable();

function get_query_variable()
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  var pair, i;
  var filename = window.location.href;

  show_submenu = -1;

  for (i=0; i < vars.length; i++)
  {
    pair = vars[i].split("=");

    if (pair[0] == 'submenu')
	{
		show_submenu = parseInt(pair[1]);
    }
  }


	// If index.html or / is the leafname, then check for a submenu.  This will then display that submenu by default.
  	if (filename.substr(filename.length - 10, 10) == "index2.html" ||
		filename.substr(filename.length - 1, 1) == "/787")
	{
		if (smItemsLinks[0][0] != "#")
		{
			show_submenu = 0;
		}
	}
}

// Function called at load time to display the main menu, built from the mItems array.
// h_or_v : 1 = horizontal, 2 = vertical
function display_menu(a, h_or_v)
{
	var menu_html = "", set_separator = "&nbsp;-&nbsp;";

	if (h_or_v == 1) // Horizontal menu
	{
		// Loop through all the menu items in the main array including the script for changing the submenu when clicked.
		for (i = 0; i < mItems.length; i++)
		{
			// The SPAN tag allows the background colour to be changed for the highlighted menu item. 
			if (mItemLinks[i] == '#') // There *is* a submenu and no link from the top-level item
			{
				document.write("<span class='nav_bg' id='nav_" + i + "' onMouseDown='javascript:display_horizontal_submenu(" + i + ", this);'><a href='#'>&nbsp;" + mItems[i] + "&nbsp;</a></span>");
			}
			else // There *isn't* a submenu or there's both
			{
				if (smItemsLinks[i] == '#') // There's a top-level link, but no submenu
				{
					document.write("<span class='nav_bg' id='nav_" + i + "'><a href='" + mItemLinks[i] + "'>&nbsp;" + mItems[i] + "&nbsp;</a></span>");
				}
				else // There's a top-level link AND a submenu
				{
					document.write("<span class='nav_bg' id='nav_" + i + "'><a href='" + mItemLinks[i] + "?submenu=" + i + "'>&nbsp;" + mItems[i] + "&nbsp;</a></span>");
				}

			}
			
			// Only add the separator if it's not the final item.
			if (i < (mItems.length - 1))
			{
				document.write(set_separator);		
			}
		}
		
		if (show_submenu != -1) // Submenu passed in through the query string
		{
			// Shows the submenu that was passed in after a short delay to allow the HTML to display first
			setTimeout("display_horizontal_submenu(show_submenu, 'nav_' + show_submenu);", 200);
			//show_submenu = -1;
		}
	}
	else // Vertical menu
	{
		// Loop through the number of main menu items and also show the submenu for the specified main item.
		for (i = 0; i < mItems.length; i++)
		{
			if (mItemLinks[i] == '#') // There *is* a submenu
			{
				menu_html = menu_html + "<span class='nav_sub_bg' id='nav_" + i + "' onMouseDown='javascript:display_menu(" + i + ", 2);'><a href='" + mItemLinks[i] + "'>" + mItems[i] + "</a></span><br>";
			}
			else  // There *isn't* a submenu or there's both
			{
				if (smItemsLinks[i] == '#') // There's a top-level link, but no submenu
				{
					menu_html = menu_html + "<span class='nav_sub_bg' id='nav_" + i + "'><a href='" + mItemLinks[i] + "'>" + mItems[i] + "</a></span><br>";
				}
				else // There's a top-level link AND a submenu
				{
					menu_html = menu_html + "<span class='nav_sub_bg' id='nav_" + i + "'><a href='" + mItemLinks[i] + "?submenu=" + i + "'>" + mItems[i] + "</a></span><br>";				
				}
			}

			// Show the sub menu for the specified item if there is a submenu.
			if (i == a && mItemLinks[i] == '#')
				menu_html = menu_html + display_vertical_submenu(a);

			// Show the sub menu for a query string passed in.  Will only happen the first time!
			if (i == show_submenu)
			{
				menu_html = menu_html + display_vertical_submenu(show_submenu);
				show_submenu = -1;
			}
		}
	
		// Change the sub menu HTML - different methods for IE and Netscape browsers
		if (document.all) // IE
			side_menu.innerHTML = menu_html;
		else
			document.getElementById("side_menu").innerHTML = menu_html;
	}
}

// Function called when user clicks on a *main* menu item.  Changes the background colour and the sub menu items.
// For horizontal menus only
function display_horizontal_submenu(a, this_obj)
{
	var html_output, set_separator = "&nbsp;&nbsp;/&nbsp;&nbsp;";
	var i;

	// Clear the current highlight
	if (current_highlight != -1)
	{
    	current_highlight.className = "";
	}

	// Set the new highlight of the menu item
	this_obj.className = "nav_sub_bg";
	// Store the object of the new highlighted menu item
	current_highlight = this_obj;

	// Set a default sub menu HTML contents (i.e. blank)
	html_output = "";

	// Loop through all the submenu items for the specified main menu item.
	for (i = 0; i < smItems[a].length; i++)
	{
		html_output = html_output + "<a href='" + smItemsLinks[a][i] + "'>" + smItems[a][i] + "</a>";

		// Only add the separators if it's not the final item.
		if (i < (smItems[a].length - 1))
		{
			html_output = html_output + set_separator;
		}
	}

	// Change the sub menu HTML - different methods for IE and Netscape browsers
	if (document.all) // IE
		submenu.innerHTML = html_output;
    else
   		document.getElementById("submenu").innerHTML = html_output;
   		
   	if (show_submenu != -1)
   		show_submenu = -1;
}

// Creates the submenu list under it's parent menu item.
// For vertical menus only
function display_vertical_submenu(a)
{
	var html_output = "", set_spaces = "&nbsp;&nbsp;&nbsp;&nbsp;";
	var i;

	// Loop through the submenu items for the specified main item.
	for (i = 0; i < smItems[a].length; i++)
	{
		html_output = html_output + set_spaces + "<span class='nav_sub_links'><a href='" + smItemsLinks[a][i] + "'>" + smItems[a][i] + "</a></span><br>";
	}

	return (html_output);
}
