/* Menu Behaviour */
  
  var maxItemsLevel0 = 12;
  var maxItemsLevel1 = 3;
    
  // enlisto los items, con sus sub-items, y sus estados correspondientes
  var level0_items = ["item_1", "item_2", "item_8", "item_10"];
  var level0_subs = ["item_1_sub", "item_2_sub", "item_8_sub", "item_10_sub"];
  var level0_states = ["close", "close", "close", "close"];
  
  var level1_items = ["item_1_3", "item_2_1", "item_8_1", "item_8_2", "item_10_1", "item_10_2"]
  var level1_subs = ["item_1_3_sub", "item_2_1_sub", "item_8_1_sub", "item_8_2_sub"];
  var level1_states = ["close", "close", "close", "close"];
  
  // armo los estilos correspondientes a los estados
  var level0_styles = {"open" : {fontWeight: "bold",
                                 borderLeft: "5px solid #e32881",
                                 padding: "0 0 0 6px",
                                 backgroundColor: "#594853",
                                 backgroundImage: "url('css/images/item_flechita_up.gif')",
                                 backgroundPosition: "93% 50%",
                                 backgroundRepeat: "no-repeat"
                                },
                       "close" : {fontWeight: "normal",
                                 borderLeft: "none",
                                 padding: "0 0 0 11px",
                                 backgroundColor: "transparent",
                                 backgroundImage: "url('css/images/item_flechita_down.gif')",
                                 backgroundPosition: "93% 50%",
                                 backgroundRepeat: "no-repeat"
                                 }
                      };
                      
  var level1_styles = {"open" : {color: "#ffffff",
                                 backgroundImage: "url('css/images/menu_flechita_level1_over.gif')"
                                },
                       "close" : {color: "#cccccc",
                                  backgroundImage: "url('css/images/menu_flechita_level1_up.gif')"
                                 }
                      };
                      
  var level2_styles = {"open" : {color: "#ffffff",
                                 backgroundImage: "url('css/images/menu_flechita_level2_over.gif')"
                                },
                       "close" : {color: "#cccccc",
                                  backgroundImage: "url('css/images/menu_flechita_level2_up.gif')"
                                 }
                      };
  
  // cierra todo el arbol
  function closeAll() {
      // recorro el primer nivel y me fijo cuales tienen sub y los cierro
      for (var i = 1; i <= maxItemsLevel0; i++) {
        var id_sub0 = "item_"+i+"_sub";
        if ($(id_sub0)) {
          $(id_sub0).setStyle({display: 'none'});
        }
        // recorro el segundo nivel y me fijo cuales tienen sub y los cierro
        for (var j = 1; j <= maxItemsLevel1; j++) {
          var id_sub1 = "item_"+i+"_"+j+"_sub";
          if ($(id_sub1)) {            
            $(id_sub1).setStyle({display: 'none'});
          }
        }
      }
  }
  
  // funcion que determina el comportamiento del menu
  function btnBehaviour(arrayItems, arraySubs, arrayStates, arrayStyles) {
      for (var i = 0; i < arrayItems.length; i++) {
          var btns = $(arrayItems[i]);
          btns.num = i;
          btns.onclick = function() {
              // evalua el estado actual del boton
              if (arrayStates[this.num] == "close") {
                  for (var j = 0; j < arrayStates.length; j++) {
                      // si hay otro abierto del mismo nivel, lo cierra
                      if (arrayStates[j] == "open") {
                          Effect.SlideUp(arraySubs[j], {duration:0.5});
                          arrayStates[j] = "close";
                          $(arrayItems[j]).setStyle(arrayStyles['close']);
                      }
                  }
                  // restituyo el activo a normal
                  /*if (eval(actualItem).length == 2) {
                    $(actualItem).setStyle(level0_styles['close']);
                  } else if (eval(actualItem).length == 3) {
                    $(actualItem).setStyle(level1_styles['close']);
                  } else if (eval(actualItem).length == 4) {
                    $(actualItem).setStyle(level2_styles['close']);
                  }*/
                  // abro el sub y determino el estilo del boton
                  Effect.SlideDown(arraySubs[this.num], {duration:0.5});
                  arrayStates[this.num] = "open";
                  $(arrayItems[this.num]).setStyle(arrayStyles['open']);
              } else if (arrayStates[this.num] == "open") {
                  // cierro el sub y determino el estilo del boton
                  Effect.SlideUp(arraySubs[this.num], {duration:0.5});
                  arrayStates[this.num] = "close";
                  $(arrayItems[this.num]).setStyle(arrayStyles['close']);
              }
          }
          btns.onmouseover = function() {
              if (arrayStates[this.num] == "close") {
                  $(arrayItems[this.num]).setStyle(arrayStyles['open']);
              }
          }
          btns.onmouseout = function() {
              if (arrayStates[this.num] == "close") {
                  $(arrayItems[this.num]).setStyle(arrayStyles['close']);
              }
          }
      }      
  }
  
  function eval(id) {
    // veo el nivel al cual pertenece
    var identify = id.split("_");
    return identify;
  }
  
  function showActual(id) {    
    // primer nivel?
    if (eval(id).length == 2) {
      if (id == 'item_1' || id == 'item_2' || id == 'item_8') {
        $(id).setStyle(level0_styles['open']);
      } else {
        $(id).setStyle({fontWeight: "bold", borderLeft: "5px solid #e32881", padding: "0 0 0 6px", backgroundColor: "#594853"});        
      }
    }
    // segundo nivel?
    else if (eval(id).length == 3) {
      $(id).setStyle(level1_styles['open']);
      var id_parent = "item_"+eval(id)[1];
      var sub = "item_"+eval(id)[1]+"_sub";
      $(id_parent).setStyle(level0_styles['open']);
      $(sub).setStyle({display: "block"});
      
      for (var i = 0; i < level0_items.length; i++) {
        if (id_parent == level0_items[i]) {
          level0_states[i] = "open";
        }
      }
    }
    // tercer nivel
    else if (eval(id).length == 4) {
      $(id).setStyle(level2_styles['open']);
      var id_parent_parent = "item_"+eval(id)[1];
      var id_parent = "item_"+eval(id)[1]+"_"+eval(id)[2];
      var sub_parent = "item_"+eval(id)[1]+"_sub";
      var sub = "item_"+eval(id)[1]+"_"+eval(id)[2]+"_sub";
      $(id_parent_parent).setStyle(level0_styles['open']);
      $(id_parent).setStyle(level1_styles['open']);
      $(sub_parent).setStyle({display: "block"});
      $(sub).setStyle({display: "block"});
      
      for (var i = 0; i < level0_items.length; i++) {
        if (id_parent_parent == level0_items[i]) {
          level0_states[i] = "open";
        }
      }
      for (var i = 0; i < level1_items.length; i++) {
        if (id_parent == level1_items[i]) {
          level1_states[i] = "open";
        }
      }   
    }
  }
  
  function applyInitStyles(items, styles) {
    for (var i = 0; i < items.length; i++) {
      $(items[i]).setStyle(styles['close']);
    }
  }
  
  function overrideHref(items) {    
    for (var i = 0; i < items.length; i++) {
      $(items[i]).href = "javascript:void(0);";
    }
  }
  
  function highlightFestival() {
    $('item_8').setStyle({height: "50px"});
    caca = Builder.node('div', {id: 'festival_highlight'}, '-> ¡Edición 2008! <-');
    $('item_8').appendChild(caca);
  }
  
  function init() {
      closeAll();
      applyInitStyles(level0_items, level0_styles);
      overrideHref(level0_items);
      btnBehaviour(level0_items, level0_subs, level0_states, level0_styles);
      btnBehaviour(level1_items, level1_subs, level1_states, level1_styles);
      showActual(actualItem);
      highlightFestival();
  }

