//	cmsmenu.js -- Curtis's main menu system with drop-down submenus
//
//  Copyright 2004-2010 Curtis Smith
//  No copyright is claimed on the structure or algorithm -- please 
//  feel free to copy and adapt to your own web sites without 
//  fear of my wrath, but do not steal the contents of my pages.
//
//  This code implements a web-page menuing system with a list of
//  principal menu items, each of which has its own submenu of choices.
//
//  The submenus are implemented using the Document Object Module (DOM)
//  and JavaScript.  Browsers without JavaScript or DOM support for
//  getElementById can't use submenus; the user will just be given
//  the primary menu which will lead the user to another page 
//  containing the usual submenu choices.
//
//  Files that incorporate this module should have the curtis.css
//  stylesheet in the header of the .html (or .shtml etc.) file.
//  This incorporation is done by including the HTML tag
//  <link href="../curtis.css" rel="stylesheet" type="text/css">
//  in the header.  For instance here is the header of my main web page:

/*
	<head>
	  <meta http-equiv="Content-type" content="text/html; charset=us-ascii">
          <meta http-equiv="Content-Script-type" content="text/javascript">
	  <title>Curtis's quarters</title>
	  <link href="../curtis.css" rel="stylesheet" type="text/css">
	</head>

*/

//  Also, at the position where the menu is desired, the following code
//  (which includes a barebones primary menu for the non-JavaScript case)
//  should be inserted (note that the precise code depends upon the 
//  relative directory; here we assume the code is being placed in a
//  directory one level deep):

/*

  <div class="menubar" id="cmsmenubar">
  <script type="text/javascript" src="../cmsmenu.js">
  </script>

  <noscript>
    <a href="../">home</a> |
    <a href="../poem/">poetry</a> |
    <a href="../trav/">travels</a> |
    <a href="../pix/">pix</a> |
    <a href="../play/">acting</a> |
    <a href="../misc/">misc.</a> |
    <a href="../me/">about&nbsp;me</a>
  </noscript>

  </div>

*/

var show_reference_menu = 0;
var show_links_menu = 0;

var cms_shown_menu='';        /* menu currently being shown (empty string if none) */
var cms_menu_to_hide = '';    /* menu to be hidden in 1/4 second (or empty string) */

/*
    Show a menu when the cursor is moved over the label word.

    Hide a menu when
         * showing another menu
      or * the cursor has been out of the label word and the menu itself for 1/2 seconds

    At all times, cms_menu_to_hide must be either cms_shown_menu or an empty string.


*/



function show_it(obj)	// show (display) a submenu object
{
  if ( document.getElementById )
  {
    // We only handle modern browsers (those that support getElementById)
    // don't give submenus if screen be too narrow that the menu line
    // had to be split

    if ( cms_menu_to_hide == obj )
    {
        cms_menu_to_hide = '';
    }
    if ( cms_shown_menu == obj )
    {
        cms_shown_menu = ''; /* reset this in a moment or so */
    }

    if ( cms_shown_menu != '' )
    {
      prepare_to_hide(cms_shown_menu);
    }
    if ( document.getElementById('m_home').offsetTop ==
         document.getElementById('m_me').offsetTop )
    {
      document.getElementById(obj).style.visibility = 'visible';
      document.getElementById(obj).style.z_index = +2;
      if ( cms_shown_menu != '' ) 
        hide_it_now(cms_shown_menu);
      cms_shown_menu = obj;
    }
  }
}

function hide_it(obj) // hide a submenu object in 250 ms
{  
  if ( cms_menu_to_hide != '' && cms_menu_to_hide != obj )
  {
    hide_it_now( cms_menu_to_hide );
  }
  cms_menu_to_hide = obj;
  setTimeout( 'hide_it_now("*");', 500 );
}

function prepare_to_hide(obj)	// hide a submenu object
{
  if ( document.getElementById && obj != '' )
  {
    document.getElementById(obj).style.z_index = +1;
  }
}


function hide_it_now(obj)	// hide a submenu object
{
  if ( obj == "*" )
    obj = cms_menu_to_hide;
  if ( document.getElementById && obj != '' )
  {
    document.getElementById(obj).style.visibility = 'hidden';
    document.getElementById(obj).style.z_index = -1;
    if ( cms_shown_menu == obj )
    {
        cms_shown_menu = '';
    }
    if ( cms_menu_to_hide == obj )
    {
        cms_menu_to_hide = '';
    }
  }
}

