// Configuració

SituacioMenu = 'posicioMenu'
SituacioMenuNS = 'posicioMenuNS'

DestiDocuments = 'space'
DestiDocumentsNS = 'spaceNS'

ColorFons = '#93D9FF'
ColorFonsMouseOver = '#000079'

ColorFonsNivell = Array()
ColorFonsNivell[0] = ColorFons
ColorFonsNivell[1] = '#B7E5FF'

TancarPrimerNivell = 1

Identacio = 8

FontName = 'Verdana'
FontSize = 7
FontBold = 1
FontColor = '#006DB7'
FontColorMouseOver = '#FFFFFF'

FontNameNivell = Array()
FontSizeNivell = Array()
FontBoldNivell = Array()
FontColorNivell = Array()

FontNameNivell[0] = FontName
FontSizeNivell[0] = FontSize
FontBoldNivell[0] = FontBold
FontColorNivell[0] = FontColor


CarpetaOberta = "tridown.gif"
CarpetaTancada = "tri.gif"
CarpetaSense = "qua.gif"

BorderWidth = 1
BorderColor = '#000000'

TopPadding = 2
LeftPadding = 3


// Funcions

ImgCarpetaOberta = new Image()
ImgCarpetaOberta.src = CarpetaOberta

ImgCarpetaTancada = new Image()
ImgCarpetaTancada.src = CarpetaTancada

ImgCarpetaSense = new Image()
ImgCarpetaSense.src = CarpetaSense

Arbre_Array = new Array()

__DIV = document.getElementById ? 'DIV' : 'LAYER'

function getObjecte(id, doc)
{
  var obj = null;
  var vdoc = (doc == null) ? document : doc;

  if (vdoc.getElementById)
    obj = vdoc.getElementById(id)
  else
    if (vdoc.all)
      obj = vdoc.all[id]
    else
    {
      if (vdoc.layers)
      {
        obj = vdoc.layers[id]
        for (var i = 0; obj == null && i < vdoc.layers.length;  i++)
        {
          obj = getObjecte(id, vdoc.layers[i])
          
          if (obj == null) obj = getObjecte(id, vdoc.layers[i].document)
        }
      }      
    }
  
  return obj
}

function getEstil(id)
{
  var obj = getObjecte(id)
  
  if (obj != null)
    if (obj.style != null)
      obj = obj.style;
  
  return obj;
}

function Afegir_Fill(node)
{
  var index = this.fills.length 
  
  node.pare = this
  this.fills[index] = node
}

function Node_Arbre(nivell, index, texte, estat, url, finestra, alsada)
{
  this.nivell = nivell
  this.index = index
  this.texte = texte
  this.estat = estat
  this.url = url
  this.finestra = finestra
  this.pare = null
  this.visible = ''
  this.alsada = alsada
  this.fills = new Array()
  
  this.afegir_fill = Afegir_Fill
}

function Nou_Node(nivell, texte, estat, url, finestra, alsada)
{
  var index = Arbre_Array.length
  var nou_node = new Node_Arbre(nivell, index, texte, estat, url, finestra, alsada)
  
  Arbre_Array[index] = nou_node
  
  return nou_node
}

function Obre_Document(index)
{
  var vDesti = getObjecte(DestiDocuments);
  var vEstil = getEstil(DestiDocuments)
  var node = Arbre_Array[index];

  Canvia_Estat(index)

  if (vDesti == null) vDesti = getObjecte(DestiDocumentsNS)
  if (vEstil == null) vEstil = getEstil(DestiDocumentsNS)

  if (Trim(node.url))
    if (node.url.indexOf('javascript:') != -1)
      eval(node.url)
    else
      if (node.finestra == 'n')
      {
        if (node.alsada > 0)
          open(node.url, "finestra", "directories, location, menubar, resizable, scrollbars, status, toolbar, height=" + node.alsada)
        else
          open(node.url)
      }
      else
        if (vDesti.load)
        {
          if (node.alsada > 0) vDesti.clip.height = node.alsada
          vDesti.load(node.url, vDesti.clip.width)
        }
        else
          vDesti.innerHTML = '<IFRAME SCROLLING="YES" MARGINHEIGHT="0"' +
		    ' MARGINWIDTH="10" HSPACE="0" VSPACE="0" FRAMEBORDER="0"' +
   		    ' FRAMESPACING=0 BORDER="0" HEIGHT="' + (node.alsada > 0 ? node.alsada : vEstil.height) + '"' +
		    ' WIDTH="' + vEstil.width + '" SRC="' + node.url + '"></IFRAME>'
}

