var globalTopOffset = 77;
var globalLeft      = "220px";
var menuArr      = new Array();
var menuNum2Name = new Array();
var delay = 800;
var showQ = new Array();
var hideQ = new Array();
var openedMenu;
var menuWidth = 230;
var itemHeight = 22;
var subItemHeight = 20;

var pippo = null;

function push(arr, el) {
  arr[arr.length] = el;
//  pippo.innerHTML = arr.length;
}

function pop(arr) {
  var len = arr.length;
  if ( len < 1 ) {
    return false;
  } else {
    var res = arr[arr.length-1];
    arr[arr.length-1] = null;
    arr.length = arr.length-1;
//    pippo.innerHTML = arr.length;
    return res;
  }
}

function initMenu(arr,opened) {
  openedMenu = opened;
  for (var i=0;i<arr.length;i+=7) {
    document.getElementById('menu'+i/7).onmouseover = menuMOver;
    document.getElementById('menu'+i/7).onmouseout = menuMOut;
    menuNum2Name[i/7] = arr[i];
    menuArr[arr[i]] = new menuEl(i/7,arr[i],arr[i],arr[i+2],arr[i+3],arr[i+4],arr[i+5],arr[i+6]);
  }
  pippo = document.createElement("div");
  pippo.style.top="5";
  pippo.style.left="300";
  pippo.style.zIndex="10";
  pippo.style.position="absolute";
  pippo.onclick = mydisplay;
  pippo.innerHTML = "pippo";
  pippo.style.display = "none";
  document.body.appendChild(pippo);
}

function mydisplay() {
  var el = document.getElementById('4');
  el.style.display = "block";
  pippo.innerHTML = el.style.display;
}

menuEl = function (index,name,parent,color,bgcolor,arrItem,img,className) {
  this.index     = index;
  this.name      = name;
  this.parent    = parent;
  this.items     = new Array();
  this.links     = new Array();
  this.img       = new Image();
  this.img.src   = img;
  this.color     = color;
  this.bgColor   = bgcolor;
  this.className = className;
  this.display   = "none";
  if (arrItem) {
    var xy = xyCalc(index);
    this.x = xy[0];
    this.y = xy[1];
    this.children = new Array();
    for (var i=0;i<arrItem.length;i+=7) {
      this.items[this.items.length] = arrItem[i];
      this.links[this.links.length] = arrItem[i+1];
      if (arrItem[i+4]) {
	    this.children[this.children.length] = new menuEl(index+"_"+(i/7),arrItem[i],this,arrItem[i+2],arrItem[i+3],arrItem[i+4],arrItem[i+5],arrItem[i+6]);
      }
      else
	this.children[this.children.length] = null;
    }
    this.hasSubMenu = true;
  }
  else {
    this.hasSubMenu = false;
  }
  this.div = null;
}

menuEl.prototype.setVisible = function() { this.display = "block"; }

menuEl.prototype.unsetVisible = function() { this.display = "none"; }

menuEl.prototype.show = function () {
  if (! this.hasSubMenu) {
//    alert("che??? non ha submenu");
    return;
  }
  if (this.index == openedMenu) {
//    alert("non e' o vero, non e' operto");
    return;
  }
  if (!this.div) {
//    alert("adesso lo costruiamo");
    this.div = this.build();
  }

  if (this.display == "block") {
    pippo.innerHTML = this.div.id;
    this.div.style.display = "block";
  }
}

menuEl.prototype.hide = function () {
  if (this.div)
    if (this.display == "none")
      this.div.style.display = "none";
}

menuEl.prototype.build = function () {

//  var o = document.createElement("DIV id='"+this.index+"'");

//  var o = document.createElement("div");
//  o.id = this.index;

  var o = null;
  if ( bw.ie ) {
    o = document.createElement("DIV id='"+this.index+"'");
  } else {
    o = document.createElement("div");
    o.id = this.index;
  }

//  alert(o.id);

  o.style.zIndex = "10";
  o.style.position = "absolute";
  o.style.display = "none";
  o.style.top  = this.x;
  o.style.left = this.y;
  var str = "<table cellpadding='0' cellspacing='0' width='230px'>";

  for (var i in this.items) {
    str += "<tr style='background-color:"+this.color+"' id='"+this.index+"_"+i+"'><td style='width:1px;color:white;padding-left:2px' valign='top'>-</td><td style='padding-left:2px;padding-right:2px' class='" + this.className + "'";
    if (this.children[i]) {
      str += ">" + this.items[i]+'</td>';
      str += "<td class='img'><img src='" + this.img.src + "'></td></tr>"; // immagine
    }
    else {
      str += "colspan='2'>" + this.items[i] + "</td></tr>";
    }
  }
  str += "</table>";

  o.innerHTML = str;
//set event
  for (var i=0;i<o.childNodes[0].childNodes[0].childNodes.length;i++) {
    o.childNodes[0].childNodes[0].childNodes[i].onmouseover=menuMOver;
    o.childNodes[0].childNodes[0].childNodes[i].onmouseout=menuMOut;
    var ff = new Function("x","window.menuClick('"+this.links[i]+"')");
    o.childNodes[0].childNodes[0].childNodes[i].onclick = ff;
}

//  alert( o.innerHTML );

  document.body.appendChild(o);
  return o;
}

