n=0;
ie=0;
if (document.all)
{
  n=0;
  ie=1;
  fShow="visible";
  fHide="hidden";
  styleClass="clsMenuItemIE";
}
else if (document.layers)
{
  n=1;
  ie=0;
  fShow="show";
  fHide="hide";
  styleClass="clsMenuItemNS";
}


if (document.all)
{
  n=0;
  ie=1;
  fShow="visible";
  fHide="hidden";
  menuSubItemStyleClass="clsSubMenuItemIE";
  menuHeaderStyleClass="clsMenuHeaderStyleIE";
}
else if (document.layers)
{
  n=1;
  ie=0;
  fShow="show";
  fHide="hide";
  menuSubItemStyleClass="clsSubMenuItemNS";
  menuHeaderStyleClass="clsMenuHeaderStyleNS";
}
else
{
  n=0;
  ie=0;
}

function ttnbRefreshMenuStructure()
{
  if (n) // for Netscape only
  {
    var menubarIndex = 1;   // ignore "Home"
    for (index=0;index < top.document.layers.length;index++) {
      if (top.document.layers[index].id.indexOf("submenu") != -1) {
	    layerHTMLstring = "";
        layerHTMLstring += "<table border="+menubar.subMenuPaneBorder+" bgcolor="+menubar.bgColor+">\n";
        
		for (itemIndex=0;itemIndex < top.document.layers[index].document.links.length;itemIndex++) {
	      /* Now rewrite the layer using our stored text. */
          layerHTMLstring += "<tr><td><a class="+menuSubItemStyleClass+" title='' href='";
          layerHTMLstring += menubar.subMenuLayerDataArray[menubarIndex][itemIndex].location;
          layerHTMLstring += "'>";
          layerHTMLstring += menubar.subMenuLayerDataArray[menubarIndex][itemIndex].text;
          layerHTMLstring += "&nbsp;</a></td></tr>\n";
		}
        layerHTMLstring += "</table>\n";
        top.document.layers[index].document.open();
        top.document.layers[index].document.write(layerHTMLstring);
        top.document.layers[index].document.close();
        top.document.layers[index].onMouseOver = setCurMenuVisible2;
	    top.document.layers[index].onMouseOut = setCurMenuInvisible;
        top.document.layers[index].document.linkColor = "#FFFFFF";
        top.document.layers[index].document.vlinkColor = "#223399";
        top.document.layers[index].document.alinkColor = "#223399";
        top.document.layers[index].document.hlinkColor = "#223399";
		menubarIndex++;
	  }
      if (top.document.layers[index].id.indexOf("menuheader") != -1) {
		top.document.layers[index].visibility = fShow;
	  }
	}
  }
}

function MenuLayerData()
{
  // no code needed
}

function addItem(idItem, text, location, altLocation)
{
  var Lookup = "<!-- ITEM "+idItem+" -->";
  if (HTMLstr.indexOf(Lookup) != -1)
  {
    alert(idItem + " already exist");
    return;
  }

  var MENUitem = "\n<!-- ITEM "+idItem+" -->\n";
  
  /* New code that calculates the width of each menu heading individually depending on heading text. */
  this.menuHeaderPaneWidth = (text.length + 2) * this.menuHeaderPaneWidthFactor;

  /* This is a more robust way of making sure that menubar headers don't wrap onto a second line. */
  /* But take it out again because it causes other problems. */
  //text = text.replace(" ", "&nbsp;");

  if (n)
  {
    var index = this.subMenuLayerDataArray.length;
    this.subMenuLayerDataArray[index] = new Array();
    this.menuHeaderLayerDataArray[idItem] = new MenuLayerData();
    this.menuHeaderLayerDataArray[idItem].location = location;

    MENUitem += "<td width="+this.menuHeaderPaneWidth+" height="+this.subMenuPaneHeight+">\n";
    MENUitem += "<ilayer name="+idItem+"menuheader" + " width="+this.menuHeaderPaneWidth+" visibility=hide>";
    MENUitem += "<a title='" + text + "' href='"+location+"' class="+menuHeaderStyleClass;
    MENUitem += " onMouseOver='displaySubMenu(";
    MENUitem += '"';
    MENUitem += idItem;
    MENUitem += '"';
    MENUitem += ")' onMouseOut='setCurMenuInvisible2();' onclick='return true;'>";
    MENUitem += "&nbsp;&nbsp;";
    MENUitem += text;
    MENUitem += "&nbsp;";
    MENUitem += "</a>";
    MENUitem += "</ilayer>";
    MENUitem += "</td>\n";
  }
  else if (ie)
  {
    MENUitem += "<td  height="+this.subMenuPaneHeight+">\n";
	/* the menuHeaderStyle on the DIV seems to affect vertical positioning of the header menubar text? */
    MENUitem += "<div width="+this.menuHeaderPaneWidth+" id='"+idItem+"' style='position:relative; "+this.menuHeaderStyle+"' "
    MENUitem += 'onMouseOver="';
    MENUitem += "displaySubMenu('"+idItem+"')";
    MENUitem += '" ';
    MENUitem += "onMouseOut='setCurMenuInvisible();'>\n";
    //MENUitem += "<a class="+menuHeaderStyleClass+" title='"+text+"'";
    MENUitem += "<a class="+menuHeaderStyleClass+" title=''";
    if (location != null)
    {
      MENUitem += "href='"+location+"'  onClick='hideCurrentMenu()'";
    }
    else
    {
      if (altLocation != null)
      {
        MENUitem += "href='"+altLocation+"' ";
      }
      else
      {
        MENUitem += "href='.' ";
      }
      MENUitem += "onClick='return false;'"
    }
    MENUitem += ">";
    MENUitem += "&nbsp;&nbsp;";
    MENUitem += text;
    MENUitem += "&nbsp;";
    MENUitem += "</a>\n";
    MENUitem += "</div>\n";
    MENUitem += "</td>\n";
  }

  MENUitem += "<!-- END OF ITEM "+idItem+" -->\n";
  MENUitem += "<!-- MAIN_MENU -->\n";

  HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
  
}

