// Nov. 21/2003 - New script COMMENTS.JS handles commentaries link menu - csk
// Feb. 29/2004 - Summaries/Commentaries - production - mtm
// Mar. 09/2004 - Down arrow for multiple commentaries - csk
// Apr. 27/2004 - Avoid JS error loading in single frame - mtm
// Oct. 14/2005 - Position persistence - mtm

// ******** General Commentaries menu functions ********

function comments_general() // general comments creation function
{
 if ("undefined" != typeof COMMENTS){
   top.numcom=0; // default no down arrow
   if (COMMENTS.length > 0){
     top.numcom = top.numcom + COMMENTS.length; // may have down arrow
     comments_table(); // create table in memory
     comments_properties(); // assign commenu properties
   } // have commentaries for this case
 } // if COMMENTS defined
} // end comments_general

function comments_table() // create comments table in memory
{
 commatrix = new Object(); // global object no var

 commatrix['IMAG'] = "Imaginary&nbsp;Commentaries&nbsp;Source";
 commatrix['MINE'] = "My&nbsp;Madeup&nbsp;Commentaries";
 commatrix['TCT']  = "Three&nbsp;Character&nbsp;Test&nbsp;Commentaries";

} // end comments_table

function comments_properties() // HTML properties for commenu
{
   var pt = parent.top;
   if (!pt) return;
   var ptr = pt.RIGHT;
   if (!ptr) return;	
   var rd = parent.top.RIGHT.document;
   var cm=MM_findObj("commenu",rd); // find DIV in DOM or ns4.x
   if (cm) {
      var z = comments_menu(); // compose commenu HTML string
      if (top.makecomm == true){
        if (parent.top.QLBrowser.ns4){
          top.netcomtab = 0;
          top.netcommenu = 0;
          cm.onmouseout=com_net_hide_verify;
          cm.onmouseover=com_net_pass_down;
          var menulayer = rd.layers["commenu"];
          var md=menulayer.document;
          md.write(z); // ns4.x document write
          md.close(); // close or will append
        } // if ns4.x object
        else{
          cm.onmouseout=com_hide_menu;
          cm.onmouseover=com_set_in;
          cm.onmousemove=com_set_in;
          cm.innerHTML = z; // DOM document write
        } // else DOM object
      } // if making menu
   } // if commenu found
} // end comments_properties

function comments_menu() // make commenu for DOM or ns4.x
{
 var z = '<TABLE cellspacing=0 ';
 if (parent.top.QLBrowser.ns4){
   z += 'cellpadding=2 ';
 } // if ns4.x foible
 else{
   z += 'cellpadding=0 ';
 }
 z += 'border=0>';

 for (i=0; i< COMMENTS.length; i++) {
   var e = COMMENTS[i]; // unparsed array string
   var comindex = e.indexOf(','); // index of comma in string
   var a = e.slice(0,comindex); // tabcode before comma, no validation
                                // necessary
   a = a.replace(/\s+/g,'');    // strip blanks
   var b = e.slice(comindex+1); // tabdoc after comma, no validation done
   var c = commatrix[a]; // tabstring from data structure
   if ("undefined" != typeof c){ // if found commentary title string
     var d = comment_entry(a,b,c); // write table row HTML
     z += d; // add row to commenu
     if (top.makecomm != true){ // if this is first commentary found
       top.makecomm = true; // set flag that making menu
     } // if flag not set yet
   } // if access code in commatrix
 } // for each COMMENTS element

 z += '</TABLE>';
 return z;
} // end comments_menu

function comment_entry(tabcode,tabdoc,tabstring) // write each commenu entry
{
 var MYPFX = CMDPFX.replace(/QL002/ig,'QLCMD');
 var d = '<TR><TD class="smenu"><A class="smenu" HREF="';
 d += MYPFX + 'C=CS+' + tabcode + '+' + tabdoc;
 d += '" TARGET="_top"';
 if ("undefined" != typeof top.record_pos)
   d += ' onclick="top.record_pos();"';
 d +='>';
 d += tabstring + '</A></TD></TR>';
 return d; // add string to table build
} // end comment_entry


// ******** DOM Commentaries menu functions ********

function com_show_menu() // DOM comtab onmouseover
{
 var d=parent.top.RIGHT.document;
 var c = MM_findObj("commenu",d);
 if (c) {
  if (c.style.visibility != "visible") {
   var q=parent.top.COMMANDS.document;
   var e = get_anchor(q,"commentaries");
   if (e) {
    var adjust = -1;
    if (top.QLBrowser.ie5mac) adjust = 2; // ie5mac prints right
    var fx = e.fx + adjust;

    var s=c.style;
    var addon="px"
    var showon="visible";

    top.comonmenu = '1';

    var topscreen = d.body.scrollTop;

    s.top= topscreen + addon;
    s.left=fx + addon;
    s.visibility=showon;

    var r = MM_findObj("comtab",q);
    if (r) r.style.backgroundColor="blue";

   } // if found the anchor
  } // if not visible
 } // if commenu
} // end com_show_menu

function com_hide_menu(e) // commenu onmouseout
{
 var f = "none";
 var e = (e) ? e :((event) ? event : null);
 if (e) {
  var trg=(e.relatedTarget) ? e.relatedTarget :
          ((e.toElement) ? e.toElement : null);
  if (trg) {
    var f = trg.className;
    while (!f) {
      if (trg.parentNode) {
        trg = trg.parentNode;
        f=trg.className;
      } // if parent node
      else
        f = "Not smenu";
    } // while f is undefined
    if (f == "smenu") return;
  } // if found target trg
 }  // if found event e
 else return;

 top.comonmenu='';
 com_my_hide_menu();
} // end com_hide_menu

