// check browser version
NS4 = (document.layers) ? 1 : 0;

var topID  = -1;
var sels;
var selsh = false;

// constructor of menu elements

function unhideSels() {
  sels = document.getElementsByTagName('select');
  for (i = 0; i < sels.length; i++) { 
    sels[i].style.visibility = 'visible';
  }
}
function hideSels() {
  sels = document.getElementsByTagName('select');
  for (i = 0; i < sels.length; i++) { 
    sels[i].style.visibility = 'hidden';
  }
}
function menuConstructor (id, content)
{
  this.ID            = id;
  this.width         = content [0]*1;
  this.x             = content [1]*1;
  this.y             = content [2]*1;
  this.timerID       = -1;
  this.isOn          = false;
  this.item          = new Array ();
  this.currItemID    = -1;

  if (this.x < 0) {
    this.x = initX + usedWidth;
    usedWidth = usedWidth + this.width;
  }
       
  if (this.y < 0) this.y = initY;

  items = content [3];

  layerBody = '<div class="ddmenu"><table width=' + this.width + ' cellpadding=0 cellspacing=1 border=0>';
  
  for (j = 0; j <= items.length - 2; j = j + 2) {
    if (j == 0) {
      this.itemHeight = topHeight;
      this.itemClass = 'menutop';
    }
    else if (j < items.length - 2) {
      this.itemHeight = middleHeight;
      this.itemClass = 'menumiddle';
    } 
    else {
      this.itemHeight = bottomHeight;
      this.itemClass = 'menubottom';
    }
       
    layerBody += '<td height=' + this.itemHeight + ' width=' + this.width + ' class=' + this.itemClass + '><a class=' + this.itemClass + ' href='+ items [j + 1] +' >' + items [j] + '</a></td>';
    if (j < items.length - 2) layerBody = layerBody +  '<tr>\n';
    else layerBody = layerBody + '\n';
  }

  if (!NS4)
    layerHeader = '<div id=Menu' + this.ID +
                  ' onMouseOver="enterMenu (' + this.ID + '); " onMouseOut = "exitMenu (' + this.ID + '); "' +
                  ' style="width: ' + this.width + '; visibility: hidden; position: absolute; left:' + this.x +
                  'px; top:' + this.y + 'px;">';
  else
    layerHeader = '<layer id=Menu' + this.ID +
                  ' visibility=hide onMouseOver="enterMenu (' + this.ID + '); " onMouseOut = "exitMenu (' + this.ID + '); "' +
                  ' left=' + this.x +
                  'pt top =' + this.y + 'pt>';

  layerFooter = '</table></div>';

  if (!NS4) layerFooter = layerFooter + '</div>';
  else layerFooter = layerFooter + '</layer>';

  document.writeln (layerHeader + layerBody + layerFooter);

  return this;
}
function enterTopItem (ID) {
  if (topID != ID && topID != -1) hide (topID);
  releaseTree (ID);
  topID = ID;
  show (ID);
  hideSels();
}

function exitTopItem (ID) {
  menuElement [ID].timerID = setTimeout ('hide (' + ID + ')', delay);
}

function enterMenu (ID) {
  clearTimeout (menuElement [ID].timerID);
  menuElement [ID].timerID = -1;
  hideSels();
}

function exitMenu (ID) {
  timeoutTree (ID);
}

function releaseTree (ID) {
  clearTimeout (menuElement [ID].timerID);
  menuElement [ID].timerID = -1;
}

function timeoutTree (ID) {
  menuElement [ID].timerID = setTimeout ('hide (' + ID + ')', delay);
}

function show (ID) {
  if (!NS4) {
    if (document.all) {
      document.all['Menu' + ID].style.visibility = "visible";
    }
    else if (document.getElementById) {
      document.getElementById('Menu' + ID).style.visibility = "visible";
    }
  }
  else document.layers[ID].visibility = "show";
  menuElement [ID].isOn = true;
}

function hide (ID) {
  if (!NS4) {
    if (document.all) {
      document.all['Menu' + ID].style.visibility = "hidden";
    }
    else if (document.getElementById) {
      document.getElementById('Menu' + ID).style.visibility = "hidden";
    }
  }
  else {
    document.layers['Menu' + ID].visibility = "hide";
  }
  menuElement [ID].isOn = false;
  if (!menuElement[topID].isOn) unhideSels();
}

function createMenu () {
  for (var i = 0; i < menuContent.length; i++) {
    menuElement [i] = new menuConstructor (i, menuContent [i]);
    numOfMenus++;
  }
}

createMenu ();