function addSubItem(idParent, text, location)
{
  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 += "\n";
    if (n)
    {
      MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor;
      MENUitem += " onMouseOut='setCurMenuInvisible();' onMouseOver='setCurMenuVisible2();'";
      MENUitem += "'>\n";
      MENUitem += "<table border="+this.subMenuPaneBorder+" bgcolor="+this.bgColor+">\n";
      MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
      MENUitem += "</table>\n";
      MENUitem += "</layer>\n";
    }
    if (ie)
    {
      MENUitem += "<span bgcolor=black id='"+idParent+"submenu' onMouseOut='setCurMenuInvisible();'";
      MENUitem += " onMouseOver='setCurMenuVisible();'";
      MENUitem += " style='background-color:"+this.bgColor+"; position:absolute; width: 180px; visibility: hidden '>\n";
      MENUitem += "<table border="+this.subMenuPaneBorder+" bgcolor="+this.bgColor+">\n";
      MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
      MENUitem += "</table>\n";
      MENUitem += "</span>\n";
    }
    MENUitem += "\n";
    MENUitem += "<!-- SUB_MENU -->\n";
    HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
  }

  if (n)
  {
      var currentMenuIndex = this.subMenuLayerDataArray.length - 1;
      var index = this.subMenuLayerDataArray[currentMenuIndex].length;
      this.subMenuLayerDataArray[currentMenuIndex][index] = new MenuLayerData();
      this.subMenuLayerDataArray[currentMenuIndex][index].text = text;
      this.subMenuLayerDataArray[currentMenuIndex][index].location = location;
  }
  MENUitem = "<tr><td><a class="+menuSubItemStyleClass+" title='' href='"+location+"'>"+text+"&nbsp;</a></td></tr>\n";
  MENUitem += Lookup;
  HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

function showMenuBar()
{
  document.writeln(HTMLstr);
}

