var mObj = new Array();

function TREEMENU(openAll) {
//----------------------------------------------------------------------------------------------------
// Configuração
//----------------------------------------------------------------------------------------------------

  this.width = 150;                                 
  this.bgColor = "#FFFFFF";                         
  this.autoClose = true;                           

  this.itemMargin = 0;                              
  this.itemPadding = 2;                             
  this.itemColor = "#04296D";                       
  this.itemBGColor = "#F0F0F0";                     
  this.itemBGColor1 = "#FFFFFF";                    
  this.itemFont = "Arial, Helvetica";      
  this.itemSize = 11;                               
  this.itemBold = false;                            
  this.itemWrap = false;                            
  this.itemActive = "#04296D";                          
  this.itemActiveUnderline = true;                  

  this.iconWidth = 3;                              
  this.iconHeight = 10;                             
  this.iconClosed = "menu/images/dot.gif";                   
  this.iconClosedHilight = "menu/images/dot.gif";    
  this.iconOpen = "menu/images/dot.gif";                       
  this.iconOpenHilight = "menu/images/dot.gif";        
  this.iconPoint = "menu/images/dot.gif";                    
  this.iconPointHilight = "menu/images/dot.gif";      

  this.imgBlank = "menu/images/dot.gif";                      

//----------------------------------------------------------------------------------------------------
// Funções
//----------------------------------------------------------------------------------------------------

  this.mNr = 0;
  this.actItem = -1;
  this.hilightItem = -1;
  this.targetWindow = -1;
  this.items = new Array();

  if(openAll == null) openAll = false;
  this.openAll = openAll;

  this.entry = function(level, text, url, target, onClick) {
    var i = this.items.length;
    this.items[i] = new makeItem(level, text, url, target, onClick, this.openAll);
  }

  this.getObj = function(id) {
    var obj;
    if(document.getElementById) obj = document.getElementById(id);
    else if(document.all) obj = document.all[id];
    return obj;
  }

  this.jump = function(item) {
    this.hilightItem = item;
    this.openMenu(item);
    if(this.items[item].onClick) eval(this.items[item].onClick);
    if(this.items[item].url) {
      if(this.items[item].target) {
        if(this.items[item].target.indexOf('parent.') == -1 && this.items[item].target.indexOf('top.') == -1) {
          if(this.targetWindow && !this.targetWindow.closed) this.targetWindow.location.href = this.items[item].url;
          else this.targetWindow = window.open(this.items[item].url, 'targetWindow');
          this.targetWindow.focus();
        }
        else eval(this.items[item].target + '.location.href = "' + this.items[item].url + '"');
      }
      else document.location.href = this.items[item].url;
    }
  }

  this.openMenu = function(item) {
    if(this.items[item].knot) this.actItem = item;
    this.newMenu();
  }

  this.closeMenu = function(item) {
    this.actItem = -1;
    if(this.items[item].knot) {
      this.items[item].icon = this.iconClosed;
      for(var i = item+1; i < this.items.length && this.items[i].level > this.items[item].level; i++) {
        if(this.items[i].level > 1) this.items[i].view = false;
      }
    }
    this.newMenu();
  }

  this.viewMenu = function(item) {
    var icon = this.items[item].icon;
    if(icon == this.iconOpen || icon == this.iconOpenHilight) this.closeMenu(item);
    else this.openMenu(item);
  }

  this.setItems = function() {
    var i, showlevel;
    if(this.autoClose && this.actItem >= 0) {
      if(this.items[this.actItem].level <= 1) {
        for(i = 0; i < this.items.length; i++) {
          if(this.items[i].level > 1) this.items[i].view = false;
        }
      }
    }
    for(i = 0; i < this.items.length; i++) {
      if(i < this.items.length-1 && this.items[i+1].level > this.items[i].level) {
        this.items[i].icon = this.items[i+1].view ? this.iconOpen : this.iconClosed;
        this.items[i].knot = true;
      }
      else this.items[i].icon = this.iconPoint;
    }
    if(this.actItem >= 0) {
      showLevel = this.items[this.actItem].level + 1;
      this.items[this.actItem].icon = this.iconOpen;
      for(i = this.actItem+1; i < this.items.length && this.items[i].level >= showLevel; i++) {
        if(this.items[i].level == showLevel) this.items[i].view = true;
      }
    }
    if(this.hilightItem >= 0) {
      i = this.hilightItem;
      if(this.items[i].icon == this.iconOpen) this.items[i].icon = this.iconOpenHilight;
      else if(this.items[i].icon == this.iconClosed) this.items[i].icon = this.iconClosedHilight;
      else if(this.items[i].icon == this.iconPoint) this.items[i].icon = this.iconPointHilight;
    }
  }

  this.content = function(item) {
    var text = '';
    if(item == null) item = 0;
    if(this.items[item].view) {
      var bgc = (this.items[item].level <= 1) ? this.itemBGColor1 : this.itemBGColor;
      text += '<table border=0 cellspacing=' + this.itemMargin + ' cellpadding=0 width=100%><tr>' +
              '<td' + (bgc ? ' bgcolor=' + bgc  : '') + '>' +
              '<table border=0 cellspacing=0 cellpadding=' + this.itemPadding + '><tr valign=top>';
      if(this.items[item].level > 1) {
        for(i = 1; i < this.items[item].level; i++) {
          text += '<td><img' +
                  ' src="' + this.imgBlank + '"' +
                  ' width=' + this.iconWidth +
                  ' border=' + 0 +
                  ' height=' + this.iconHeight +
                  '></td>';
        }
      }
      text += '<td>';
      if(this.items[item].knot) text += '<a href="javascript:mObj[' + this.mNr + '].viewMenu(' + item + ')">';
      text += '<img src="' + this.items[item].icon + '" border=0' +
              ' width=' + this.iconWidth +
			  ' border=' + "0" +
              ' height=' + this.iconHeight +
              '></a></td>' +
              '<td' + (this.itemWrap ? '>' : ' nowrap>') +
              '<a href="javascript:mObj[' + this.mNr + '].jump(' + item + ')" class="' +
              ((item == this.hilightItem) ? 'cssLinkHilight' : 'cssLink') + this.mNr + '">' +
              (this.itemBold ? '<b>' + this.items[item].text + '</b>' : this.items[item].text) +
              '</a></td></tr></table></td></tr></table>';
    }
    item++;
    if(item < this.items.length) text += this.content(item);
    return text;
  }

  this.newMenu = function() {
    var obj = this.getObj('divTreeMenu' + this.mNr);
    if(obj) {
      this.setItems();
      obj.innerHTML = this.content();
    }
  }

  this.buildContainer = function() {
    var lnk1 = '.cssLink' + this.mNr;
    var lnk2 = '.cssLinkHilight' + this.mNr;
    document.write('<style> .cssMenu' + this.mNr + ' { ' +
                   'width: ' + this.width + 'px; ' +
                   (this.bgColor ? 'background-color: ' + this.bgColor + '; ' : '') +
                   '} ' + lnk1 + ', ' + lnk1 + ':visited, ' + lnk1 + ':active { ' +
                   'color: ' + this.itemColor + '; ' +
                   'font-family: ' + this.itemFont + '; ' +
                   'font-size: ' + this.itemSize + 'px; ' +
                   'text-decoration: none; ' +
                   '} ' + lnk1 + ':hover { ' +
                   'text-decoration: none; ' +
                   '} ' + lnk2 + ', ' + lnk2 + ':visited, ' + lnk2 + ':active { ' +
                   'color: ' + this.itemActive + '; ' +
                   'font: bold; ' +
                   'font-family: ' + this.itemFont + '; ' +
                   'font-size: ' + this.itemSize + 'px; ' +
                 //  (this.itemActiveUnderline ? 'text-decoration: none; ' : '') +
                   '} </style>' +
                   '<div id="divTreeMenu' + this.mNr + '" class="cssMenu' + this.mNr + '"></div>');
  }

  this.create = function() {
    this.mNr = mObj.length;
    if(mObj[this.mNr] = this) {
      this.buildContainer();
      this.newMenu();
    }
    else alert("Erro!");
  }

  //------------------------------------------------------------------------
  // Argumentos: position level 1, [position level 2], ... [position level n]
  // Exemplo:   jumpTo(1, 3, 2, 1) ==> vai para o  menu item 1.3.2.1
  //
  this.jumpTo = function() {
    var pos, aktPos;
    var item = 0;
    var level = 1;
    for(var i = 0; i < this.items.length; i++) {
      if(this.items[i].level > 1) this.items[i].view = false;
    }
    this.actItem = -1;
    this.setItems();
    if(!arguments) var arguments = this.jumpTo.arguments;
    for(i = 0; i < arguments.length; i++, level++) {
      pos = arguments[i];
      for(aktPos = 0; item < this.items.length && aktPos < pos; item++) {
        if(this.items[item].level == level) aktPos++;
      }
      if(aktPos == pos) {
        item -= 1;
        this.openMenu(item);
      }
      else break;
    }
    if(item) this.jump(item);
  }
  //------------------------------------------------------------------------
}

function makeItem(level, text, url, target, onClick, openAll) {
  this.level = level;
  this.text = text;
  this.url = url;
  this.target = target;
  this.onClick = onClick;
  this.icon = '';
  this.view = (level <= 1) ? true : openAll;
  this.knot = false;
}

//----------------------------------------------------------------------------------------------------