function Crear_Node(node, nivell, estat, txtAEstil, visibilitat, maxwidth)
{
  var imgCarpeta = ''
  
  var txtNode = ''
  var txtHREF = (node.fills.length == 0) ? 'Obre_Document(' + node.index + ')' : 'Obre_Document(' + node.index + ')'
  var txtDIVEstil = txtAEstil + '; display: ' + visibilitat
  var txtColorFons = ''
  
  var txtFontName = ''
  var txtFontSize = 0
  var txtFontColor = ''
  var txtFontBold = 0
  
  node.visible = visibilitat
 
  var vIdentacio = 0
  
  if (Identacio > 0) vIdentacio = nivell * Identacio

  if (!document.layers)  
    txtDIVEstil += '; padding: ' + TopPadding + 'pt ' + (vIdentacio + LeftPadding) + 'pt';
  
  if (node.fills.length > 0)
  {
    if (CarpetaOberta)
      if (estat == "open" && CarpetaOberta.length > 0) imgCarpeta = ImgCarpetaOberta.src
    
    if (CarpetaTancada)
      if (estat == "closed" && CarpetaTancada.length > 0) imgCarpeta = ImgCarpetaTancada.src
  }
  else if (CarpetaSense)
    if (CarpetaSense.length > 0) imgCarpeta = ImgCarpetaSense.src
  
  if (BorderWidth > 0)
  {
    if (node.index == 0) 
      txtDIVEstil += ';border-top: solid ' + BorderColor + ' ' + BorderWidth + 'px';
    txtDIVEstil += ';border-left: solid ' + BorderColor + ' ' + BorderWidth + 'px';
    txtDIVEstil += ';border-right: solid ' + BorderColor + ' ' + BorderWidth + 'px';   
    txtDIVEstil += ';border-bottom: solid ' + BorderColor + ' ' + BorderWidth + 'px';
  }
  
  txtNode = '<' + __DIV + ' ID="ARBRE_ARRAY_' + node.index + '"'
  
 
  if (FontNameNivell) if (FontNameNivell.length > 0)
      txtFontName = FontNameNivell[FontNameNivell.length > node.nivell ? node.nivell : FontNameNivell.length - 1]

  if (txtFontName.length == 0) if (FontName) if (FontName.length > 0) txtFontName = FontName

  if (FontSizeNivell) if (FontSizeNivell.length > 0)
      txtFontSize = FontSizeNivell[FontSizeNivell.length > node.nivell ? node.nivell : FontSizeNivell.length - 1]

  if (txtFontSize.length == 0) if (FontSize) txtFontSize = FontSize
  
  if (FontColorNivell) if (FontColorNivell.length > 0)
      txtFontColor = FontColorNivell[FontColorNivell.length > node.nivell ? node.nivell : FontColorNivell.length - 1]

  if (txtFontColor.length == 0) if (FontColor) if (FontColor.length > 0) txtFontColor = FontColor

  if (FontBoldNivell) if (FontBoldNivell.length > 0)
      txtFontBold = FontBoldNivell[FontBoldNivell.length > node.nivell ? node.nivell : FontBoldNivell.length - 1]

  if (txtFontBold.length == 0) if (FontBold) txtFontBold = FontBold
        
  if (ColorFonsNivell)
    if (ColorFonsNivell.length > 0)
      txtColorFons = ColorFonsNivell[ColorFonsNivell.length > node.nivell ? node.nivell : ColorFonsNivell.length - 1]

  if (txtColorFons.length == 0)
    if (ColorFons)
      if (ColorFons.length > 0)
        txtColorFons = ColorFons
        
  if (document.layers)
  {
    txtNode += ' WIDTH="' + maxwidth + '"'
    
    if (txtColorFons.length > 0)
      txtNode += 'bgcolor="' + txtColorFons + '"'
  }
  
  if (txtColorFons.length > 0)
    txtDIVEstil += '; background-color: ' + txtColorFons
    
  if (txtFontName.length > 0 ) txtDIVEstil += ';font-family: ' + txtFontName
  if (txtFontSize > 0) txtDIVEstil += ';font-size: ' + txtFontSize + 'pt'
  if (txtFontColor.length > 0) txtDIVEstil += ';color: ' + txtFontColor
  txtDIVEstil += ';font-weight: ' + (txtFontBold ? 'bold' : 'normal')
  
  txtNode += ' STYLE="' + txtDIVEstil + '"'
  txtNode += ' onMouseOver="Canvia_Barra(' + node.index + ')" onMouseMove="Canvia_Barra(' + node.index + ')" onMouseOut="Estil_Original(' + node.index + ')"'
  if (!document.layers)
    txtNode += ' onClick="' + txtHREF + '"'
  txtNode += '>'
  
  if (imgCarpeta.length > 0)
  {
    if (document.layers && vIdentacio > 0)
      txtNode += '<IMG SRC="tra.gif" WIDTH="' + vIdentacio + '" BORDER=0 ALIGN=TOP>'
    txtNode += '&nbsp;'
    txtNode += '<IMG'
    if (imgCarpeta != ImgCarpetaSense.src) txtNode += ' ID="IMATGE_ARBRE_' + node.index + '"'
    txtNode += ' SRC="' + imgCarpeta + '" BORDER="0" ALIGN="BOTTOM">'
    txtNode += '&nbsp;'
  }

  txtNode += node.texte
  txtNode += '</' + __DIV + '>'
  
  for (var i = 0; i < node.fills.length; i++)
    txtNode += Crear_Node(node.fills[i], nivell + 1, node.fills[i].estat, txtAEstil,  (node.estat == "closed" || visibilitat == "none") ? "none" : "block", maxwidth)
  
  return txtNode;
}