function MenuBar()
{
   this.bgColor = "#006600";
  if (ie)
  {
    /* style (as well as styleClass) needed for IE DIV */
    this.menuHeaderStyle = "font: bold 7pt Verdana, Arial, Helvetica; color: white; text-decoration: none;";
    //this.subMenuPaneWidth = subMenuPaneWidth;
    this.menuHeaderPaneWidthFactor = 15;
    this.subMenuPaneHeight = 18;
    this.menuHeaderIndent = 7;
	this.rightmargin = 1;
  }
  else if (n)
  {
    this.subMenuLayerDataArray = new Array();
    this.menuHeaderLayerDataArray = new Array();
	
    this.menuHeaderStyle = "font: bold 8pt Verdana, Arial, Helvetica; color: white; text-decoration: none;";
    //this.subMenuPaneWidth = subMenuPaneWidth - 9;
    this.menuHeaderPaneWidthFactor = 6;
    this.subMenuPaneHeight = 18;
    this.menuHeaderIndent = 1;
	if ("index" != "newspaper edition") {
	  this.rightMargin = 1;
	} else {
	  this.rightMargin = 400;
	}
  }
  this.mainPaneBorder = 0;
  this.subMenuPaneBorder = 0;

   this.addItem    = addItem;
  this.addSubItem = addSubItem;
  this.showMenuBar   = showMenuBar;

   lastMenu = null;
  lastMenuHeading = null;

  HTMLstr = "<!-- MENU PANE DECLARATION BEGINS -->\n";
  HTMLstr += "\n";
  HTMLstr += "<table width=650 cellpadding=0 height=14 cellspacing=0 bgcolor="+this.bgColor+" border="+this.mainPaneBorder+">\n";
  HTMLstr += "<tr>";
  HTMLstr += "<td width='"+this.menuHeaderIndent+"'><font face='verdana,arial,sans-serif' size='1' color='#ffffff'>&nbsp;</font></td>";
  HTMLstr += "<!-- MAIN MENU STARTS -->\n";
  HTMLstr += "<!-- MAIN_MENU -->\n";
  HTMLstr += "<!-- MAIN MENU ENDS -->\n";
  HTMLstr += "<td width='"+this.rightMargin+"'><font face='verdana,arial,sans-serif' size='1' color='#ffffff'>&nbsp;</font></td>";
  HTMLstr += "</tr>\n";
  HTMLstr += "</table>\n";
  HTMLstr += "\n";
  HTMLstr += "<!-- SUB MENU STARTS -->\n";
  HTMLstr += "<!-- SUB_MENU -->\n";
  HTMLstr += "<!-- SUB MENU ENDS -->\n";
  HTMLstr += "\n";
  HTMLstr += "<!-- MENU PANE DECLARATION ENDS -->\n";
}

function displaySubMenu(idMainMenu)
{
  var menu;
  var submenu;
  //alert(eval("nav" + idMainMenu));
  
  if (n)
  {
    idMainMenuLayerName = idMainMenu + "menuheader";
    menu = document.layers[idMainMenuLayerName];

     if (document.layers[idMainMenu+"submenu"] != null)
    {
      submenu = document.layers[idMainMenu+"submenu"];
      if (lastMenu != null && lastMenu != submenu) hideCurrentMenu();
	  
      submenu.left = document.layers[idMainMenuLayerName].pageX + 3;
      submenu.top  = document.layers[idMainMenuLayerName].pageY +
      document.layers[idMainMenuLayerName].clip.height;
		if (eval("nav" + idMainMenu)) {
      		submenu.visibility = fShow;
		}

      submenu.onMouseOut=hideCurrentMenu;
    } else
    {
      if (lastMenu != null) hideCurrentMenu();
    }
  }
  else if (ie)
  {
//     top.alert("idMainMenu: " + document.all.item(idMainMenu+"submenu"))
     if (document.all.item(idMainMenu+"submenu") != null)
    {
      menu = eval(idMainMenu);
      submenu = eval(idMainMenu+"submenu.style");

      var totalOffset = 0;
      var item = menu;
      do
      {
        totalOffset += eval('item.offsetLeft');
        item = eval('item.offsetParent');
      } while (item != null);
      submenu.left = totalOffset+3;

      submenu.top  = menu.style.top;
	  if (eval("nav" + idMainMenu)) {
      		submenu.visibility = fShow;
		}
      
    }

    if (lastMenu != null && lastMenu != submenu)
    {
      hideCurrentMenu();
    }

  }
  
  setCurMenuVisible();
  lastMenu = submenu;
  lastMenuHeading = menu;

}

function hideCurrentMenu()
{
  if (lastMenu != null)
  {
    lastMenu.visibility = fHide;
    lastMenu = null;
    lastMenuHeading = null;
  }
}

var timeOutID = null;
function setCurMenuVisible()
{
   if (timeOutID)
  {
    clearTimeout(timeOutID);
    timeOutID = null;
  }
}

function setCurMenuVisible2()
{
   if (timeOutID)
  {
    clearTimeout(timeOutID);
    timeOutID = null;
  }

  /* This timeout when executed should clear out any hide timeout set by leaving the menu bar. (?) */  
  timeOutID = setTimeout("setCurMenuVisible()", 300)
  
}

function setCurMenuInvisible()
{
   if (ie || 
         (n && lastMenuHeading && lastMenu &&
          lastMenuHeading.name+"submenu" != lastMenu.name))
  {
    timeOutID = setTimeout("hideCurrentMenu()", 500)
  }
}

function setCurMenuInvisible2()
{
   if (n)
  {
    timeOutID = setTimeout("hideCurrentMenu()", 500)
  }
}


