function menu2GetItemById(Id) {

    var name = "", menu;

    if( typeof(window.menu2Menus) == 'undefined')
      return null;

    for(name in window.menu2Menus) {

      menu = window.menu2Menus[name];
      if(typeof(menu.itemsAll[Id]) != 'undefined')
        return menu.itemsAll[Id];

    }

    return null;
  }

  function menu2stateStyle() {
    this.bgcolor     = "";
    this.bordercolor = "";
    this.borderstyle = "";
    this.borderwidth = "";
    this.className   = "";
    this.color       = "";
    this.fontsize    = "";
    this.fontstyle   = "";
    this.fontweight  = "";
    this.fontfamily  = "";
    this.image        = "";
    this.imagenochild = "";
    this.imageopen    = "";
    this.bgimage     = "";
  }

  function menu2Style(name) {
    this.name         = name;
    this.align        = "";
    this.bgcolor      = "";
    this.bgimage      = "";
    this.bordercolor  = "";
    this.borderstyle  = "";
    this.borderwidth  = "";
    this.className    = "";
    this.color        = "";
    this.fontsize     = "";
    this.fontstyle    = "";
    this.fontweight   = "";
    this.fontfamily   = "";
    this.height       = "";
    this.image        = "";
    this.imagenochild = "";
    this.imageopen    = "";
    this.imagepadding = "";
    this.imageposition= "left";//left or right
    this.margin       = "";
    this.nolink       = false;
    this.padding      = "";
    this.textAfter    = "";
    this.textBefore   = "";
    this.vAlign       = "";
    this.width        = "";

    this.onStyle       = new menu2stateStyle;
    this.selectedStyle = new menu2stateStyle;
  }


  function menu2levelStyle() {
    this.bgcolor = "";
    this.bgimage = "";
    this.border  = "";
    this.level   = "";
    this.margin  = "";
    this.padding = "";
    this.style   = "";
    this.width   = "100%";
  }

  function menu2Item(name) {

    this.name   = name;
    this.text   = "";
    this.url    = "";
    this.level  = null;
    this.style  = null;//reference to style
    this.menu   = null;
    this.parentItem = null;
    this.selected = false;
    this.image     = "";
    this.imageopen = "";
    this.imageover = "";
    this.imageselected = "";

    this.items = new Array();

    this.getStyle = function(state) {
      var i=0;

      if(this.style==null && this.level !== null && this.menu !== null) {
        for (i = 0; i < this.menu.levelStyles.length; i++) {

          if ((this.style != null && Math.abs(this.menu.levelStyles[i].level - this.level) 
                  < Math.abs(this.style.level - this.level)
              && this.menu.levelStyles[i].level <= this.level) || this.style == null) {

            this.style = this.menu.levelStyles[i];

          }
        }
      }

      if (!this.style) {
        alert("style is null\n" + "level:\t" + this.level + "\n" + "menu:\t" + this.menu);
        return false;
      }

      if (typeof(state) == 'undefined') state = '';

      switch(state.toLowerCase()) {
        case 'over'    : return this.menu.styles[this.style.style].onStyle; break;
        case 'selected': return this.menu.styles[this.style.style].selectedStyle; break;
        case 'level'   : return this.style; break;
        default        : return this.menu.styles[this.style.style]; break;
      }

    };

    this.appendItem = function(menuItem) {
      var l = this.items.length;

      this.items[l]            = menuItem;
      this.items[l].level      = this.level + 1;
      this.items[l].parentItem = this;
      this.items[l].menu       = this.menu;

      this.items[l].id = "menu" + this.items[l].menu.name + "_" + this.items[l].name;

      if(document.getElementById(this.items[l].id) != null)
        this.items[l].id  += (new Date).getTime();

      this.menu.itemsAll[this.items[l].id] = this.items[l];

      return this.items[l];
    };

    this.toHTML = function() {
      var style, levelStyle = null, HTML = "", tableStyleText = "", cellStyleText = "";
      var imageCellStyleText = "", i, classText = "", linkStyleText = "";
      var imageCellText = "";

      style = this.getStyle();

      if(this.items.length > 0)
        levelStyle = this.items[0].getStyle("level");

      with(style) {
        if (align)        cellStyleText += "text-align:" + align + ";";
        if (bgcolor)     tableStyleText += "background-color:" + bgcolor + ";";
        if (bgimage)     tableStyleText += "background-image:url(" + bgimage + ");";
        if (bordercolor) tableStyleText += "border-color:" + bordercolor + ";";
        if (borderstyle) tableStyleText += "border-style:" + borderstyle + ";";
        if (borderwidth) tableStyleText += "border-width:" + borderwidth + ";";
        if (className)        classText += " class='" + className + "' ";
        if (color)        linkStyleText += "color:" + color + ";";
        if (fontsize)     linkStyleText += "font-size:" + fontsize + ";";
        if (fontstyle)    linkStyleText += "font-style:" + fontstyle + ";";
        if (fontweight)   linkStyleText += "font-weight:" + fontweight + ";";
        if (fontfamily)   linkStyleText += "font-family:" + fontfamily + ";";
        if (height)      tableStyleText += "height:" + height + ";";
        if (margin)      tableStyleText += "margin:" + margin + ";";
        if (padding)      cellStyleText += "padding:" + padding + ";";
        if (vAlign) {
           cellStyleText += "vertical-align:" + vAlign + ";";
           imageCellStyleText += "vertical-align:" + vAlign + ";";
        }
        if (width)       tableStyleText += "width:" + width + ";";

        if (imagepadding)   imageCellStyleText += "padding:" + imagepadding +";";
        imageCellStyleText += "width:1;";
      }

      if(linkStyleText != "") linkStyleText = "style='" + linkStyleText + "'";
      if(tableStyleText != "") tableStyleText = "style='" + tableStyleText + "'";
      if(imageCellStyleText != "") imageCellStyleText = "style='" + imageCellStyleText + "'";

      HTML += style.textBefore;
      HTML += "<TABLE " + tableStyleText + " cellpadding='0' cellspacing='0' id='" + this.id + "'>\n";
      HTML += "<TR onmouseover='menu2GetItemById(\"" + this.id + "\").changeStyle(\"over\")'" + 
              " onmouseout='menu2GetItemById(\"" + this.id + "\").changeStyle(\"out\")'" + 
              " onclick='menu2GetItemById(\"" + this.id + "\").showHideSub()'>\n";

      if ( this.image != "" )
        imageCellText += "<TD " + imageCellStyleText + "><IMG src='" + this.image + 
                 "' border='0'></TD>\n";
      else if(style.image && this.items.length > 0)
        imageCellText += "<TD " + imageCellStyleText + "><IMG src='" + style.image + 
                 "' border='0'></TD>\n";
      else if(style.imagenochild && this.items.length == 0)
        imageCellText += "<TD " + imageCellStyleText + "><IMG src='" + style.imagenochild + 
                 "' border='0'></TD>\n";


      if(style.imageposition == "left")
         HTML += imageCellText;

      HTML += "<TD " + cellStyleText + ">";

      if(this.url && !style.nolink)
        HTML += "<A href=\"" + this.url + "\" " + linkStyleText + classText + 
                ">" + this.text + "</A>";
      else
        HTML += "<SPAN " + linkStyleText + classText + ">" + this.text + "</SPAN>";

      HTML += "</TD>\n";

      if(style.imageposition == "right")
         HTML += imageCellText;

      HTML += "</TR></TABLE>\n";

      if(this.items.length > 0) {

        if ( !this.menu.itemsOpen )
          cellStyleText += "display:none;";

        with (levelStyle) {

          if (bgcolor) cellStyleText += "background-color:" + bgcolor + ";";
          if (bgimage) cellStyleText += "background-image:url(" + bgimage + ");";
          if (border)  cellStyleText += "border:" + border + ";";
          if (margin)  cellStyleText += "margin:" + margin + ";";
          if (padding) cellStyleText += "padding:" + padding + ";";
          if (width)   cellStyleText += "width:" + width + ";";

        }

        if(cellStyleText != "")
          cellStyleText = "style='" + cellStyleText + "'";
                  
        HTML += "<TABLE cellspacing='0' cellpadding='0' " + cellStyleText + " id='" + this.id + "_sub'><TR><TD>";

        for(i = 0; i < this.items.length; i++) {
          HTML += this.items[i].toHTML();
        }

        HTML += "</TD></TR></TABLE>";
      }

    HTML += style.textAfter;

    return HTML;

    };

    this.changeStyle = function(state) {

      var id = this.id, element = document.getElementById(id), style, image = null, link;
      var cell = (this.getStyle().imageposition == 'left')? element.rows.item(0).cells.item(element.rows.item(0).cells.length - 1): element.rows.item(0).cells.item(0);
      var elementSub = document.getElementById(id + "_sub");

      image = element.getElementsByTagName('IMG');

      if(image.length != 0)
        image = image.item(0);
      else
        image = null;

      link = cell.getElementsByTagName('A');

      if(link.length != 0)
        link = link.item(0);
      else {
        link = cell.getElementsByTagName('SPAN');

        if(link.length != 0)
          link = link.item(0);
        else
          return false;
      }

      if(typeof(state) == 'undefined') state = "";
      state = state + '';

      switch(state.toLowerCase()) {
        case 'over':

          style = this.getStyle("over");

          if ( this.imageover != "" ) 
            imageSrc = this.imageover;
          else if ( this.image != "" )
            imageSrc = this.image;
          else if (this.items.length == 0)
            imageSrc = (style.imagenochild != "")? style.imagenochild: this.getStyle().imagenochild;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display == 'none')
            imageSrc = (style.image != "")? style.image: this.getStyle().image;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display != 'none')
            imageSrc = (style.imageopen != "")? style.imageopen: this.getStyle().imageopen;

          if(style.bgcolor) {
             element.style.backgroundColor = style.bgcolor;
             element.style.backgroundImage = "";
          }

          if(style.bordercolor) element.style.borderColor = style.bordercolor;
          if(style.borderstyle) element.style.borderStyle = style.borderstyle;
          if(style.borderwidth) element.style.borderWidth = style.borderwidth;
          if(style.className)   element.className         = style.className;
          if(style.color)       link.style.color = style.color;
          if(style.fontsize)    link.style.fontSize = style.fontsize;
          if(style.fontstyle)   link.style.fontStyle= style.fontstyle;
          if(style.fontweight)  link.style.fontWeight = style.fontweight;
          if(style.fontfamily)  link.style.fontFamily = style.fontfamily;
          if(imageSrc && image != null)  image.src = imageSrc;
          if(style.bgimage)     element.style.backgroundImage = style.bgimage;
          break;
        case 'selected':
          style = this.getStyle("selected");

          if ( this.imageselected != "" )
            imageSrc = this.imageselected;
          else if ( this.image != "" )
            imageSrc = this.image;
          else if (this.items.length == 0)
            imageSrc = (style.imagenochild != "")? style.imagenochild: this.getStyle().imagenochild;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display == 'none')
            imageSrc = (style.image != "")? style.image: this.getStyle().image;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display != 'none')
            imageSrc = (style.imageopen != "")? style.imageopen: this.getStyle().imageopen;

          if(style.bgcolor) {
            element.style.backgroundColor = style.bgcolor;
            element.style.backgroundImage = "";
          }
          if(style.bordercolor) element.style.borderColor = style.bordercolor;
          if(style.borderstyle) element.style.borderStyle = style.borderstyle;
          if(style.borderwidth) element.style.borderWidth = style.borderwidth;
          if(style.className)   element.className         = style.className;
          if(style.color)       link.style.color = style.color;
          if(style.fontsize)    link.style.fontSize = style.fontsize;
          if(style.fontstyle)   link.style.fontStyle= style.fontstyle;
          if(style.fontweight)  link.style.fontWeight = style.fontweight;
          if(style.fontfamily)  link.style.fontFamily = style.fontfamily;
          if(imageSrc && image != null)  image.src = imageSrc;
          if(style.bgimage)     element.style.backgroundImage = style.bgimage;
          break;
        case 'out':
          if(this.selected)
            style = this.getStyle("selected");
          else
            style = this.getStyle();

          if ( this.image != "" )
            imageSrc = this.image;
          else if (this.items.length == 0)
            imageSrc = (style.imagenochild != "")? style.imagenochild: this.getStyle().imagenochild;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display == 'none')
            imageSrc = (style.image != "")? style.image: this.getStyle().image;
          else if (this.items.length > 0 && elementSub != null && elementSub.style.display != 'none')
            imageSrc = (style.imageopen != "")? style.imageopen: this.getStyle().imageopen;


          if(style.bgcolor)     element.style.backgroundColor = style.bgcolor;  else element.style.backgroundColor = "";
          if(style.bordercolor) element.style.borderColor = style.bordercolor;  else element.style.borderColor     = "";
          if(style.borderstyle) element.style.borderStyle = style.borderstyle;  else element.style.borderStyle     = "";
          if(style.borderwidth) element.style.borderWidth = style.borderwidth;  else element.style.borderWidth     = "";
          if(style.className)   element.className         = style.className;    else element.className             = "";
          if(style.color)       link.style.color = style.color;                 else element.style.color           = "";
          if(style.fontsize)    link.style.fontSize = style.fontsize;           else element.style.fontSize        = "";
          if(style.fontstyle)   link.style.fontStyle= style.fontstyle;          else element.style.fontStyle       = "";
          if(style.fontweight)  link.style.fontWeight = style.fontweight;       else element.style.fontWeight      = "";
          if(style.fontfamily)  link.style.fontFamily = style.fontfamily;       else element.style.fontFamily      = "";
          if(imageSrc && image != null)  image.src = imageSrc;
          if(style.bgimage)     element.style.backgroundImage = "url("+style.bgimage+")";  else element.style.backgroundImage = "";
        default:
          break;
      }

    };

    this.showHideSub = function() {
      var id = this.id, element = document.getElementById(id + "_sub"), style, image = null, i;

      if(element == null)
        return false;

      var visible = (element.style.display == 'none')? false: true;

      style = this.getStyle();

      image = document.getElementById(id).getElementsByTagName('IMG');

      if(image.length != 0)
        image = image.item(0);
      else
        image = null;

      if(visible) {
 
        if(style.imageopen && image != null)
          image.src = style.image;

        element.style.display = 'none';

        for (i = 0; i < this.menu.openItems.length; i++) {

          if (this.menu.openItems[i] == this.id) {
            this.menu.openItems = this.menu.openItems.slice(0,i).concat(this.menu.openItems.slice(i + 1));
            break;
          }

        }

      } else {
        if(style.imageopen && image != null)
          image.src = style.imageopen;

        this.menu.openItems[this.menu.openItems.length] = this.id;

        element.style.display = '';
      }

      if (!visible && this.menu.autoHide) {
        var openItemsCopy = this.menu.openItems, object = this;

        while(object != null) {

          for(i = 0; i < openItemsCopy.length; i++) {

            if(openItemsCopy[i] == object.id) {
              openItemsCopy = openItemsCopy.slice(0, i).concat(openItemsCopy.slice(i + 1));
            }

          }

          object = object.parent;

        }

        var mItem;

        for(i = 0; i < openItemsCopy.length; i++) {

          mItem = menu2GetItemById(openItemsCopy[i]);

          if (element = document.getElementById(mItem.id) && element.style.display != 'none') {
            mItem.showHideSub();
          }

        }

      }

    };

  }

  function menu2Menu(name) {

    this.name      = name;
    this.autoHide  = false;
    this.itemsOpen = false;

    if( typeof(window.menu2Menus) == 'undefined')
       window.menu2Menus = new Array();

    window.menu2Menus[name] = this;

    this.styles      = new Array();
    this.levelStyles = new Array();
    this.items       = new Array();
    this.itemsAll    = new Array();
    this.openItems   = new Array();

    this.appendItem = function(menuItem) {
      var l = this.items.length;

      this.items[l]            = menuItem;
      this.items[l].level      = 0;
      this.items[l].parentItem = null;
      this.items[l].menu       = this;

      this.items[l].id = "menu" + this.name + "_" + this.items[l].name;

      if(document.getElementById(this.items[l].id) != null)
        this.items[l].id  += (new Date).getTime();

      this.itemsAll[this.items[l].id] = this.items[l];

      return this.items[l];
    };

    this.highlightSelected = function() {

      var id, url = window.location.href, likeUrl = "", likeId = null, items = this.itemsAll, element;

      for(id in items) {

        if(items[id].url != "" && url.indexOf(items[id].url) != -1 && items[id].url.length > likeUrl.length) {
          likeUrl = items[id].url;
          likeId  = id;
        }

      }

      if(likeId != null) {
        items[likeId].changeStyle('selected');
        items[likeId].selected = true;

        element = items[likeId];

        while((element = element.parentItem)) {
          id = element.id + "_sub";

          if(document.getElementById(id) != null && document.getElementById(id).style.display == 'none')
            element.showHideSub();
        }

        id = items[likeId].id + "_sub";

        if(items[likeId].items.length != 0 && document.getElementById(id) != null && 
            document.getElementById(id).style.display == 'none')
          items[likeId].showHideSub();
      }

    };

    this.drawMenu = function(containerId) {
      var container = null, levelStyle, style, cellStyleText = "", HTML = "";

      if(typeof(containerId) != undefined && document.getElementById(containerId) != null)
         container = document.getElementById(containerId);

      if(this.items.length > 0)
         levelStyle = this.items[0].getStyle("level");

      if(this.items.length > 0) {

        cellStyleText = "";

        with (levelStyle) {

          if (bgcolor) cellStyleText += "background-color:" + bgcolor + ";";
          if (bgimage) cellStyleText += "background-image:" + bgimage + ";";
          if (border)  cellStyleText += "border:" + border + ";";
          if (margin)  cellStyleText += "margin:" + margin + ";";
          if (padding) cellStyleText += "padding:" + padding + ";";
          if (width)   cellStyleText += "width:" + width + ";";

        }

        if(cellStyleText != "")
          cellStyleText = "style='" + cellStyleText + "'";

        HTML += "<TABLE border='0' cellspacing='0' cellpadding='0' " + cellStyleText + "><TR><TD>";

        for(i = 0; i < this.items.length; i++) {
          HTML += this.items[i].toHTML();
        }

        HTML += "</TD></TR></TABLE>";

      }

      if(container != null) {
        container.innerHTML = HTML;
        this.highlightSelected();
      }

    };

  }