function Crear_Arbre()
{
  var tagDibuix = getObjecte(SituacioMenu)
  var estilTag = getEstil(SituacioMenu)
  
  if (tagDibuix == null) tagDibuix = getObjecte(SituacioMenuNS)
  if (estilTag == null) estilTag = getEstil(SituacioMenuNS)
  
  var txtAEstil = ''
  var vTexte = '';
  
  for (var i = 0; i < Arbre_Array.length; i++)
    if (Arbre_Array[i].nivell == 0)
      vTexte += Crear_Node(Arbre_Array[i], 0, Arbre_Array[i].estat, txtAEstil, "block", (document.layers) ? tagDibuix.clip.width : null)
  
  if (tagDibuix.innerHTML != null)
    tagDibuix.innerHTML = vTexte
  else
  {
    tagDibuix.visibility = 'hide'
    
    tagDibuix.bgColor = ColorFons

    tagDibuix.document.clear()
    tagDibuix.document.open('text/html')
    tagDibuix.document.writeln(vTexte)
    tagDibuix.document.close()
    
    RecolocaLayers()

    tagDibuix.visibility = 'visible'
  }  
}

function Canvia_Barra(index)
{
  var estil_div_node = getEstil('ARBRE_ARRAY_' + index)
  
  window.status = Arbre_Array[index].texte
  
  estil_div_node.cursor = (document.all) ? 'hand' : 'pointer'
  
  if (FontColorMouseOver)
    if (FontColorMouseOver.length > 0)
      if (estil_div_node.color)
        estil_div_node.color = FontColorMouseOver
      else
        Dibuixa_Node(index, FontColorMouseOver)
  
  if (ColorFonsMouseOver)
    if (ColorFonsMouseOver.length > 0)
      if (estil_div_node.bgColor)
        estil_div_node.bgColor = ColorFonsMouseOver
      else
        estil_div_node.backgroundColor = ColorFonsMouseOver
      
}

