function infraArvore(divId,arrNos){
  
  var me = this;
  
  this.nodes = arrNos;
  this.nosAbertos	= new Array();
  this.icons	= new Array(6);
  this.div = document.getElementById(divId);
  this.noAtual = null;
  this.pathToImages = '/infra_js/arvore/';
  

  this.carregarIcones = function() {
    me.icons[0] = new Image();
    me.icons[0].src = me.pathToImages + 'plus.gif';
    me.icons[1] = new Image();
    me.icons[1].src = me.pathToImages + 'plusbottom.gif';
    me.icons[2] = new Image();
    me.icons[2].src = me.pathToImages + 'minus.gif';
    me.icons[3] = new Image();
    me.icons[3].src = me.pathToImages + 'minusbottom.gif';
    me.icons[4] = new Image();
    me.icons[4].src = me.pathToImages + 'folder.gif';
    me.icons[5] = new Image();
    me.icons[5].src = me.pathToImages + 'folderopen.gif';
  }
  
  this.inicializar = function() {
    
    if (me.nodes.length > 0) {
      
      var j = 0;
      for(var i=0;i<me.nodes.length;i++){
        if (me.nodes[i].bolAberto){
          me.nosAbertos[j++] = me.nodes[i].id;
        }
      }
    
      me.div.className = 'infraArvore';
      
      me.carregarIcones();

      var no = me.nodes[0];
      
      var a = document.createElement('a');
      a.id = 'anchor' + no.id;
      a.align = 'absbottom';
      a.target = no.target;
      a.href = no.href;
      a.disabled = !no.bolHabilitado;
      
      var img = me.criarImg(no.icone);
      img.id = 'icon' + no.id;
      a.appendChild(img);
      var span = me.criarSpan(no);
      span.id = 'span' + no.id;
      
      a.appendChild(span);
      
      a.onclick = function(e){
        
          ret = true;
          
          var no = me.getNo(this.id.substr(6));
          
          if (!no.bolHabilitado){
            ret = false;
          }else{
            
            if (typeof(no.processar)=='function'){
              ret = no.processar(no);
            }else if (typeof(me.processar)=='function'){
              ret = me.processar(no);
            }
            
            if (ret){
              me.setNoSelecionado(no);
            }
            
          }
          
          return ret;
      };
        
      a.onblur = function(){
        var span = document.getElementById('span'+this.id.substr(6));
        if (span.className == 'infraArvoreNoSelecionado'){
          span.className = 'infraArvoreNoVisitado';
        }
      };
      
      me.div.appendChild(a);

      var imgEspaco = null;
      for(var k=0;k<no.iconesAdicionais.length;k++){
        
        if (k > 0){
          imgEspaco = document.createElement('img');
          imgEspaco.src = '/infra_css/imagens/espaco.gif';
          me.div.appendChild(imgEspaco);
        }
        
        img = me.criarImg(no.iconesAdicionais[k][0],no.iconesAdicionais[k][1]);
        me.div.appendChild(img);
        
      }
      
    	var quebra = document.createElement('span');
     	quebra.innerHTML = '<br />';
      me.div.appendChild(quebra);
      
      var divFilhos = document.createElement('div');
      divFilhos.id = 'div' + no.id; 
      divFilhos.className = 'infraArvore';
      me.div.appendChild(divFilhos);

      var recursedNodes = new Array();
      me.adicionarNo(no.id, recursedNodes);
    }
  }

  this.criarImg = function(src,title){
  	var img = document.createElement('img');	
    img.src =  src;
    img.align = 'absbottom';
    if (title!=undefined){
      img.title = title;
    }
  	return img;
  }

  this.criarSpan = function(no){
    //var div = document.createElement('div');
    //div.className = 'infraArvoreTeste';
  	var span = document.createElement('span');
  	//span.innerHTML = no.label + '<br />';
  	span.innerHTML = no.label + ' ';
  	span.align = 'absbottom';
  	span.title = no.title;
  	//div.appendChild(span);
  	return span;
  }
  
  this.getNo = function(nodeId){
    for (var i=0; i<me.nodes.length; i++) {
      if (me.nodes[i].id == nodeId){
        return me.nodes[i];
      }
    }
    return null;
  }
  
  this.isNoAberto = function(node) {
    for (var i=0; i<me.nosAbertos.length; i++)
    if (me.nosAbertos[i]==node) return true;
    return false;
  }
  
  this.temFilhos = function(parentNode) {
    for (var i=0; i< me.nodes.length; i++) {
      if (me.nodes[i].idPai == parentNode) return true;
    }
    return false;
  }
  
  this.ultimoRamo = function(node, parentNode) {
    var lastChild = 0;
    for (var i=0; i< me.nodes.length; i++) {
      if (me.nodes[i].idPai == parentNode){
        lastChild = me.nodes[i].id;
      }
    }
    if (lastChild==node) return true;
    return false;
  }
  
  this.adicionarNo = function(parentNode, recursedNodes) {
    var a = null;
    var img = null;
    var span = null;
    var div = document.getElementById('div' + parentNode);
    
    for (var i = 0; i < me.nodes.length; i++) {
  
      var no = me.nodes[i];
      
      if (no.idPai == parentNode) {
  
        var ls	= me.ultimoRamo(no.id, no.idPai);
        var hcn	= me.temFilhos(no.id);
        var ino = me.isNoAberto(no.id);
  
        for (var g=0; g<recursedNodes.length; g++) {
          if (recursedNodes[g] == 1) {
            div.appendChild(me.criarImg(me.pathToImages + 'line.gif'));
          }else {
            div.appendChild(me.criarImg(me.pathToImages + 'empty.gif'));
          }
        }
  
        if (ls) {
          recursedNodes.push(0);
        }
        else {
          recursedNodes.push(1);
        }
  
        if (hcn) {
        
          a = document.createElement('a');
          a.align = 'absbottom';
          img = null;

          if (ls) {
            if (ino){
              img = me.criarImg(me.pathToImages + 'minusbottom.gif');
            }else{
              img = me.criarImg(me.pathToImages + 'plusbottom.gif');
            }
            
            img.onclick = function(){
              me.processarNo(this.id.substr(4),1);
              return false;
            }
          } else {
            if (ino){
              img = me.criarImg(me.pathToImages + 'minus.gif');
            }else{
              img = me.criarImg(me.pathToImages + 'plus.gif');
            }
            
            img.onclick = function(){
              me.processarNo(this.id.substr(4),0);
              return false;
            }
          }
          
          img.id = 'join' + no.id;          
          a.appendChild(img);
          div.appendChild(a);
        } else {
          if (ls){
            div.appendChild(me.criarImg(me.pathToImages + 'joinbottom.gif')); 
          }else{
            div.appendChild(me.criarImg(me.pathToImages + 'join.gif'));
          }
        }
  
        a = document.createElement('a');
        a.id = 'anchor' + no.id;
        a.align = 'absbottom';
        a.target = no.target;
        a.href = no.href;
        a.disabled = !no.bolHabilitado;
        
        if (hcn) {
          if (ino){
            img = me.criarImg(no.iconeAberto);
          }else{
            img = me.criarImg(no.iconeFechado);
          } 
        } else{
          img = me.criarImg(no.icone);
        }
        
        img.id = 'icon' + no.id;
        a.appendChild(img);
        span = me.criarSpan(no);
        span.id = 'span' + no.id;
        
        a.appendChild(span);
        
        a.onclick = function(){
          
          ret = true;
          
          var no = me.getNo(this.id.substr(6));

          if (!no.bolHabilitado){
            ret = false;
          }else{
          
            if (typeof(no.processar)=='function'){
              ret = no.processar(no);
            }else if (typeof(me.processar)=='function'){
              ret = me.processar(no);
            }
            
            if (ret){
              me.setNoSelecionado(no);
            }
          }
          
          return ret;
        }
        
        a.onblur = function(){
          var span = document.getElementById('span'+this.id.substr(6));
          if (span.className == 'infraArvoreNoSelecionado'){
            span.className = 'infraArvoreNoVisitado';
          }
          return true;
        }
        
        div.appendChild(a);
        
        var imgEspaco = null;
        for(var k=0;k<no.iconesAdicionais.length;k++){
          
          if (k > 0){
            imgEspaco = document.createElement('img');
            imgEspaco.src = '/infra_css/imagens/espaco.gif';
            div.appendChild(imgEspaco);
          }
          
          img = me.criarImg(no.iconesAdicionais[k][0],no.iconesAdicionais[k][1]);
          div.appendChild(img);
        }
        
      	var quebra = document.createElement('span');
       	quebra.innerHTML = '<br />';
        div.appendChild(quebra);
        
        
        if (hcn) {
          var divFilhos = document.createElement('div');
          divFilhos.id = 'div' + no.id; 
          divFilhos.className = 'infraArvore';
          if (!ino){
            divFilhos.style.display = 'none';
          }
          div.appendChild(divFilhos);
          me.adicionarNo(no.id, recursedNodes);
        }
  
        recursedNodes.pop();
      }
    }
  }
  
  // Opens or closes a node
  this.processarNo = function (node, bottom) {
  
    var theDiv = document.getElementById("div" + node);
    var theJoin	= document.getElementById("join" + node);
    var theIcon = document.getElementById("icon" + node);
  
    var no = me.getNo(node);
    
    if (theDiv.style.display == 'none') {
      if (bottom==1) {
        theJoin.src = me.icons[3].src;
      }else{ 
        theJoin.src = me.icons[2].src;
      }
      theIcon.src = no.iconeAberto;
      theDiv.style.display = '';
      
    } else {
      if (bottom==1) { 
        theJoin.src = me.icons[1].src;
      }else{
        theJoin.src = me.icons[0].src;
      }
      theIcon.src = no.iconeFechado;
      theDiv.style.display = 'none';
    }
    
    me.setNoSelecionado(no);    
    
  }

  this.setNoSelecionado = function(novoNo){
    if (me.noAtual != null){
      document.getElementById('span'+me.noAtual.id).className ='';
    }
    me.noAtual = novoNo;
    var span = document.getElementById('span'+me.noAtual.id);
    span.className = 'infraArvoreNoSelecionado';
  }
  
  this.getNoSelecionado = function(){
    return me.noAtual;
  }
  
  this.getAncoraNo = function(id){
    return (me.div.getElementById('anchor' + id));
  }
  
  if(!Array.prototype.push) {
    function array_push() {
      for(var i=0;i<arguments.length;i++)
        this[this.length]=arguments[i];
      return this.length;
    }
    Array.prototype.push = array_push;
  }
  
  if(!Array.prototype.pop) {
    function array_pop(){
      lastElement = this[this.length-1];
      this.length = Math.max(this.length-1,0);
      return lastElement;
    }
    Array.prototype.pop = array_pop;
  }
  
  me.inicializar();
}

function infraArvoreNo(tipo, id, idPai, label, title, href, target){
  this.tipo = tipo;
  this.id = id;
  this.idPai = idPai;
  this.label = label;
  this.title = title;
  
  if (href != undefined){
    this.href = href;
  }else{
    this.href = 'about:blank';
  }
    
  if (target != undefined){
    this.target = target;
  }else{
    this.target = null;
  }

  this.bolAberto = false;
  this.bolHabilitado = true;

  this.pathToImages = '/infra_js/arvore/';
  this.icone = this.pathToImages + 'page.gif'
  this.iconeAberto = this.pathToImages + 'folderopen.gif';
  this.iconeFechado = this.pathToImages + 'folder.gif';
  this.iconesAdicionais = Array();
}