function locate_it(menuname)  // put menu on screen in good position
{
   // For a given menuname (e.g., 'poetry'), we assume that there are
   // two items with id 'm_poetry' and 'sm_poetry'.

   var choice = document.getElementById ( 'm_' + menuname );
   var submenu = document.getElementById ( 'sm_' + menuname );
   var menu = document.getElementById ( 'cmsmenubar' ); 
   var home = document.getElementById ( 'm_me' ); /* me is last menu entry */
   
   // If the submenu will fit, then we just leave it at offset 0, otherwise
   // we make sure it doesn't go off the right-hand side.

   entire_menu_width = home.offsetLeft +
            home.offsetWidth;

   if (  0 && ( entire_menu_width == 0 || window.opera ) )
   {
     /* looks like we're running Opera web browser */
     
     /* Opera has some serious problems trying to align
        the submenu underneath the menu choice.  We seem 
        to do OK, though,  if we just use a left-aligned 
        menubar.  In old Opera browsers, entire_menu_width 
        is 0. */

      x0 = home.offsetLeft;
      menu.style.textAlign = 'left';
      if ( entire_menu_width == 0 ) return; /* old Opera browser */ 
      if ( menu.style.marginLeft == 0 )
          menu.style.marginLeft = x0 - home.offsetLeft;
   }


   submenu_extent = choice.offsetLeft + submenu.clientWidth;
   if ( submenu_extent > entire_menu_width )
   {
      /* first attempt -- relocate menu after floating pictures */
     //menu.style.clear="both";
     submenu_extent = choice.offsetLeft + submenu.clientWidth;
   }
   if ( submenu_extent > entire_menu_width )
   {
      if ( submenu.offsetWidth >= entire_menu_width )
      {
        submenu.style.width = entire_menu_width + 'px';
        submenu.style.left = -choice.offsetLeft;
      }
      else
      {
        submenu.style.left = ((entire_menu_width - submenu.clientWidth) - 
           choice.offsetLeft)+'px';
      }
   }
}