function Estil_Original(index)
{
  var estil_div_node = getEstil('ARBRE_ARRAY_' + index)
  var node = Arbre_Array[index]
  var txtColorFons = '';
  
  var txtFontColor = '';
  
  if (FontColorNivell) if (FontColorNivell.length > 0)
    txtFontColor = FontColorNivell[FontColorNivell.length > node.nivell ? node.nivell : FontColorNivell.length - 1]

  if (txtFontColor.length == 0) if (FontColor) if (FontColor.length > 0) txtFontColor = FontColor
  
    if (txtFontColor != '')
      if (estil_div_node.color)
        estil_div_node.color = txtFontColor
      else
        Dibuixa_Node(index, txtFontColor)
  
  if (ColorFonsNivell)
    if (ColorFonsNivell.length > 0)
      txtColorFons = ColorFonsNivell[ColorFonsNivell.length > node.nivell ? node.nivell : ColorFonsNivell.length - 1]

  if (txtColorFons.length == 0)
    if (ColorFons)
      if (ColorFons.length > 0)
        txtColorFons = ColorFons
        
  if (txtColorFons.length > 0)
    if (estil_div_node.bgColor)
      estil_div_node.bgColor = txtColorFons
    else
      estil_div_node.backgroundColor = txtColorFons
}

function Mostrar_Node(node, visibilitat)
{
  var estil_div_node = getEstil('ARBRE_ARRAY_' + node.index)
  
  node.visible = visibilitat
  estil_div_node.display = visibilitat
  
  for (var i = 0; i < node.fills.length; i++)
    Mostrar_Node(node.fills[i], (node.estat == "open" && visibilitat == "block") ? "block" : "none")
}

function Canvia_Estat_NS(evt)
{
  var vX = evt.pageX
  var vY = evt.pageY
  var vLeft = 0
  var vTop = 0
  var vRight = 0
  var vBottom = 0
  
  for (var i = 0; i < Arbre_Array.length; i++)
  {
    obj = getObjecte('ARBRE_ARRAY_' + i)
    
    if (obj.visibility != 'hide')
    {
      vLeft = obj.left
      vTop = obj.top
      
      vRight = vLeft + obj.clip.width
      vBottom = vTop + obj.clip.height
      
      while (obj.parentLayer != window)
      {
        obj = obj.parentLayer
        
        vLeft += obj.left
        vRight += obj.left
        
        vTop += obj.top
        vBottom += obj.top
      }
      
      if (vX > vLeft && vX < vRight && vY > vTop && vY < vBottom)
      {
        var node = Arbre_Array[i]
        
        if (node.fills.length == 0)
          Obre_Document(i)
        else
        {
          Canvia_Estat(i)
          RecolocaLayers()
        }
        return
      }
    }
  }
}

