/*! 
 * mia estensione della jquery 
 * jQuery JavaScript Library v1.3.2 (deve essere inclusa)
 * version 1.0 07/12/2009
 */

// onload
$(window).load(function () {
  $(".JQ_hidden").hide();
  // $("#JQ_hid_popup").hide();
});

/**
 * cattura il click
 **/
$(document).ready(function (){

  // link su JQ_link
  $("a.JQ_link").toggle(function(){
    $(".JQ_hidden").animate({ height: 'show', opacity: 'show' }, 'slow');
  },function(){
    $(".JQ_hidden").animate({ height: 'hide', opacity: 'hide' }, 'slow');
  });

 });

// popup in JQ
/* $(document).ready
(
  function ()
  {
    $(".JQ_opener_popup").toggle(
      function(){
		$("#JQ_hid_popup").animate({ height: 'show', opacity: 'show' }, 'slow');
      },
      function(){
        $("#JQ_hid_popup").animate({ height: 'hide', opacity: 'hide' }, 'slow');
      } 
    );
  }
);
$(document).ready
(
  function ()
  {
    $("#JQ_hid_popup").toggle(
      function(){
        $("#JQ_hid_popup").animate({ height: 'show', opacity: 'show' }, 'slow');
      },
      function(){
        $("#JQ_hid_popup").animate({ height: 'hide', opacity: 'hide' }, 'slow');
      } 
    );
  }
); */

/**
 * rende visibile un livello liv_id caricandogli dentro i dati 'liv_name'
 **/
function apri_JQ(uri, liv_name)
{
  write_get(uri, liv_name);
  //show_it(liv_id);
  
  //alert(liv_name);
  
  if(document.getElementById(liv_name).style.visibility == 'visible')
  {
    document.getElementById(liv_name).style.visibility = 'hidden';
  }
  else
  {
    document.getElementById(liv_name).style.visibility = 'visible';
  }
}

/**
 * rende visibile il livello con id JQ_hid_popup e gli carica i dati presi da una chiamata remota ad uri
 **/
function apri_JQ_popup(uri)
{
  //alert(uri);
  apri_JQ(uri, "JQ_hid_popup");
}

/**
 * sceglie la tab della ricerca attivando e disattivando le classi
 **/
function select_search(liv)
{
  // nascondo tutti i link e mostro solo il selected
  //$("#search_nav a").removeClass("selected");
  //$("#search_nav "+selected).addClass("selected");
  
  $("#adv-search_network").addClass("invisibile");
  $("#adv-search_films").addClass("invisibile");
  $("#adv-search_festivals").addClass("invisibile");
  
  // mostro il liv selezionato
  $(liv).removeClass("invisibile");
}

/**
 * apre il div di alert e ci scrive il messaggio
 **/
function msg_into_div(msg, id_div)
{
  //alert(\'qui\');
  $("#"+id_div).html(msg);
}

/**
 * prende l'html di una chiamata get e lo spara in un div
 * necessita di jquery-1.3.2.js
 * @param String uri
 * @param String livello
 **/
function write_get(uri, id_livello)
{
  $.get(uri, function(data)
  {
     //alert("Data Loaded: " + data); 
     $('#'+id_livello).html(data);
  });
  
  return false;
}

/**
 * prepara i dati del form
 **/
function prepara_dati(id_form)
{
  stringa = "";
  //var form = document.forms[0];
  var form = document.getElementById(id_form);
  azione = form.action;
  //alert(form.action);
  
  var numeroElementi = form.elements.length;
  //alert(numeroElementi);
  
  for(var i=0 ; i<numeroElementi ; i++)
  {
    // if(i < numeroElementi-1)
	if(i < numeroElementi)
	{
	  //if(form.elements[i].length==1)
		stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
    }
	/*else
	{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    }*/
  }
  
  // dice al php che la chiamata è ajax
  stringa += 'ajax=OK';
  //alert(stringa);
}

/**
 * prepara l'header della chiamata http
 **/
function execfunc()
{
  myReq.onreadystatechange = handleResponse;
  myReq.open("POST", azione, true);
  myReq.setRequestHeader("Content-Type", tipo_req+" charset=UTF-8");
  myReq.send(stringa);
}

/**
 * AJAX! req
 **/
function AJAXReq()
{
  if(window.XMLHttpRequest)
  {
    myReq = new XMLHttpRequest();
  }
  else
  {
    if(window.ActiveXObject)
    {
      myReq = new ActiveXObject("Microsoft.XMLHTTP");
    
      if(!myReq)
	  {
        myReq = new ActiveXObject("Msxml2.XMLHTTP");
      }
    }
  }
  
  if(myReq)
  {
    execfunc();
  }
  else
  {
    alert("Problemi nella esecuzione dello script");
  }
}

/**
 * Gestisce il risultato (testualmente)
 **/
function handleResponse()
{
  // esito ok
  if(myReq.readyState==4)
  {
	// http 200 OK
    if(myReq.status==200){
      //alert(id_livello);
	  $('#'+id_livello).html(myReq.responseText);
    }else{
      alert("Problemi nella richiesta http");
    }
  }
}

/**
 * prepara ed invia
 **/
function invia_dati(id_form, id_liv)
{
  // senza var resta globale
  // id del liv result
  id_livello = id_liv;
  
  /* if(multi==true)
    tipo_req = 'multipart/form-data;';
  else */
  tipo_req = 'application/x-www-form-urlencoded;';
  
  prepara_dati(id_form);
  //alert(azione);
  
  AJAXReq();
  handleResponse();
  
  return false;
}

/**
 * prende l'html di una chiamata POST e lo spara in un div
 * necessita di jquery-1.3.2.js
 * @param String uri
 * @param String livello
 **/
function write_post(uri, id_livello)
{
  //alert(id_livello);
  var allInputs = $("form"); 
  alert(allInputs[4].length);
  

  return false;
  /*$.post(uri, { F: "JQ" },  function(data)
  {
     alert("Data Loaded: " + data); 
     $('#'+id_livello).html(data);
  }); */
}

/* function valida()
{
  //alert( $("")
  arr = $(":input").text();
  alert( arr[1] );
  
  len = $(":input").length;
  for(var i=1; i<=len; i++)
  {
    //if((document.f.elements[i].name.substring(0,2)=='o-') & (document.f.elements[i].value==""))
  }
  
  
  return false;
} */
