//  menu.js
//
//  Code for building and operating the menus.
//------------------------------------------------------

var mm_current;          // currently-selected main menu item
var sm_current;          // currently_displayed sub menu
var mm_TimerId;          // id of main menu timer
var ids_main = [];       // main menu ids, indexed by main menu strings
var ids_main_sub = [];   // ids of each main menu item's sub_menu, indexed by main menu strings
var ids_sub  = [];       // sub-menu ids, indexed by sub-menu strings

// get domain component of URL from browser
var domain = window.location.hostname;

// make up URL prefixes for menu items. 
var pre = "http://" + domain + "/pgs/";
if (domain == "testvdgsa.org") {
  // testbed doesn't do https
  var secure_pre = "http://" + domain + "/pgs/"
} else {
  var secure_pre = "https://" + domain + "/pgs/"
}

/* Main menu definition. Note that each main menu item has either a next_menu or a link, not both. Sub-menus have */
/* only links - we don't have third-level menus.                                                                  */
  
var title0     = ["ABOUT US", "RESOURCES", "MUSIC", "NEWS", "GRANTS &amp; PROGRAMS", "CONCLAVE", "VIOL WORLD", "SEARCH", "HOME"];
var next_menu0 = ["1", "2", "3", "4", "5", "6", "7", "", ""];
var link0      = ["",  "",  "",  "",  "",  "",  "",  "/search/scripts/search.cgi", "/index.html"];
  
/* Sub-menu 1 */
var title1     = ["Letter from the President",
                  "About the Society", "About the Viol", "Membership", "Donate to the VdGSA", "Points of Contact", "Setting up a Chapter", 
                  "Area Reps and Chapters", "Member Universities and Organizations"];
/* The link to life members is removed until we are ready to go ahead with it */
//                "Area Reps and Chapters", "Member Universities and Organizations", "Life Members"];
var link1      = [pre+"welcome.html",
                  pre+"about.html", pre+"the_viol.html", secure_pre+"member.html", pre+"support.html", pre+"who.html", pre+"chapter.html", 
                  pre+"reps.html", pre+"school.html"];
/* The link to life members is removed until we are ready to go ahead with it */
//                pre+"reps.html", pre+"school.html", ppre+ "life.html"];

/* Sub-menu 2 */
var title2     = ["Videos", "Members' Area", "Viol Care", "VdGSA Publications", "Research and Scholarship"];
var link2      = [pre+"video.html", pre+"MembersArea/", pre+"violcare.html", pre+"pubs.html", pre+"research/research.html"];
/* The link to ask your viol teacher is removed until we are ready to go ahead with it */
//  var title2     = ["Videos", "Members' Area", "Viol Care", "VdGSA Publications", "Ask Your Viol Teacher"];
//  var link2      = [pre+"video.html", pre+"MembersArea/", pre+"violcare.html", pre+"pubs.html", pre+"askyour.html"];

/* Sub-menu 3 */
var title3     = ["Printed Music", "Microfilm Catalog", "New Music Catalog"];
var link3      = [pre+"music_1.html", pre+"mfilm.html", pre+"nm/catalog_intro.html"];

/* Sub-menu 4 */
var title4     = ["Classifieds", "Events and Workshops", "Lost and Found"];
var link4      = [pre+"ads.html", pre+"events.html", pre+"lost.html"];

/* Sub-menu 5 */
var title5     = ["Viol Rentals", "Viol Loan Program", "Circuit Rider Program", "Grants to Regions", "Grants-in-aid", "Chapter start-up Grants", 
                  "Traynor Competition"];
var link5      = [pre+"rental.html", pre + "loans.html", pre+"circuit.html", pre+"grants.html", pre+"gia.html", pre+"chapter_start.html", 
                    pre+"traynor.html"];

/* Sub-menu 6 */
var title6     = ["2012 Conclave", "2011 Conclave", "2010 Conclave", "Conclave Photos", "Self-rating Guide"];
var link6      = [pre+"conclave-2012/2012conclave.shtml", pre+"conclave-2011/conclave2011.shtml", pre+"conclave-2010/conclave2010.shtml", pre+"conclave_photos/conclave_photos.html", pre+"selfrate.html"];