function com_set_in() // commenu onmouseover, onmousemove
{
  top.comonmenu='1';
} // end set_in

function commentaries_mm(e) // comtab onmousemove
{
 var f = "none";
 if (parent.top.COMMANDS.document.event)
  var ie_e = parent.top.COMMANDS.document.event;
 else
  var ie_e = null;

 var e = (e) ? e : ie_e ;
 if (e) {
  var y = (e.y) ? e.y : e.layerY ;
  if (y<1) {
   top.comonmenu = '';
   com_my_hide_menu();
   return;
  } // if (y<1)
 } // if e
 com_show_menu();
} // end commentaries_mm

function commentaries_hide_menu(e) // comtab onmouseout
{
 var f = "none";
 if (parent.top.COMMANDS.document.event)
  var ie_e = parent.top.COMMANDS.document.event;
 else
  var ie_e = null;

 var e = (e) ? e : ie_e ;
 if (e) {
  var y = (e.y) ? e.y : e.layerY ;
   if (y < 1) {
     com_really_hide_menu();
     return;
   }
 }
 if (e) {
  var trg=(e.relatedTarget) ? e.relatedTarget :
          ((e.toElement) ? e.toElement : null);
  if (trg) {
    f = trg.className;
    while (!f) {
      if (trg.parentNode) {
        trg = trg.parentNode;
        f=trg.className;
      } // if parent node
      else
        f = "Not comtab";
    } // while f undefined
  } // if found target trg
 } // if found event e
 else { return; }

 if (f == "comtab") return; // no exit if same class
 top.comonmenu = '';
 setTimeout("com_my_hide_menu();", 500);
} // end commentaries_hide_menu

function com_my_hide_menu()
{
 if (top.comonmenu)
  if (top.comonmenu == '1')
    return;
 com_really_hide_menu();
} // end com_my_hide_menu

function com_really_hide_menu()
{
 var c = MM_findObj("commenu",parent.top.RIGHT.document);
 if (c) {
  if(c.style.visibility == "visible"){
   c.style.visibility = "hidden";
   var r = MM_findObj("comtab",parent.top.COMMANDS.document);
   if (r) r.style.backgroundColor="green";
  } // if visible
 } // if c
} // end com_really_hide_menu


// ******** Netscape 4.x Commentaries menu functions ********

function com_net_show_test(e) // netscape 4.x comtab onmouseover
{
 top.netcomtab = 1;
 if (top.netcommenu == 1){
   top.netcommenu = 0;
 } // came from below
 else{
   com_net_show_final();
 } // came from outside
} // com_net_show_test

function com_net_show_final() // netscape 4.x menu display
{
 var rd = parent.top.RIGHT.document;
 if (rd) var cl = rd.layers["commenu"]; else var cl= NULL;
 if (cl) {
   var ld=parent.top.COMMANDS.document.LNKS.document;

   if (ld) { // Have LNKS layer document
     var lda = ld.anchors;
     var ldal = lda.length;
     for (i=0; i<ldal; i++){ // anchor loop code
       if (lda[i].name == 'commentaries'){ // open each anchor
         var m = lda[i];
       } // if commentaries
     } // for each named anchor

     if (m) { // have summaries anchor
       if (!FR_INT){
	 ld.images['comimage'].src = "../img/comxeimage.gif"; // blue English
       }
       else{
	 ld.images['comimage'].src = "../img/comxfimage.gif"; // blue French
       }
       var fx = m.x; // capture x-position of anchor

       var showon = "show"
       var topscreen = parent.RIGHT.pageYOffset; // ns4.x way of doing it
       cl.top= topscreen;
       cl.left=fx;
       cl.visibility=showon;

     } // if found commentaries anchor m
   } // if found LNKS layer document ld
 } // if found commenu layer cl
} // end com_net_show_final

function com_net_hide_test(e) // netscape 4.x comtab onmouseout
{
 if (top.netcommenu != 1){
   com_net_hide_final(); // going outside comtab
 }
} // com_net_hide_test

function com_net_pass_down(e) // netscape 4.x commenu onmouseover
{
  top.netcomtab = 0; // came from above
  top.netcommenu = 1; // have gone below
} // com_net_pass_down

function com_net_hide_verify(e) // netscape 4.x commenu onmouseout
{
 if (top.netcommenu == 1){
   com_net_hide_final();
 } // going outside
} // com_net_hide_verify

function com_net_hide_final() // netscape 4.x confirmed commenu hide
{
 var rd = parent.top.RIGHT.document;
  if (rd) var m = rd.layers["commenu"]; else var m= NULL;
  if (m) { // ns4.x goes invisible
    var showoff = "hide"
    m.visibility=showoff;
    top.netcomtab = 0;
    top.netcommenu = 0;
  } // if ns4.x commenu m

 var ld=parent.top.COMMANDS.document.LNKS.document;
  if (ld) { // Have LNKS layer document
    if (!FR_INT){
      ld.images['comimage'].src = "../img/comeimage.gif"; // green English
    }
    else {
      ld.images['comimage'].src = "../img/comfimage.gif"; // green French
    }
  } // if have LNKS document

} // com_net_hide_final

