/*
 * Funciones que se usan en la búsqueda (views/busqueda/index)
*/

function show_last_delete_item() {
  for (i=0; i< document.getElementsByClassName('del-criterio').length; i++) {
    var this_del_id = document.getElementsByClassName('del-criterio')[i].id;
    if (i < document.getElementsByClassName('del-criterio').length-1) {
      document.getElementById(this_del_id).style.visibility = "hidden";
    } else {
      document.getElementById(this_del_id).style.visibility = "visible";
      var id_part = this_del_id.split(/-/);
      var last_id = id_part[id_part.length-1];

      $("last_res_link").innerHTML = "<a href=\"/registros/criterio/"+last_id+"\" accesskey=\"l\">enlace al último resultado</a>"  
    }
  }
}


function item_added() {
  // Firefox 1.5 considera como nodos (vacíos) los divs que contienen otros divs.
  // Seguimos buscando hasta que haya uno con id
  var element = $('criterioList').lastChild;
  while (element.id == undefined) {
    element = element.previousSibling;
  }
  var element_id = element.id;
  new Effect.Appear(element_id);

  $('form-submit-button').disabled = false;
  Element.hide('criterio_loading');

  $("n-busqueda").style.display="block";
  $("search_info").innerHTML = "Para seguir buscando entre los resultados añada otro criterio, o realice una <a href=\"/busqueda/new\">nueva búsqueda</a>.";

  window.setTimeout(emphasizeInfoMessage,10000);

  $('info_msg').style.visibility='hidden';
  // Volver a poner el focus en el input que estaba activo antes.
  initialize_inputs();
  show_last_delete_item();
}

function initialize_inputs() {

  var criterio_inputs = new Array("criterio_query_string", "descriptor_geo", "descriptor_them", "descriptor_author", "descriptor_publisher", "descriptor_all");

  for (i = 0; i < criterio_inputs.length; i++) {
       this_input = $(criterio_inputs[i]);
      this_input.value = "";
     if (this_input.style.display == "block") {
        this_input.focus();
     }
  }
  document.getElementById('descriptor_them').style.position = 'relative';
  $('criterioForm').style.position = 'static';
}

function item_deleted(node) {
  Effect.Fade($(node));
  // $(node).style.display='none';
  // Element.remove(node);

  show_last_delete_item();

  initialize_inputs();

}

function item_loading() {
   $('criterio_nom_id').focus();
  $('form-submit-button').disabled = true;
  Element.show('criterio_loading');
}

window.onload = function() {
  show_last_delete_item();
}

// Reglas
function changeOptions() {

  var nom=document.forms['criterioform'].criterio_nom_id;
  // Esti: estaría mejor así, pero si cambio el name de este select,
  // deja de funcionar (y no sé cómo usar aqui criterio[regla_id])
  // var x=document.forms['criterioform'].criterio[regla_id];
  var x=document.getElementById('criterio_regla_id');

  while (x.length > 0) {
    x.remove(0);
  }

  if (nom.options[nom.selectedIndex].value == "year") {
      x.style.visibility="visible";
      x.size="3";

      x.options[0] = new Option("antes de","less", true);
      x.options[1] = new Option("después de","greater");
      x.options[2] = new Option("igual a","equal");
  } else {
      x.style.visibility="hidden";
      x.size="0";

      x.options[0] = new Option("contiene","contains", true);
  }

  var qf = document.getElementById("criterio_query_string");
  var geo_f = document.getElementById("descriptor_geo");
  var them_f = document.getElementById("descriptor_them");
  var author_f = document.getElementById("descriptor_author");
  var publisher_f = document.getElementById("descriptor_publisher");
  var all_f = document.getElementById("descriptor_all");

  qf.value = '';
  geo_f.value = '';
  them_f.value = '';
  author_f.value = '';
  publisher_f.value = '';
  all_f.value = '';

  if (nom.options[nom.selectedIndex].value == "geo") {
    qf.style.display = 'none';
    them_f.style.display = 'none';
    author_f.style.display = 'none';
    publisher_f.style.display = 'none';
    all_f.style.display = 'none';
    geo_f.style.display = 'block';
    geo_f.focus();
  } else if (nom.options[nom.selectedIndex].value == "author") {
    geo_f.style.display = 'none';
    qf.style.display = 'none';
    them_f.style.display = 'none';
    publisher_f.style.display = 'none';
    all_f.style.display = 'none';
    author_f.style.display = 'block';
    author_f.focus();
  } else if (nom.options[nom.selectedIndex].value == "publisher") {
    geo_f.style.display = 'none';
    qf.style.display = 'none';
    them_f.style.display = 'none';
    author_f.style.display = 'none';
    all_f.style.display = 'none';
    publisher_f.style.display = 'block';
    publisher_f.focus();
  } else if (nom.options[nom.selectedIndex].value == "descriptor") {
    geo_f.style.display = 'none';
    qf.style.display = 'none';
    author_f.style.display = 'none';
    publisher_f.style.display = 'none';
    all_f.style.display = 'none';
    them_f.style.display = 'block';
    them_f.focus();
  } else if (nom.options[nom.selectedIndex].value == "all") {
    geo_f.style.display = 'none';
    qf.style.display = 'none';
    author_f.style.display = 'none';
    publisher_f.style.display = 'none';
    them_f.style.display = 'none';
    all_f.style.display = 'block';
    all_f.focus();
  } else {
    geo_f.style.display = 'none';
    them_f.style.display = 'none';
    author_f.style.display = 'none';
    publisher_f.style.display = 'none';
    all_f.style.display = 'none';
    qf.style.display = 'block';
    qf.focus();
  }
}

function emphasizeInfoMessage() {
  new Effect.Pulsate('search_info', {from:0.5})
  new Effect.Highlight('search_info', {})
}