/* Sub-menu 7 */
var title7     = ["Early Music Links",  "The Professionals", "Mailing Lists", "Viol Whimsies", "The StoryChords Project"];
var link7      = [pre+"links.html", pre+"professionals/professionals.html", pre+"mailinglists.html", pre+"fun.html", pre+"story.html"];


function errmsg(msg, url, line) {
  var txt;
  txt = "Javascript error has occurred\n";
  txt += "Error message: " + msg + "\n";
  txt += "URL: " + url + "\n";
  txt += "Line #: " + line ;
  alert(txt);
  return true;
}


window.onerror=errmsg;


//                ----------  Code for building the menu ------------


function build_menu () {
/* This is the subroutine called by each web page to build the menu structure. It contains the
   menu data structure, and calls the necessary routines to build the main and sub menus.
   
   Note that earlier versions of this routine needed a paramater to be passed, and this may
   still be present on some pages. It is ignored in the present version of the routine.
   
   Subroutine calling hierarchy:
   
   build_menu
     build_main_menu
       begin_menu
       end_menu
     build_sub_menu
       begin_menu
       end_menu
*/

  build_main_menu(0, title0, next_menu0, link0);
  build_sub_menu (1, title1, link1);
  build_sub_menu (2, title2, link2);
  build_sub_menu (3, title3, link3);
  build_sub_menu (4, title4, link4);
  build_sub_menu (5, title5, link5);
  build_sub_menu (6, title6, link6);
  build_sub_menu (7, title7, link7);
  
}  // end of build_menu



function build_main_menu(menu_num, title, next_menu, link) {
  var n_items = title.length;
  var item_num;
  var item_text;
  var main_id;
  var sub_id;
  begin_menu(menu_num);
  for (item_num=0; item_num<n_items; item_num++) {
    main_id = "m" + menu_num + "_" + (item_num + 1);
    if (next_menu[item_num] !== "") {
      // This main menu item leads to a sub-menu.
      item_text = '        <div class = "mmIdle" ID ="' + main_id + '"';
      // insert event fields
      sub_id = 'm' + (item_num+1);
      item_text = item_text + 'onMouseOver="mm_mouseover(' +  "'" + main_id + "','" + sub_id + "')" + '"';
      item_text = item_text + 'onMouseOut="mm_mouseout(' +  "'" + main_id + "','" + sub_id + "')" + '"';
    } else {
      sub_id = "";
    }
    if (link[item_num] !== "") {
      // This main menu item is just a link to another page.
      item_text = '        <div class = "mmlIdle" ID ="' + main_id + '"';
      // insert link
      item_text = item_text + 'onClick="' + "location.href='" + link[item_num] + "'" + '">' + title[item_num] ;
    } else {
      item_text = item_text + ">";
      // no link, just insert text
      item_text = item_text + title[item_num];
    }
    item_text = item_text + "</div>";
    document.writeln(item_text);
    // save main menu id and corresponding sub_menu id in arrays addressed by menu text
    ids_main[title[item_num]] = main_id;
    ids_main_sub[title[item_num]] = sub_id;
  }  
  end_menu();
  
} // end build_main_menu



function build_sub_menu(menu_num, title, link) {
  var n_items = title.length;
  var item_num;
  var item_text;
  var id;
  begin_menu(menu_num);
  for (item_num=0; item_num<n_items; item_num++) {
    id = "m" + menu_num + "_" + (item_num + 1);
    item_text = '        <div class = "smIdle" ID ="' + id + '" ';
    if (item_num == 0) {
      item_text = item_text + 'style="border-top:0px" ';
    }
    if (link[item_num] !== "") {
      // insert link
//      item_text = item_text + 'onClick="' + "location.href='" + link[item_num] + "'" + '" ';
      item_text = item_text + 'onClick=    "sm_click(' +  "'"  + link[item_num] + "')" + '" ';
      item_text = item_text + 'onMouseOver="sm_mouseover(' +  "'"  + id + "')" + '" ';
      item_text = item_text + 'onMouseOut="sm_mouseout(' +  "'"  + id + "')" + '" ';
      item_text = item_text + '>' + title[item_num] ;
    } else {
      // no link, just insert text
      item_text = item_text + '>' + title[item_num];
    }
    item_text = item_text + "</div>";
    document.writeln(item_text);
    // save id in array addressed by menu text
    ids_sub[title[item_num]] = id;
  }  
  end_menu();
  
} // end build_sub_menu


