  /* --------------------------------------------------------------------- */
  /* meta_card.js                                                          */
  /*                                                                       */
  /* created by: An-Min Kuo for Simplify v4.2                              */
  /* c. 2005 Tomoye Corp.                                                  */
  /* --------------------------------------------------------------------- */

  var g_previous_activated_arrows_img;
  var g_previous_activated_menu_item_id;
  
  function meta_card_arrows_clicked (e, anchor_obj, ko_id)
  {
    var arrows_img = anchor_obj.childNodes[0];
    
    if (arrows_img.src.match(/mc_down_arrows/))
      open_meta_card (e, arrows_img, ko_id);
    else
      close_meta_card (arrows_img);

    return false;
  }

  function open_sub_menu (menu_item_id)
  {
    menu_item               = getObj(menu_item_id);
    menu_item.style.visibility = "visible";
    g_previous_activated_menu_item_id = menu_item_id;
  }

  function open_meta_card (e, arrows_img, ko_id)
  {
    // close any currently open meta-card
    close_meta_card ();

    // update the activator's image to up arrows
    arrows_img.src = "IMAGES/mc_up_arrows_hover.gif";
    g_previous_activated_arrows_img = arrows_img;

    // is this to open a sub-menu?
    if (!is_numeric(ko_id))
    {
      open_sub_menu (ko_id);
      return;
    }

    // this is to open the meta-card
    
    // find the x,y coordinate where the meta-card should show up
    var pos_x = 0;
    var pos_y = 0;
    
    if (document.all) // IE
    {    
      if (document.documentElement && document.documentElement.scrollTop) // IE 6
      {
        horizontal_scroll_offset = document.documentElement.scrollLeft;
        vertical_scroll_offset   = document.documentElement.scrollTop;
      }
      else if (document.body) // older IE
      {
        horizontal_scroll_offset = document.body.scrollLeft;
        vertical_scroll_offset   = document.body.scrollTop;
      }

      pos_x = e.clientX + horizontal_scroll_offset;
      pos_y = e.clientY + vertical_scroll_offset;
    }
    else // FF
    {
      pos_x = e.pageX;
      pos_y = e.pageY;
    }

    // make sure the right hand side of the meta card appears within the screen
    pos_x_max = pos_x + 300;
    if (pos_x_max > document.body.clientWidth)
    {
      pos_x = document.body.clientWidth - 300;
    }
    
    // offset the meta card from the adjusted current mouse position so it's
    // more visible
    pos_x -= 20;
    pos_y += 12;
    
    // create the meta card element
    meta_card_div                = document.createElement('div');
    meta_card_div.id             = "meta_card_div";
    meta_card_div.style.position = "absolute";
    meta_card_div.style.left     = pos_x + "px";
    meta_card_div.style.top      = pos_y + "px";
    meta_card_div.style.zIndex   = "1000";
    meta_card_div.style.width    = "300px";
    meta_card_div.style.border   = "";
    meta_card_div.style.backgroundColor = "white";

    message_html = "<span style=\"font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 10pt;\">Getting summary info...</span>";
    please_wait_message = document.createElement('div');
    please_wait_message.innerHTML = message_html;
    meta_card_div.appendChild (please_wait_message);
    
    myBody=document.getElementsByTagName("body").item(0);
    myBody.appendChild (meta_card_div);
    
    // load the meta card html into the new element
    url = location.href;
    if (url.match("ev[_a-zA-Z0-9]*\.php$"))
    {
      // this is a root page with no KO information specifieds
      url += "?"   +
             "ID=" + ko_id + "_201" + 
             "&"   +
             "ID2=DO_INFO_META_CARD";
    }
    else
    {
      url = url.replace (/ID=[0-9]+/,     "ID=" + ko_id);
      url = url.replace (/ID2=([_A-Z]+)/, "ID2=DO_INFO_META_CARD");  
    }
    loadHTML (url, 'meta_card_div');
  }
  
  function close_meta_card (arrows_img)
  {
    if (arrows_img == null)
    {
      if (g_previous_activated_arrows_img != null)
      {
        // reset the arrows image from the last activation
        previous_activated_arrows_img     = g_previous_activated_arrows_img;
        previous_activated_arrows_img.src = "IMAGES/mc_down_arrows.gif";
      }
    }
    else
    {
      // reset the arrows image from a specified activation
      arrows_img.src = "IMAGES/mc_down_arrows_hover.gif";
    }

    meta_card_div = getObj ("meta_card_div");
    if (meta_card_div)
    {
      parent_node = meta_card_div.parentNode;
      parent_node.removeChild (meta_card_div);
    }

    sub_menu_item = getObj(g_previous_activated_menu_item_id);
    if (sub_menu_item)
    {
      sub_menu_item.style.visibility = "hidden";
    }
  }

  function highlight_meta_card_arrows(arrows_img)
  {
    var matches    = arrows_img.src.match("([_a-zA-Z]+)arrows[_a-zA-Z]*.gif$");
    var up_or_down = matches[1];
    var img_name   = up_or_down + "arrows_hover" + ".gif";

    arrows_img.src = "IMAGES/" + img_name;
  }
  
  function un_highlight_meta_card_arrows(arrows_img)
  {
    var matches    = arrows_img.src.match("([_a-zA-Z]+)arrows[_a-zA-Z]*.gif$");
    var up_or_down = matches[1];
    var img_name   = up_or_down + "arrows" + ".gif";

    arrows_img.src = "IMAGES/" + img_name;
  }
  
  function highlight_close_meta_card_icon(icon_img)
  {
    icon_img.src = "IMAGES/icon.close_meta_card_hover.gif";
  }
  
  function un_highlight_close_meta_card_icon(icon_img)
  {
    icon_img.src = "IMAGES/icon.close_meta_card.gif";
  }
  
  // add event capturing for closing meta cards
  document.onclick = close_meta_cards_when_on_document_clicked;
  
  function close_meta_cards_when_on_document_clicked (e)
  {
    var target_object;
    
    if (!e)
    {
      var e = window.event;
    }
    
    if (e.target)
    {
      target_object = e.target;
    }
    else
    {
      target_object = e.srcElement;
    }
    
    if (target_object.className != "meta_card_link_img")
    {
      close_meta_card();
    }
    
    return;
  }

  function is_numeric (str)
  {
    var numerals         = "0123456789";
    var is_number_so_far = true;
    var a_char;

    for (i=0; (i<str.length) && (is_number_so_far==true); i++) 
    { 
      a_char = str.charAt(i); 

      if (numerals.indexOf(a_char) == -1) 
      {
         is_number_so_far = false;
      }
    }

    return is_number_so_far;
  }

  /* --------------------------------------------------------------------- */
  /* start of DHTML micro API                                              */
  /* --------------------------------------------------------------------- */
  /* 
     note: 2005.06.30
     
     The following block of code is based on the DHTML micro API
       http://www.quirksmode.org/js/dhtmloptions.html
     
     This code has been heavily modified, and only small bits resemble
       the original version.
  */

  var g_req;
  var g_dest;
  
  function getObj(name)
  {
    if      (document.getElementById) { return document.getElementById(name); }
    else if (document.all)            { return document.all[name];            }
    else if (document.layers)         { return document.layers[name];         }
    return false;
  }
  
  function getStyle(name)
  {
    if      (document.getElementById) { return document.getElementById(name).style; }
    else if (document.all)            { return document.all[name].style;            }
    else if (document.layers)         { return document.layers[name];               }
    return false;
  }
  
  function processStateChange()
  {
    if (g_req.readyState == 0){ window.status = "UNINITIALIZED"; }
    if (g_req.readyState == 1){ window.status = "LOADING"; }
    if (g_req.readyState == 2){ window.status = "LOADED"; }
    if (g_req.readyState == 3){ window.status = "INTERACTIVE"; }
    if (g_req.readyState == 4){ window.status = " "; }
    if (g_req.readyState == 4){
      if (g_req.status == 200){
         response = g_req.responseText;
         destinationDiv = document.getElementById(g_dest);
         destinationDiv.innerHTML = response;
      } else {
         window.status = "Error: Status "+g_req.status;
      }
    }
  }
  
  function loadHTML(url, destination)
  {
    g_dest = destination;
    
    g_req = false;
    
    if (window.XMLHttpRequest)
    {
      try
      {
        g_req = new XMLHttpRequest();
      }
      catch (e)
      {
        g_req = false;
      }
    }
    else if (window.ActiveXObject) // IE ActiveX
    { 
      try
      {
        g_req = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          g_req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
          g_req = false;
        }
      }
    }
    
    if (g_req)
    {
      g_req.onreadystatechange = processStateChange;
      g_req.open("GET", url, true);
      g_req.send(null);
    }
  }
  
  /* --------------------------------------------------------------------- */  
  /* end of DHTML micro API                                                */
  /* --------------------------------------------------------------------- */
