/* Original from http://www.boutell.com/newfaq/creating/include.html
 *
 * My modifications:
 *
 * 2006-08-29:
 *
 * Renamed function clientSideInclude() to putUrl()
 *
 * putUrl(): removed alert() displayed when id wasn't found.
 *
 * putUrl(): moved coded that creates the XMLHttpRequest instance to a
 * function, getXmlHttpReq().
 *
 * Added function putStr() which simply adds a string to a given
 * element, changed putUrl() to use putStr().
 *
 * Added function render() which inserts a menu and latest
 * modification date using putStr() and putUrl().
 *
 * 2007-01-09:
 *
 * Added utility function addClass() which adds a class to an element.
 * I plan to use this for highlighting of current menu.
 *
 * Added utility function basename() which gives the basename of an URL.
 *
 * render(): now also renders a section menu, based on the current page
 * and the submenus[] array.
 *
 * 2007-01-10:
 * 
 * render(): highlights current menu item and sub menu item.
 *
 * 2007-10-14:
 *
 * Introduced a way to call render() when the DOM is loaded, see
 * http://dean.edwards.name/weblog/2005/09/busted/
 *
 * render(): can only be called once logic implemented.
 *           front page didn't show sub menu if using default path
 *           there's a bug: last modified date doesn't work on front page
 */

var submenus = {
  'default.htm': 'home', 'thanks.html': 'home', 'techinfo.html': 'home', 'sitemenu.html': 'home',
  'theweb.html': 'web', 'sites.html': 'web', '25links.html': 'web', 'freshmeatsubs.html': 'web',
  'prog.html': 'prog', 'libs.html': 'prog', 'cadaver.html': 'prog',
  'misc.html': 'misc', 'ffext.html' : 'misc', 'lookalikes.html': 'misc', 
  'scancalc.html': 'misc', 'purity.html': 'misc', 'proverbs.html': 'misc'
};

function addClass(element, name) {
	  if (!element.className) {
    element.className = name;
  } else {
    element.className += " ";
    element.className += name;
  }
}

function basename(url) {
  return url.replace(/.*\//,'');
}


function render() {
  if (arguments.callee.done) return;
  arguments.callee.done = true;

  putUrl('menu', 'menu.inc.html');
  if (typeof document != 'undefined')  putStr('date', document.lastModified);
  
  var page=basename(document.location.href);
  if (page.length == 0) page = 'default.htm';

  if (submenus[page]) {
    putUrl('submenu', submenus[page]+'.inc.html');
  }

  var id='menu.' + submenus[page];
  var element=document.getElementById(id);
  if (element) {
    addClass(element, "current");
  }
  id += '.' + page.replace(/\.html?$/,'');
  element=document.getElementById(id);
  if (element) {
    addClass(element, "current");
  }
}


function putStr(id, str) {
  var element = document.getElementById(id);
  if (!element) {
    return;
  }
  element.innerHTML = str;
}

function putUrl(id, url) {
  var req = getXmlHttpReq();
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    putStr(id, req.responseText);
  } /* else {
    putStr(id, "Sorry, your browser does not support " +
               "XMLHTTPRequest objects. This page requires " +
               "Internet Explorer 5 or better for Windows, " +
               "or Firefox for any system, or Safari. Other " +
  	       "compatible browsers may also exist.");
  }*/
}

function getXmlHttpReq() {
  var req = false;

  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
  return req;
}

