/* modify_path
 *
 * For anchor links (i.e. beginning with '#'), the path should not change.  For 
 * absolute paths (i.e. containing '://'), the path should not change.  For all
 * other paths, prepend the relative sitemap directory.
 */
function modify_path(orig_path, relative_dir) 
{
  if (orig_path.substr(0,1) == "#")
    return orig_path;
  
  if (orig_path.indexOf("://") != -1)
    return orig_path;
  
  new_path = relative_dir + orig_path;
  return new_path;
}

/* modify_anchor
 *
 * Run the modify_path method on the href attribute of an
 * anchor element, if it has an href attribute.
 */
function modify_anchor(anchor_elem, relative_dir)
{
  if (anchor_elem.hasAttribute == null)
    return;
  
  if (anchor_elem.hasAttribute("href")) {
    link_path = anchor_elem.getAttribute("href");
    new_path = modify_path(link_path, relative_dir)
    anchor_elem.setAttribute("href", new_path)
  }
}

function hide_menu(menu_name)
{
  // The 'hide_menu' method would normally set the display property to "none",
  // but I decided that I didn't like that particular effect.  So, instead of 
  // removing the action from the links just yet, I modified the 'hide_menu'
  // method to show the menus as well.  The effect is that, since both 'hide_'
  // and 'reveal_menu' show the menus, they are always shown.
  document.getElementById(menu_name).style.display = "block";
}

function hide_all_menu()
{
  hide_menu("about-menu");
  hide_menu("nkrumah-menu");
  hide_menu("supp-menu");
  hide_menu("primary-menu");
  hide_menu("external-menu");
}

function reveal_menu(menu_name)
{
  var submenu = document.getElementById(menu_name);
  if (submenu.style.display != "block")
  {
    hide_all_menu();
    submenu.style.display = "block";
  }
}

function toggle_menu(menu_name)
{
  var submenu = document.getElementById(menu_name);
  if (submenu.style.display == "block")
  {
    submenu.style.display = "none";
  }
  else
  {
    submenu.style.display = "block";
  }
}

function import_menu(http_object)
{
  // The following code is here thanks to QuirksMode.com 
  // (http://www.quirksmode.org/js/navigation.html)
  // ------------------------------------------------------------
  var container = document.createElement('div');
	container.innerHTML = http_object.responseText;
	
	var x;
	var siteMap;
	
	x = container.getElementsByTagName('div');
	for (var i=0;i<x.length;i++) {
		if (x[i].id == 'main-menu') {
			siteMap = x[i];
			break;
		}
	}
	if (!siteMap) return;
	
	document.getElementById('imported-menu').innerHTML = '';
	document.getElementById('imported-menu').appendChild(siteMap);
	container.innerHTML = '';
  // ------------------------------------------------------------
}

function import_splash_menu(http_object)
{
  import_menu(http_object);
}

function prep_splash_page()
{
  sendRequest('sitemap.html', import_splash_menu);
}

relative_sitemap_directory = ''

function import_main_menu(http_object)
{
  import_menu(http_object);
  
  // Make the menus drop-down-able
  var imenu = document.getElementById('imported-menu');
  var category, category_name;
  
  x = imenu.getElementsByTagName('a');
  for (var i = 0; i < x.length; i++) {
    if (x[i].className == 'super-a') {
      category = x[i];
      category_name = category.id.substring(0,category.id.length-5); // cut off the "-link"
      category.setAttribute("onmouseover","javascript: reveal_menu('" + category_name + "-menu');");
//      category.setAttribute("href","javascript: toggle_menu('" + category_name + "-menu');");
    }
    
    cur_link = x[i]
    modify_anchor(cur_link, relative_sitemap_directory)
  }
}

function buy_time_while_loading(http_object)
{
  var hold_please = document.createElement('div');
	hold_please.innerHTML = "Loading sitemap..."
	hold_please.className = "menu-problem-instructions"
	
	document.getElementById('imported-menu').innerHTML = '';
	document.getElementById('imported-menu').appendChild(hold_please);
}

function prep_normal_page(relative_dir)
{
  if (relative_dir != null)
    relative_sitemap_directory = relative_dir;
  
  if (relative_sitemap_directory != "" && document.getElementsByTagName("body")[0].hasAttribute == null)
    // Browsers that do not understand certain XML DOM methods cannot handle
    // the URL mangling.  So, if URL mangling is necessary (i.e. if we are not
    // in the same directory as the site map), then just bail.
    return;
  
  buy_time_while_loading();
  
  sitemap_path = relative_sitemap_directory + 'sitemap.html';
      
  sendRequest(sitemap_path, import_main_menu);
//  document.getElementById('middle').setAttribute("onmouseover","javascript: hide_all_menu();");
}

