if (document.all)    {n=0;ie=1;ns6=0;fShow="visible";fHide="hidden";}
if (document.getElementById&&!document.all)    {n=0;ie=0;ns6=1;fShow="visible";fHide="hidden";}
if (document.layers) {n=1;ie=0;ns6=0;fShow="show";fHide="hide";}

//Top Nav bar script v2.1- http://www.dynamicdrive.com/dynamicindex1/sm/index.htm

opr6=ie&&navigator.userAgent.indexOf("Opera")!=-1

window.onerror=new Function("return true")
////////////////////////////////////////////////////////////////////////////
// Function Menu()                                                        //
////////////////////////////////////////////////////////////////////////////
rightX = 0;
function Menu()
{

  this.addItem    = addItem;
  this.addSubItem = addSubItem;
  this.showMenu   = showMenu;
  this.mainPaneBorder = 0;
  this.subMenuPaneBorder = 0;
  
  itemCount = 0;
  
  menuMutexUsed = false;

  lastMenu = null;
  
  menuTopLeft = [0,0];
  menuBottRight = [0,0];

  submenuTopLeft = [0,0];
  submenuBottRight = [0,0];

  HTMLstr = "";
//  HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->";
  HTMLstr += "";
  if (ie||ns6) HTMLstr += "<div id='MainTable'>";
//  if (n)  HTMLstr += "<layer name='MainTable'>";
  HTMLstr += "<table class=\"mainNavigationTable\">";
  HTMLstr += "<tr>";
  if (n) HTMLstr += "<td nowrap=\"nowrap\">&nbsp;";
  HTMLstr += "<!-- MAIN MENU STARTS -->";
  HTMLstr += "<!-- MAIN_MENU -->";
  HTMLstr += "<!-- MAIN MENU ENDS -->";
  if (n) HTMLstr += "</td>";
  HTMLstr += "</tr>";
  HTMLstr += "</table>";
  HTMLstr += "";
  HTMLstr += "<!-- SUB MENU STARTS -->";
  HTMLstr += "<!-- SUB_MENU -->";
  HTMLstr += "<!-- SUB MENU ENDS -->";
  HTMLstr += "";
  if (ie||ns6) HTMLstr+= "</div>";
//  if (n)  HTMLstr+= "</layer>";
//  HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->";
}

function addItem(idItem, text, hint, location, altLocation)
{
  if (hint)
  {
    hint = hint.replace("'","\'");
  }
  
  itemCount++;

  var Lookup = "<!-- ITEM "+idItem+" -->";
  if (HTMLstr.indexOf(Lookup) != -1)
  {
    alert(idParent + " already exist");
    return;
  }
  var MENUitem = "";
  MENUitem += "<!-- ITEM "+idItem+" -->";
  
  if (itemCount > 1)
  {
    MENUitem += "<td nowrap=\"nowrap\">";
    MENUitem += "<div class=\"mainNavigationSeparator\">&nbsp;</div>";
    MENUitem += "</td>";
  }
  MENUitem += "<td nowrap=\"nowrap\">";
  MENUitem += "<div id='"+idItem+"' style='position:relative; white-space: nowrap;'>";
  MENUitem += "<a ";
  MENUitem += "class=\"clsMenuItem\" ";
  if (hint != null)
    MENUitem += "title=\""+hint+"\" ";
  if (location != null)
  {
    MENUitem += "href='"+location+"' ";
    MENUitem += "onmouseover=\"hideAll()\" ";
  }
  else
  {
    if (altLocation != null)
      MENUitem += "href='"+altLocation+"' ";
    else
      MENUitem += "href='.' ";
    MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
    //MENUitem += "onclick=\"return false;\" "
  }
  MENUitem += ">";
  MENUitem += "&nbsp;";
  MENUitem += text;
  MENUitem += "</a>";
  MENUitem += "</div>";
  MENUitem += "</td>";

  MENUitem += "<!-- END OF ITEM "+idItem+" -->";
  MENUitem += "<!-- MAIN_MENU -->";

  HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->", MENUitem);
}

