﻿var DropMenuDelay = 300;
var DropMenus = new Array();

function DropMenu(parentElementId, pos)
{
  this.ParentElement = document.getElementById(parentElementId);
  this.Position = pos;
  this.table = null;
  this.DropMenuTimeOut = null;
  
  if (this.ParentElement)
  {
    this.table = document.createElement('TABLE');
    this.table.className = 'DropMenu';
    this.table.style.display = 'none';
    this.table.id = this.ParentElement.id + '_DropMenu';
    this.table.cellPadding = "0";
    this.table.cellSpacing = "0";
    this.table.border = "0";
    
    document.body.appendChild(this.table);
    
    this.table.DropMenu = this;
    this.ParentElement.ChildMenu = this;
    
    AttachEvent(this.ParentElement, "mouseover", ShowChildMenu);
    AttachEvent(this.ParentElement, "mouseout", HideChildMenu);
    AttachEvent(this.table, "mouseover", ClearDropMenuTimeOut);
    AttachEvent(this.table, "mouseout", HideThisMenu);
    
    DropMenus[DropMenus.length] = this;
  }
}
DropMenu.prototype.AddMenuItem = function(text, url, target)
{
  var child, tr, td;
  
  if (!this.ParentElement)
    return;
  
  if (url)
  {
    child = document.createElement('A');
    child.href = url;
    
    if (target)
      child.target = target;
  }
  else
    child = document.createElement('SPAN');
  
  child.innerHTML = text;
  
  tr = this.table.insertRow(this.table.rows.length);
  td = tr.insertCell(0);
  
  td.appendChild(child);
  td.id = this.table.id + '_' + (this.table.rows.length);
};
DropMenu.prototype.Show = function()
{
  if (this.DropMenuTimeOut)
    clearTimeout(this.DropMenuTimeOut);
  
  this.DropMenuTimeOut = setTimeout("DoShowMenu('" + this.table.id + "');", DropMenuDelay);
};
DropMenu.prototype.Hide = function()
{
  if (this.DropMenuTimeOut)
    clearTimeout(this.DropMenuTimeOut);
  
  this.DropMenuTimeOut = setTimeout("DoHideMenu('" + this.table.id + "');", DropMenuDelay);
};

function DoShowMenu(TableID)
{
  var parent, DM, bounds, table;
  
  table = document.getElementById(TableID);
  DM = table.DropMenu;
  parent = DM.ParentElement;
  
  bounds = getCoordinates(parent);
  
  table.style.display = '';
  
  switch (DM.Position)
  {
    case "trbr":
      table.style.top = (bounds.y + parent.offsetHeight) + "px";
      table.style.left = (bounds.x + parent.offsetWidth - table.offsetWidth) + "px";
      table.style.right = "auto";
      table.style.bottom = "auto";
      table.style.textAlign = "right";
      break;
    case "tlbl":
      table.style.top = (bounds.y + parent.offsetHeight) + "px";
      table.style.left = bounds.x + "px";
      table.style.right = "auto";
      table.style.bottom = "auto";
      table.style.textAlign = "left";
      break;
    case "tlbr":
      table.style.top = (bounds.y + parent.offsetHeight) + "px";
      table.style.left = (bounds.x + parent.offsetWidth) + "px";
      table.style.right = "auto";
      table.style.bottom = "auto";
      table.style.textAlign = "left";
      break;
    case "tltr":
      table.style.top = bounds.y + "px";
      table.style.left = (bounds.x + parent.offsetWidth) + "px";
      table.style.right = "auto";
      table.style.bottom = "auto";
      table.style.textAlign = "left";
      break;
  }
}
function DoHideMenu(TableID)
{
  document.getElementById(TableID).style.display = 'none';
}
function GetChildMenu(ParentE)
{
  var ParentElement = getNodeFromEvent(ParentE);
  
  while (ParentElement)
  {
    if (ParentElement.ChildMenu)
      return ParentElement.ChildMenu;
    
    ParentElement = ParentElement.parentNode;
  }
  
  return null;
}
function ShowChildMenu(ParentE)
{
  var cm = GetChildMenu(ParentE);
  
  cm.Show();
}
function HideChildMenu(ParentE)
{
  var cm = GetChildMenu(ParentE);
  
  cm.Hide();
}
function HideThisMenu(TableE)
{
  var table = GetTable(TableE);
  
  table.DropMenu.Hide();
}
function ClearDropMenuTimeOut(TableE)
{
  var table = GetTable(TableE);
  
  var DM = table.DropMenu;
  
  var TO = DM.DropMenuTimeOut;
  
  if (TO)
    clearTimeout(TO);
}
function GetTable(TableE)
{
  var table = getNodeFromEvent(TableE);
  
  while (table.tagName != "TABLE")
    table = table.parentNode;
  
  return table;
}
function AttachEvent(elm, eventname, fx, cancelBubble)
{
  cancelBubble = cancelBubble || false;
  
  if (elm.addEventListener)
    elm.addEventListener(eventname, fx, cancelBubble);
  else if (elm.attachEvent)
  {
    elm.detachEvent("on" + eventname, fx);
    elm.attachEvent("on" + eventname, fx);
  }
}
function getNodeFromEvent(e)
{
  e = e || window.event;

  var node = e.target || e.srcElement;

  while (node.nodeType != 1)
    node = node.parentNode;
  
	return node;
}
function getCoordinates(elm)
{
  var coords;
  
  coords = {};
  coords.x = 0;
  coords.y = 0;
  
  while (elm)
  {
    coords.x+=elm.offsetLeft;
    coords.y+=elm.offsetTop;
    
    try
      {elm=elm.offsetParent;}
    catch(x)
      {elm=null;}
  }
  
  return coords;
}

function initNavMenu()
{
 var dmAboutUs = new DropMenu('miAbout', 'tltr'); 

 dmAboutUs.AddMenuItem('ICS Institute', 'http://emsi-ics-services.com/pgs/ics.html');
 dmAboutUs.AddMenuItem('Our Clients', 'http://emsi-ics-services.com/pgs/clients.html');
 dmAboutUs.AddMenuItem('Our Staff', 'http://emsi-ics-services.com/pgs/staff.html');
 dmAboutUs.AddMenuItem('Photos', 'http://emsi-ics-services.com/album/index.html');
 dmAboutUs.AddMenuItem('Newsletter', 'http://emsi-ics-services.com/pgs/resources.html');
 dmAboutUs.AddMenuItem('EMSI Brochure', 'http://emsi-ics-services.com/pgs/resources.html');

}

AttachEvent(window, "load", initNavMenu, false);

