/****************************************************

  script required to enable drop down menu on a page
  includes quicklink content

****************************************************/


///////////////////////
//general css functions
///////////////////////


//test if a css element exists before creating a cssElement object
function testIfElement(id)
{
  if (is.nav4)
  {
    if (document.layers[id]) return true;
  }
  else if (is.nav5up)
  {
    if (document.getElementByID (id)) return true;
  }
  else if (is.ie4up)
  {
    if (document.all[id]) return true;
  }
  else return false;
}

//create an object to reference the css element
function cssElement (id)
{
  if (is.nav)
  {
    if (is.nav4)
    {
      this.obj = document.layers[id];
      this.css = this.obj;
      this.doc = this.obj.document;
    }
    else if (is.nav5up)
    {
      this.obj = document.getElementById (id);
      this.css = this.obj.style;
      this.doc = document;
    }
    this.left = this.css.left;
    this.right = this.css.clip.right;
    this.top = this.css.top;
    this.bottom = this.css.clip.bottom;
  }
  else if (is.ie4up)
  {
    this.obj = document.all[id];
    this.css = this.obj.style;
    this.doc = document;
  }
  this.show = cssElementShow;
  this.hide = cssElementHide;
}

//show the element
function cssElementShow ()
{
  this.css.visibility = "visible";
}

//hide the element
function cssElementHide ()
{
  this.css.visibility = "hidden";
}

//reloads the page to reposition elements placed according to widow width/height
//also resolves ns resize bug
function resetLayout()
{
  history.go(0)
}

//////////////////////////
//menu specific components
//////////////////////////


//global variables
var activeMenu = 0;
var isLoaded = false;
var menu1;
var menu2;

//show the menu
function activateMenu (ref)
{
  if(isLoaded)
  {
    if (activeMenu != ref)
    {
      var hideMenu = "menu" + activeMenu;
      if (activeMenu && hideMenu.hide) hideMenu.hide();
      else if (activeMenu) return;
      thisMenu = eval("menu" + ref);
      activeMenu = ref;
      thisMenu.show()
    }
    if (is.ie4up) window.event.cancelBubble = true;
  }
}

//hide the menu when the mouse moves of the anchor
function killMenu (e)
{
  //check if a menu is active
  if (activeMenu)
  {
    thisMenu = eval("menu" + activeMenu);
    if (is.nav)
    {
      menuX1 = thisMenu.left;
      menuX2 = menuX1 + thisMenu.right;
      menuY1 = thisMenu.top;
      menuY2 = menuY1 + thisMenu.bottom;
      if (e.pageX < menuX1 || e.pageX > menuX2 || e.pageY > menuY2)
      {
        thisMenu.hide();
        activeMenu = 0;
      }
    }
    else
    {
      thisMenu.hide();
      activeMenu = 0;
    }
  }  
}

//create the objects to reference the css elements
//and to respond to the mouse moving off the anchor
function menuInit ()
{
  //create the quicklink object
  menu1 = new cssElement('menu1Div');
  //create the sectional nav object only if it is included in the HTML
  if (testIfElement('menu2Div')) menu2 = new cssElement('menu2Div');
  if (is.nav)
  { 
    document.captureEvents(Event.MOUSEMOVE); 
    document.onmousemove = killMenu;
  }
  else
  {
    document.onmouseover = killMenu;
  }
}

//write the style tag to the document
function menuWrite(ypos)
{
  //compensate for the offset in ie on the mac
  var offSet = (is.mac && is.ie4) ? 9 : 0;

  //position the quicklink relative to the center of the page
  var x1 = (clientWidth < 777) ? 0 : parseInt(clientWidth/2) -388 + offSet;

  //position the sectional navigation relative to the center of the page
  var x2 = (clientWidth < 777) ? 194 + offSet: parseInt(clientWidth/2) - 194 + offSet;

  //set the location of the dropdown relative to the top of the page
  // (100 for marketing, 70 for product)
  y = ypos;

  //write the style definition to the page
  //include both possibilites even if the page contains no sectional navigation
  //because the element cannot be tested for before the page loads
  var str = '<style type="text/css">\n'+
  '  #menu1Div  {visibility: hidden; position: absolute; left: ' + x1 +'px; top: ' + y + 'px;}\n' +
  '  #menu2Div {visibility: hidden; position: absolute; left: ' + x2 +'px; top: ' + y + 'px;}\n' +
  '</style>'
  document.write(str);

  //write the quicklink content to the page
  if (is.ver4up)
  {
    with (document)
    {
      writeln('  <div id = "menu1Div" onmouseover = "activateMenu(\'1\')">');
      writeln('    <table bgcolor = "#cccc99" width = "195" cellpadding = "0" cellspacing = "0" border = "0">');
      writeln('      <tr>');
      writeln('        <td colspan="2"><img src="../kalert/images/1x1_white.gif" width="195" height="1"></td>');
      writeln('      </tr>');
      writeln('      <tr>');
      writeln('        <td width="3"><img src="../kalert/images/1x1_transparent.gif" width="3" height="1"></td>');
      writeln('        <td><a class = "menuList" onmouseover = "activateMenu(\'1\');" href = "http://www.standardandpoors.com"><b>standardandpoors.com home</b></a></td>');
      writeln('      </tr>');
      writeln('      <tr>');
      writeln('        <td colspan="2"><img src="../kalert/images/1x1_white.gif" width="195" height="1"></td>');
      writeln('      </tr>');
      writeln('    </table>');
      writeln('  </div>');
    }
  }
}