function addSubItem(idParent, text, hint, location, linktarget)
{
  var MENUitem = "";
  Lookup = "<!-- ITEM "+idParent+" -->";
  if (HTMLstr.indexOf(Lookup) == -1)
  {
    alert(idParent + " not found");
    return;
  }
  Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
  if (HTMLstr.indexOf(Lookup) == -1)
  {
    MENUitem += "";
    MENUitem += "<div id='"+idParent+"submenu' onmouseout='operahide()' style='position:absolute; visibility: hidden; z-index:100;' class='mainNavigationMenu'>";
    MENUitem += "<table class='mainNavigationSubMenuTable'>";
    MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
    MENUitem += "</table>";
    MENUitem += "</div>";
    MENUitem += "";

    MENUitem += "<!-- SUB_MENU -->";
    HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->", MENUitem);
  }

  Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
  MENUitem = "<tr><td><a href=\""+location+"\" class=\"clsSubMenuItem\" ";
  if (hint)
  {
    MENUitem += "title=\""+hint+"\" ";
  }
  if (linktarget)
  {
    MENUitem += "target=\""+linktarget+"\"";
  }
  MENUitem += ">"+text+"</a><br></td></tr>";
  
  MENUitem += Lookup;
  HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

function showMenu()
{
  //document.write("<textarea>"+HTMLstr+"</textarea>");
  document.write(HTMLstr);
}

////////////////////////////////////////////////////////////////////////////
// Private declaration
function displaySubMenu(idMainMenu)
{
  menuMutexUsed = true;
  
  logIt("Opening submenu for "+idMainMenu);
  
  var menu;
  var submenu;
  
  menu = ie? eval(idMainMenu) : document.getElementById(idMainMenu);
  submenu = ie? eval(idMainMenu+"submenu") : document.getElementById(idMainMenu+"submenu");
  submenuStyle = ie? eval(idMainMenu+"submenu.style") : document.getElementById(idMainMenu+"submenu").style;
  if (lastMenu != null && lastMenu != submenuStyle) 
  {
    lastMenu.visibility = fHide;
  }

  menuTopLeft = cumulativeTopLeftOffset(menu);
  menuBottRight = cumulativeBottomRightOffset(menu);

  submenuStyle.left = menuTopLeft[0];
  submenuStyle.top  = menuTopLeft[1] + getElementHeight(menu);

  submenuTopLeft = cumulativeTopLeftOffset(submenu);
  submenuBottRight = cumulativeBottomRightOffset(submenu);
  
  submenuStyle.visibility = fShow;

  logIt("menu: "+menuTopLeft+" to "+menuBottRight+", submenu: "+submenuTopLeft+" to "+submenuBottRight);

  lastMenu = submenuStyle;
  
  menuMutexUsed = false;
}

function hideAll()
{
  if (lastMenu != null && !menuMutexUsed) 
  {
    lastMenu.visibility = fHide;
    menuTopLeft      = [0,0];
    menuBottRight    = [0,0];
    submenuTopLeft   = [0,0];
    submenuBottRight = [0,0];
  }
}

function updateIt(e)
{
  var x,y = -1;
  
  if (ie&&!opr6)
  {
    x = window.event.clientX;
    y = window.event.clientY;
  }
  if (n||ns6)
  {
    x = e.pageX;
    y = e.pageY;
  }
  
  if (!isWithin(x, y, menuTopLeft, menuBottRight, "menu")
   && !isWithin(x, y, submenuTopLeft, submenuBottRight, "submenu"))
  {
    hideAll();
  }
}

function operahide()
{
  if (opr6)
  {
    if (!MainTable.contains(event.toElement)) hideAll()
  }
}

function cumulativeTopLeftOffset(element) 
{
  var valueT = 0, valueL = 0;
  logIt("cumulativeTopLeftOffset: "+element.id+" ("+element+")");
  do 
  {
    valueT = valueT + (1 * (element.offsetTop  || 0));
    valueL = valueL + (1 * (element.offsetLeft || 0));
    
    logIt("top/left: "+valueT+"/"+valueL+" for "+element.id+" ("+element+")");
    
    element = element.offsetParent;
  } while (element);
  return [valueL, valueT];
}

function cumulativeBottomRightOffset(element) 
{
  var topLeft = cumulativeTopLeftOffset(element);
  var bottomRight = [topLeft[0] + getElementWidth(element), topLeft[1] + getElementHeight(element)];
  return bottomRight;
}

function isWithin(x, y, topLeftCoords, bottomRightCoords, name)
{
  var name = name ? name + " " : "";
  var is = (x >= topLeftCoords[0] && x <= bottomRightCoords[0])
        && (y >= topLeftCoords[1] && y <= bottomRightCoords[1]);
        
  //logIt(x+"/"+y+" is"+(is ? "":" NOT")+" within "+name+topLeftCoords+" and "+bottomRightCoords);
        
  return is;
}

function getElementWidth(element)
{
  return element.offsetWidth;
}

function getElementHeight(element)
{
  return element.offsetHeight;
}

if (document.layers)
{
  window.captureEvents(Event.MOUSEMOVE);
  window.captureEvents(Event.CLICK);
  window.onmousemove=updateIt;
  window.onclick=hideAll;
}
if (ie||ns6)
{
  document.onclick=hideAll;
  //document.onscroll=hideAll;
  document.onmousemove=updateIt;
}