function Dibuixa_Node(index, color_font)
{
  var node = Arbre_Array[index]
  var obj = getObjecte('ARBRE_ARRAY_' + index)
  var vWidth = obj.clip.width
  
  var txtFontName = ''
  var txtFontSize = 0
  var txtFontBold = 0
  
  if (node.fills.length > 0)
  {
    if (node.estat == "open")
      if (CarpetaOberta && CarpetaOberta != "")
        img = ImgCarpetaOberta.src
      
    if (node.estat == "closed")
      if (CarpetaTancada && CarpetaTancada != "")
        img = ImgCarpetaTancada.src
  }
  else
    if (CarpetaSense && CarpetaSense != "")
      img = ImgCarpetaSense.src
      

  obj.document.clear()
  obj.document.open('text/html')
  
  if (BorderWidth)
  {
    obj.document.writeln('<LAYER LEFT="0" TOP="0" WIDTH="' + BorderWidth + '" HEIGHT="' + obj.clip.height + '" BGCOLOR="' + BorderColor + '"></LAYER>');
    obj.document.writeln('<LAYER LEFT="0" TOP="0" WIDTH="' + obj.clip.width + '" HEIGHT="' + BorderWidth + '" BGCOLOR="' + BorderColor + '"></LAYER>');
    obj.document.writeln('<LAYER LEFT="' + (obj.clip.width - BorderWidth) + '" TOP="0" WIDTH="' + BorderWidth + '" HEIGHT="' + obj.clip.height + '" BGCOLOR="' + BorderColor + '"></LAYER>');
    obj.document.writeln('<LAYER LEFT="0" TOP="' + (obj.clip.height - BorderWidth) + '" WIDTH="' + obj.clip.width + '" HEIGHT="' + BorderWidth + '" BGCOLOR="' + BorderColor + '"></LAYER>');
  }
  
  if (Identacio > 0)
    obj.document.write('<IMG SRC="tra.gif" HEIGHT="1" WIDTH="' + ( node.nivell * Identacio) + '" BORDER="0" ALIGN=TOP>')
    
  if (img != '')
    obj.document.write('&nbsp;<IMG SRC="' + img + '" BORDER=0 ALIGN=BOTTOM>&nbsp;')

  if (FontNameNivell) if (FontNameNivell.length > 0)
      txtFontName = FontNameNivell[FontNameNivell.length > node.nivell ? node.nivell : FontNameNivell.length - 1]

  if (txtFontName.length == 0) if (FontName) if (FontName.length > 0) txtFontName = FontName

  if (FontSizeNivell) if (FontSizeNivell.length > 0)
      txtFontSize = FontSizeNivell[FontSizeNivell.length > node.nivell ? node.nivell : FontSizeNivell.length - 1]

  if (txtFontSize.length == 0) if (FontSize) txtFontSize = FontSize
  
  if (FontBoldNivell) if (FontBoldNivell.length > 0)
      txtFontBold = FontBoldNivell[FontBoldNivell.length > node.nivell ? node.nivell : FontBoldNivell.length - 1]

  if (txtFontBold.length == 0) if (FontBold) txtFontBold = FontBold

  obj.document.write('<FONT COLOR="' + color_font + '" FACE="' + txtFontName + '" POINT-SIZE="' + txtFontSize + '">')
  if (txtFontBold) obj.document.write('<B>')
  obj.document.write(node.texte)
  if (txtFontBold) obj.document.write('</B>')
  obj.document.write('</FONT>')
  
  obj.document.close()

  obj.clip.width = vWidth
}

function RecolocaLayers()
{
  var i = 0;
  var dalt = 0;
  var obj = null;
  var node = null;
  var img = '';
  
  var txtFontColor = ''
  
  for (; i < Arbre_Array.length; i++)
  {
    txtFontColor = ''
    
    node = Arbre_Array[i]
    obj = getObjecte('ARBRE_ARRAY_' + i)
    
    if (FontColorNivell) if (FontColorNivell.length > 0)
      txtFontColor = FontColorNivell[FontColorNivell.length > node.nivell ? node.nivell : FontColorNivell.length - 1]

    if (txtFontColor.length == 0) if (FontColor) if (FontColor.length > 0) txtFontColor = FontColor
  
    document.captureEvents(Event.MOUSEUP)
    document.onmouseup = Canvia_Estat_NS
    
    obj.visibility = (node.visible != 'none') ? 'inherit' : 'hide'
    
    if (node.visible != 'none')
    {
      Dibuixa_Node(i, txtFontColor)
      
      obj.moveTo(obj.left, dalt)
      dalt = dalt + obj.clip.height
    }
    
  }
}