function make_cmsmenu()
{
  var webleader = '<a href="';
  var bp = '';  // base path // empty string means invalid
  var bpleader = '<a href="http://turtle/web/curtis/';
  var scripturi = '';
  if ( document.getElementById )
  {
     // First, calculate the relative path 
     var menubarElement = document.getElementById('cmsmenubar');
     for ( i = 0; i < menubarElement.childNodes.length; ++i )
     {
       if ( menubarElement.childNodes[i].nodeName == 'SCRIPT' )
       {
         scripturi = menubarElement.childNodes[i].src;
         if ( scripturi.length > 0 )
         {
               /* 
                    Note:  In Firefox, scripturi is a full uri
                    (e.g., "http://plemta.com/cmsmenu.js"), but
                    in Internet Explorer, we might just have a
                    relative filename (e.g., "cmsmenu.js")
               */

           lastslash = scripturi.lastIndexOf('/');
           if ( lastslash >= 0 )
           {
               bp = scripturi.substring (0, lastslash + 1 );
               bpleader = '<a href="' + bp;
           }
           else
           {
               bp = './';
               bpleader = '<a href="';
           }
           break;
         }
       }
     }
  }
  
  if ( bp.length )
    document.writeln('<span class="amenu" id="m_home" onmouseover=\'show_it("sm_home")\' onmouseout=\'hide_it("sm_home")\'>');
  document.writeln(bpleader+'">home</a>');
  if ( bp.length )
  {
    document.writeln('<span class="submenu" id="sm_home" onmouseout=\'hide_it("sm_home")\'>');
      document.writeln(bpleader+'">English</a><br>');
//      document.writeln(bpleader+'de/">Deutsch</a><br>');
//      document.writeln(bpleader+'es/">Espa&ntilde;ol</a><br>');
//      document.writeln(bpleader+'pl.html">Igpay&nbsp;Atinlay</a><br>');
//      document.writeln(bpleader+'indexipa.html">i&#331;&#609;l&#618;&#643;</a></span>');
    document.writeln('</span>');
    document.writeln('</span>');
  }
  document.writeln('|');

  if ( bp.length )  
    document.writeln('<span class="amenu" id="m_poetry" onmouseover=\'show_it("sm_poetry")\' onmouseout=\'hide_it("sm_poetry")\'>');
  
  document.writeln( bpleader+'poem/">poetry</a>' );
  if ( bp.length )
  {
    document.writeln('<span class="submenu" id="sm_poetry">');
      document.writeln(bpleader+'poem/being_close">Being close</a><br>');
      document.writeln(bpleader+'poem/cheatress">The cheatress</a><br>');
      document.writeln(bpleader+'poem/dancing_shepherd">The dancing shepherd</a><br>');
      document.writeln(bpleader+'poem/error-3">Error negative three</a><br>');
      document.writeln(bpleader+'poem/past">I like the past</a><br>');
      document.writeln(bpleader+'poem/not_your_best_friend">I\'m not your best friend</a><br>');
      document.writeln(bpleader+'poem/promise_of_life">Promise of for ever life</a><br>');
      document.writeln(bpleader+'poem/prophet">The&nbsp;prophet&nbsp;of&nbsp;an&nbsp;unknown&nbsp;message</a><br>');
      document.writeln(bpleader+'poem/expounder">Song of the expounder</a><br>');
      document.writeln(bpleader+'poem/ten_commandments">Song&nbsp;of&nbsp;the&nbsp;Ten&nbsp;Commandments</a><br>');
      document.writeln(bpleader+'poem/kiss_or_kick">To kiss me or to kick me?</a><br>');
      document.writeln(bpleader+'poem/two_questions">The two questions</a><br>');
      document.writeln(bpleader+'poem/fondness">Unmatched fondness</a><br>');
      document.writeln(bpleader+'poem/valday2004">Valentine\'s Day 2004</a><br>');
      document.writeln(bpleader+'poem/linda">Without thee, I can\'t live</a>');
    document.writeln('</span>');
    document.writeln('</span>');
  }
  document.writeln('|');

                    /* travels menu */
  
  if ( bp.length )
    document.writeln('<span class="amenu" id="m_travels" onmouseover=\'show_it("sm_travels")\' onmouseout=\'hide_it("sm_travels")\'>');
  document.writeln(bpleader+'trav/">travels</a>');
  if ( bp.length )
  {
    document.writeln('<span class="submenu" id="sm_travels">');
      document.writeln(bpleader+'trav/cruise_mmiv/">Bahamas (2004)</a><br>');
      document.writeln(bpleader+'trav/cruise_2008/">Bahamas (2008)</a><br>');
      document.writeln(bpleader+'trav/biloxi/">Biloxi (2007)</a><br>');
      document.writeln(bpleader+'trav/cruise_mmvi/">Caribbean&nbsp;cruise&nbsp;(2006)</a><br>');
      document.writeln(bpleader+'trav/china2006/">China (2006)</a><br>');
      document.writeln(bpleader+'trav/france">France (1995)</a><br>');
      document.writeln(bpleader+'trav/rudolstadt/">Germany (1995)</a><br>');
      document.writeln(bpleader+'trav/gowanda">Gowanda (1996)</a><br>');
      document.writeln(bpleader+'trav/hongkong">Hong&nbsp;Kong&nbsp;(1992)</a><br>');
      document.writeln(bpleader+'trav/israel/">Israel (1997)</a><br>');
  //    document.writeln(bpleader+'trav/pottsville/">Pottsville (1979)</a><br>');
      document.writeln(bpleader+'trav/">visited&nbsp;places list</a><br>');
      document.writeln(bpleader+'trav/churches">visited&nbsp;churches&nbsp;list</a><br>');
    document.writeln('</span>');
    document.writeln('</span>');
  }
  document.writeln('|');

                    /* pictures */

  if ( bp.length )
    document.writeln('<span class="amenu" id="m_pix" onmouseover=\'show_it("sm_pix")\' onmouseout=\'hide_it("sm_pix")\'>');
  document.writeln(bpleader+'pix/">pix</a>');
  if ( bp.length )
  {
      document.writeln('<span class="submenu" id="sm_pix">');
      document.writeln(bpleader+'pix/animals/">animals</a><br>');
      document.writeln(bpleader+'pix/atlanta/">Atlanta</a><br>');
      document.writeln(bpleader+'pix/china/">Chinese landmarks</a><br>');
      document.writeln(bpleader+'pix/nature/">natural scenery</a><br>');
      document.writeln(bpleader+'pix/lawrenceville/">Lawrenceville</a><br>');
      document.writeln(bpleader+'pix/vehicles/">vehicles</a><br>');
      document.writeln(bpleader+'pix/disney_magic/"><i>M/S Disney Magic</i></a><br>');
      document.writeln(bpleader+'pix/disney_wonder/"><i>M/S Disney Wonder</i></a><br>');
      document.writeln(bpleader+'trav/">&nbsp;also&nbsp;see&nbsp;various&nbsp;travels</a><br>');
      document.writeln('<a href="https://www.cleverness.us/gallery/">Private&nbsp;gallery&nbsp;(credentials&nbsp;required)</a><br>');
    document.writeln('</span>');
    document.writeln('</span>');
  }
  document.writeln('|');

  if ( 1 /* document.location.pathname.indexOf('/play/')>=0 */)
   {
    if ( bp.length )
      document.writeln('<span class="amenu" id="m_acting" onmouseover=\'show_it("sm_acting")\' onmouseout=\'hide_it("sm_acting")\'>');
    document.writeln(bpleader+'play/">acting</a>');
    if ( bp.length )
    {
      document.writeln('<span class="submenu" id="sm_acting">');
        document.writeln(bpleader+'play/">Curtis\'s acting overview</a><br>');
        document.writeln(bpleader+'play/ot20min">The&nbsp;Com<span>plete&nbsp;Hi</span>story&nbsp;of&nbsp;the&nbsp;Old&nbsp;Testament&nbsp;in&nbsp;Twe<span>nty&nbsp;Minu</span>tes</a><br>');
        document.writeln(bpleader+'trav/gowanda#tax_collector">The Tax Collector</a><br>');
        document.writeln(bpleader+'play/bethlehem_inn">The Bet<span>hleh</span>em Inn</a><br>');
        document.writeln(bpleader+'play/xglory">Crossroads to Glory</a><br>');
        document.writeln(bpleader+'play/incidents">The Ga<span>lile</span>an&nbsp;Inci<span>den</span>ts (and auxiliary plays)</a><br>');
        document.writeln(bpleader+'play/joebob/">Joe Bob</a><br>');
        document.writeln(bpleader+'play/montley">The Mys<span>tery of Mo</span>ntley\'s Manor</a><br>');
        document.writeln(bpleader+'play/cheap">Chea<span>per by the Do</span>zen</a><br>');
        document.writeln(bpleader+'play/christmas_carol">A Christmas Carol</a>');
      document.writeln('</span>');
      document.writeln('</span>');
    }
    document.writeln('|');
   }
 
  if ( bp.length )
    document.writeln('<span class="amenu" id="m_misc" onmouseover=\'show_it("sm_misc")\' onmouseout=\'hide_it("sm_misc")\'>');
  document.writeln ( bpleader+'misc/">misc.</a>' );
  if ( bp.length )
  {
    document.writeln('<span class="submenu" id="sm_misc">');
      document.writeln(bpleader+'misc/partiless_christmas">Bimillennial&nbsp;Christmas&nbsp;celebrations&nbsp;whacked&nbsp;everywhere</a><br>');
      document.writeln(bpleader+'misc/timesheet">Cortez versus the timesheet</a><br>');
      document.writeln(bpleader+'misc/luke_greg/">Luke and Greg conquer Florida</a><br>');
      document.writeln(bpleader+'misc/jimmy_carter">thoughts on seeing Jimmy Carter</a><br>');
      document.writeln(bpleader+'misc/vsi_name_game">VSI name explanation</a><br>');
      document.writeln(bpleader+'misc/dekalb_officials">DeKalb County government personalities</a><br>');
//      document.writeln(bpleader+'prog/parnas.html">Strengths&nbsp;and&nbsp;weaknesses&nbsp;of&nbsp;cycle-free&nbsp;software&nbsp;and&nbsp;the&nbsp;Willie&nbsp;kernel</a><br>');
//      document.writeln(bpleader+'misc/great_kernel.html">The Great Kernel:  Willie under Windows 2000</a><br>');
      document.writeln(bpleader+'misc/si">International System of Units (SI) cheat sheet</a><br>');
//      document.writeln(bpleader+'ohbc/">Oak Hill Baptist Church</a><br>');
      document.writeln(bpleader+'lang/">English language</a><br>');
      document.writeln(bpleader+'prog/">computer programming</a><br>');
      document.writeln(bpleader+'misc/test">web test information</a>');
      
//      document.writeln(bpleader+'lang/subjunctive_mood.html">The subjunctive mood</a><br>');
//      document.writeln(bpleader+'lang/apostrophe.html">Using apostrophes correctly</a><br>');
      
    document.writeln('</span>');
    document.writeln('</span>');
  }
  document.writeln('|');
  if ( show_reference_menu )
  {
    if ( bp.length )
      document.writeln('<span class="amenu" id="m_reference" onmouseover=\'show_it("sm_reference")\' onmouseout=\'hide_it("sm_reference")\'>');

    document.writeln(bpleader+'reference.html">reference</a>' );
    if ( bp.length )
    {
      document.writeln('<span class="submenu" id="sm_reference">');
        document.writeln(bpleader+'writ/dekalb_officials">DeKalb County government personalities</a><br>');
        document.writeln(bpleader+'great_kernel">The Great Kernel:  Willie under Windows 2000</a><br>');
        document.writeln(bpleader+'si">International System of Units (SI) cheat sheet</a><br>');
        document.writeln(bpleader+'ohbc/">Oak Hill Baptist Church</a><br>');
        document.writeln(bpleader+'parnas">Strengths&nbsp;and&nbsp;weaknesses&nbsp;of&nbsp;cycle-free&nbsp;software&nbsp;and&nbsp;the&nbsp;Willie&nbsp;kernel</a><br>');
        document.writeln(bpleader+'lang/subjunctive_mood">The subjunctive mood</a><br>');
        document.writeln(bpleader+'lang/apostrophe">Using apostrophes correctly</a><br>');
        document.writeln(bpleader+'test">Web test information</a>');
      document.writeln('</span>');
      document.writeln('</span>');
    }
    document.writeln('|');
  }

  if ( show_links_menu )
  {
    if ( bp.length )
      document.writeln('<span class="amenu" id="m_links" onmouseover=\'show_it("sm_links")\' onmouseout=\'hide_it("sm_links")\'>');
    document.writeln(bpleader+'misc/links">links</a>');
    if ( bp.length )
    {
      document.writeln('<span class="submenu" id="sm_links">');
      document.writeln(webleader+'https://www.cleverness.us/curtis/">Secure&nbsp;version&nbsp;Curtis\'s&nbsp;Parlour</a><br>');
      document.writeln(webleader+'http://webmail.cleverness.us">cleverness.us&nbsp;web&nbsp;mail</a><br>');
      document.writeln(webleader+'http://www.thegoodnews.org/">the Good News</a><br>');
      document.writeln(webleader+'http://www.shakespearetavern.com/">Shakespeare&nbsp;Tavern&nbsp;(Atlanta)</a><br>');
      document.writeln(webleader+'http://validator.w3.org/">HTML validator</a><br>');
      document.writeln(webleader+'http://www.afn.org/~afn38637/">Hugh\'s page</a><br>');
      document.writeln(webleader+'http://www.mindspring.com/~christianrebel/">The&nbsp;Christian&nbsp;Rebel&nbsp;(Larry)</a><br>');
      document.writeln('</span>');
      document.writeln('</span>');
    }
    document.writeln('|');
  }

  if ( bp.length )
    document.writeln('<span class="amenu" id="m_me" onmouseover=\'show_it("sm_me")\' onmouseout=\'hide_it("sm_me")\'>');
  document.writeln(bpleader+'me/">about&nbsp;me</a>');
  if ( bp.length )
  {
    document.writeln('<span class="submenu" id="sm_me">');
      document.writeln(bpleader+'me/">letter&nbsp;from&nbsp;me&nbsp;to&nbsp;you</a><br>');
      document.writeln(bpleader+'me/curriculum_vitae">r&eacute;sum&eacute;&nbsp;(curriculum&nbsp;vit&aelig;)</a><br>');
      document.writeln(bpleader+'me/contact">contact me</a><br>');
      document.writeln(bpleader+'play/">acting and theatre</a><br>');
      document.writeln(bpleader+'misc/links">my favourite links</a><br>');
      // document.writeln(bpleader+'me/genealogy">genealogy information</a><br>');
    document.writeln('</span>');
    document.writeln('</span>');
    if ( document.getElementById('m_home').offsetTop !=
         document.getElementById('m_me').offsetTop )
    {
	/* the menu splits lines -- one last hope is to skip floated images
	    (applicable to main page but not many others) */
	document.getElementById ( 'cmsmenubar' ).style.clear = 'both';
        // menu.style.clear = 'both';
    }
    locate_it ( 'home' );
    locate_it ( 'poetry' );
    locate_it ( 'travels' );
    locate_it ( 'pix' );
    locate_it ( 'acting');
    locate_it ( 'misc' );
    if(show_reference_menu) locate_it( 'reference' );
    if(show_links_menu) locate_it ( 'links' );
    locate_it ( 'me' );
  }
}

make_cmsmenu();