function begin_menu(menu_num) {
  var id = "m" + menu_num;
  var item_text;
  document.writeln();
  if (menu_num === 0) {
    item_text = '      <div class="mainmenu" id="' + id + '"';
  } else {
    item_text = '      <div class="submenu" id="' + id + '"';
  }
  item_text = item_text + ' onMouseOver="cc(1)" onMouseOut="cc(0)">';
  document.writeln(item_text);

}  // end begin_menu




function end_menu() {
  document.writeln('      </div>');

}  // end end_menu




//                ----------  Code for responding to menu activity ------------

function cc(n) {
  // Change cursor style when entering or leaving menu
  if (n === 0) { document.body.style.cursor="default";}
  if (n === 1) { document.body.style.cursor="pointer";}
} // end cc









/* 

                                          Sub-menu show/hide logic follows
       
 It works like this: When we hover over a main-menu item which has a sub-menu defined, the associated
 sub-menu is displayed. When we leave the main-menu item, a timer is started. From here on there are
 3 possibilities:
 
 1. If we enter a new main-menu item, the timer is cancelled, the previous sub-menu is hidden and 
    the new sub-menu is shown.
 2. If we enter the sub-menu currently being displayed, the timer is cancelled.
 3. If we leave the main menu, the timer eventually fires. In the timer routine, we clear the sub-menu 
    if any is visible.
 
*/



function mm_mouseover(main_id, sub_id) {
  // Entering a main menu item
  // Cancel timer, if any.
  if (mm_TimerId) {clearTimeout(mm_TimerId)}
  
  // Hide previous sub-menu (if any)
  if (sm_current) {sm_current.style.display = "none";}
  
  // Display new sub-menu (if any)
  if (sub_id) {
    sm_current = document.getElementById(sub_id);
    if (sm_current) {
      sm_current.style.display="inline"
    } else {
      alert("Oops nothing by id " + sub_id);
    }
  } else {
    sub_id = 0;
  }
  
}  // end mm_mouseover


function mm_mouseout() {
  // Leaving a main menu item. 
  // Start the timer.
  mm_TimerId = setTimeout("mm_timeout()", 50)

}  // end mm_mouseout


function sm_mouseover() {
// Entering a sub-menu item (?? This should be the sub-menu, not the item)
  // Cancel timer, if any.
  if (mm_TimerId) {clearTimeout(mm_TimerId)}

}  // end sm_mouseover


function sm_mouseout() {
// Leaving a sub-menu item
  // Start the timer
  mm_TimerId = setTimeout("mm_timeout()", 50)

}  // end sm_mouseover


function sm_click (new_url) {
// Clicked on a sub-menu item
  // Hide the visible sub-menu, if any. If we don't do this, then
  // the sub-menu remains visible if we use the "back" button from the 
  // "not-found" page. Yuck.
  if (sm_current) {
    sm_current.style.display = "none";
    sm_current = 0
    }
  //go to the new page
  location.href = new_url;

}  // end sm_click


function mm_timeout() {
  // main menu timer has expired. 
  // Hide the visible sub-menu, if any.

  if (sm_current) {
    sm_current.style.display = "none";
    sm_current = 0
    }

} // end mm_timeout



function select_menu(main_text, sub_text) {

/* This function selects a main menu item. The menu item is specified by the text appearing in it.    */
/* This function is called from all web pages to display the menu in the desired state for each page. */

var id_main;
var id_sub_menu;
var id_sub_menu_item;

// Get ids for the desired menu items

id_main = ids_main[main_text];
id_sub  = ids_sub[sub_text];

// Select main menu item

mm_current = document.getElementById(id_main);
if (id_sub) {
  // There is a sub-menu item associated with this main menu item.
  // Highlight the main menu item and show a red arrow
  mm_current.className = "mmSelected"
} else {
  // There is no sub-menu item associated with this main menu item.
  // Highlight the main menu item, but no arrow.
  mm_current.className = "mmSelected_nosub"
}

// Select sub menu item

id_sub_menu_item = document.getElementById(id_sub);
if (id_sub_menu_item) {
  id_sub_menu_item.className = "smSelected"
}

}  // end select_menu