function Canvia_Estat(index)
{
  var node = Arbre_Array[index];
  
  node.estat = (node.estat == "open") ? "closed" : "open"
  node.visible = "block"
  
  if (TancarPrimerNivell && TancarPrimerNivell == 1)
    if (node.estat == "open" && node.nivell == 0)
    {
      for (var i = 0; i < Arbre_Array.length; i++)
        if (i != index && Arbre_Array[i].nivell == 0 && Arbre_Array[i].estat == "open")
          Canvia_Estat(i);
    }
  
  var img_node = getObjecte('IMATGE_ARBRE_' + node.index)
  
  if (img_node != null)
  {
    if (node.estat == "open" && CarpetaOberta && CarpetaOberta != "") img_node.src = ImgCarpetaOberta.src
    if (node.estat == "closed" && CarpetaTancada && CarpetaTancada != "") img_node.src = ImgCarpetaTancada.src
  }
  
  for (var i = 0; i < node.fills.length; i++)
    Mostrar_Node(Arbre_Array[index].fills[i], (node.estat == "open") ? "block" : "none")
   
}

function Trim(pCadena, pDireccio)
{
  var vCadena = pCadena;
  
  if (arguments.length == 1) pDireccio = "AMBDOS";
  
  if (pDireccio.toUpperCase() == "LEFT" || pDireccio == "AMBDOS")
    while (vCadena.substring(0, 1) == " ") vCadena = vCadena.substring(1);
  
  if (pDireccio.toUpperCase() == "RIGHT" || pDireccio == "AMBDOS")
    while (vCadena.substring(vCadena.length - 1) == " ") vCadena = vCadena.substring(0, vCadena.length - 1);
  
  return vCadena;
}

function Crea_Menu_Array(pCadena, pIdx)
{
  var aCadena = pCadena.split(";")
  var vProfund = parseInt(aCadena[2])
  var vAlsada = parseInt(aCadena[4])
  
  var node = Nou_Node(vProfund, Trim(aCadena[0]), "closed", aCadena[1], aCadena[3], vAlsada)

  if (vProfund > 0)
  {
    var i = Arbre_Array.length - 1
    var node_pare = null;
    
    while (i >= 0 && node_pare == null)
    {
      if (Arbre_Array[i].nivell == vProfund - 1)
        node_pare = Arbre_Array[i]
      else
        i--;
    }
    
    if (node_pare != null)
      node_pare.afegir_fill(node)
      
  }  
  
}

function Construir_Arbre()
{
  var vEstilDesti = getEstil(DestiDocuments)
  var vEstilMenu = getEstil(SituacioMenu)
  
  if (vEstilDesti == null) vEstilDesti = getEstil(DestiDocumentsNS)
  if (vEstilMenu == null) vEstilMenu = getEstil(SituacioMenuNS)
  
  if (vEstilDesti.clip)
  {
    vEstilDesti = vEstilDesti.clip
    vEstilMenu = vEstilMenu.clip
  }
    
  if (screen.availWidth >= 1024)
  {
    vEstilDesti.width = 830
    vEstilDesti.height = 550
    vEstilMenu.height = 550
  }
  else
  {
    vEstilDesti.width = 600
    vEstilDesti.height = 380
    vEstilMenu.height = 380
  }

  for (var i = 0; i < Menu.length; i++)
    Crea_Menu_Array(Menu[i], i);

  Crear_Arbre()

  Obre_Document(0)
}

function Control_Alsada(obj)
{
  if (screen.availWidth >= 1024)
    obj.clip.height = 550
  else
    obj.clip.height = 380
  
  if (obj.document.height > obj.clip.height)
  { 
    var vv = obj.document.height - obj.clip.height
    
    obj.resizeBy(0, vv)
  }
}