function menuMOut(e) {
  if ( bw.safari ) return;
  var o;
  if (bw.ie) o = window.event.srcElement;
  else o = e.target;
  while (true) {
    if (o.nodeName.match(/TR/)) break;
    o = o.parentNode;
  }
  var string = new String(o.id);
  string = string.replace(/^menu/,"");
  var indici = string.split("_");
  var oMenu = menuArr[menuNum2Name[indici[0]]];
  if (typeof(oMenu) != 'object') return;
  o.style.backgroundColor = oMenu.color;
  if (indici.length == 1) {
    oMenu.unsetVisible();
    if ( bw.mac && bw.ie ) {
      push(hideQ, oMenu);
    } else {
      hideQ.push(oMenu);
    }
    setTimeout("doHide()",delay);
  }
  else {
    var o2 = oMenu.children[indici[1]];
    if (o2) {
      o2.unsetVisible();
      if ( bw.mac && bw.ie ) {
        push(hideQ, o2);
      } else {
        hideQ.push(o2);
      }
      setTimeout("doHide()",delay);
    }
    oMenu.unsetVisible();
    if ( bw.mac && bw.ie ) {
      push(hideQ, oMenu);
    } else {
      hideQ.push(oMenu);
    }
    setTimeout("doHide()",delay);
  }
}

function menuMOver(e) {
  if ( bw.safari ) return;
  var o;
  if (bw.ie) o = window.event.srcElement;
  else o = e.currentTarget;
  while (true) {
    if (o.nodeName.match(/TR/)) break;
    o = o.parentNode;
  }
  var string = new String(o.id);
  string = string.replace(/^menu/,"");
  var indici = string.split("_");
  var oMenu = menuArr[menuNum2Name[indici[0]]];
  if (typeof(oMenu) != 'object') return;
  o.style.backgroundColor = oMenu.bgColor;
  if (indici.length == 1) {
    oMenu.setVisible();
    if ( bw.mac && bw.ie ) {
      push(showQ, oMenu);
    } else {
      showQ.push(oMenu);
    }
    setTimeout("doShow()",delay);
  }
  else {
    oMenu.setVisible();
    var o2 = oMenu.children[indici[1]];
    if (o2) {
      o2.setVisible();
      if ( bw.mac && bw.ie ) {
        push(showQ, o2);
      } else {
        showQ.push(o2);
      }
      setTimeout("doShow()",delay);
    }
    oMenu.setVisible();
    if ( bw.mac && bw.ie ) {
      push(showQ, oMenu);
    } else {
      showQ.push(oMenu);
    }
    setTimeout("doShow()",delay);
  }
}

function doHide() {
  var o;
  if ( bw.mac && bw.ie ) {
    while ( o = pop(hideQ) ) {
      o.hide();
    }
  } else {
    while (o = hideQ.pop()) {
      o.hide();
    }
  }
}

function doShow() {
  var o;
  if ( bw.mac && bw.ie ) {
    while ( o = pop(showQ) ) {
      o.show();
    }
  } else {
    while (o = showQ.pop()) {
      o.show();
    }
  }
}

// menuInit

function doOnLoad(jsid) {
//  alert("sono in doOnLoad; jsid="+jsid);
  initMenu(menuItem, jsid);
  var giorno = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var mese = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  var myDate = new Date();
  document.getElementById('currentDate').innerHTML = giorno[myDate.getDay()]+", "+mese[myDate.getMonth()]+" "+myDate.getDate()+", "+myDate.getFullYear();
}

function xyCalc(index) {
  var string = new String(index);
  var indici = string.split("_");
  var localOffset = (indici.length > 1) ? parseInt(indici[1])*subItemHeight : 0;
  var x,y;

  if ((openedMenu == -1) || (openedMenu > indici[0])) { // tutti chiusi
    x = parseInt(globalTopOffset+indici[0]*itemHeight+localOffset);
    y = parseInt(parseInt(globalLeft)+menuWidth*(indici.length-1));
  }
  else {
    if (openedMenu == indici[0]) { // questo menu
      localOffset = (indici.length > 1) ? parseInt(indici[0])*(subItemHeight+2) + parseInt(indici[1])*subItemHeight : 0;
      x = parseInt(globalTopOffset+itemHeight+localOffset);
      y = parseInt(parseInt(globalLeft)+230*(indici.length-2));
    }
    else {
      x = parseInt(globalTopOffset+indici[0]*itemHeight+localOffset+(menuArr[menuNum2Name[openedMenu]].items.length*subItemHeight)+2);
      y = parseInt(parseInt(globalLeft)+menuWidth*(indici.length-1));
    }
  }

  return [x,y];
}

function yCalc() {
  var y = 230;
  var myMenuIndex = (openedMenu < 1000) ? openedMenu : openedMenu - 1000;
  if ( (openedMenu == -1) || (openedMenu == 6) ) {
    return y-2;
  } else {
    return y+menuArr[menuNum2Name[myMenuIndex]].items.length*subItemHeight;
  }
}

function menuClick(page, target) {
  if ( target == null ) {
    target = '_self';
  }
//  document.location = page;
  self.open(page, target);
  return true;
}
