//variabile a caricare dal cmsdata:
//indice_image: indice dell'immagine che si sta visualizzando in grande nel array
//position: indice dentro il menu dell'immagine scelta
//limit: numero di immagine nel menu sotto l'imagine principale
//campo: campo da prendere con ajax
//id_campo: dove inserire il campo presso con ajax
//image_index: indice nell array dell'immagine a caricare per primo se qualcuno fa avanti
//src_array: array con la src di tutte l'immagine della gallery
//cod_array: codice della immagine nel cms (cms_data)
var quante=0;
var ajax = new sack();

//Funzioni di AJAX
function getText(code)
/*******************************************************************************
  `prende il valore della lista selezionato e chiede con ajax la sua descrizione
                              04/03/2008 11.26
*******************************************************************************/
{
  var div = document.getElementById(id_campi_array[0]);
  ajax.setVar("code", code); // recomended method of setting data to be parsed.
  ajax.setVar("numcampi", campi_array.length);
  for ( l=0; l<campi_array.length; l++ ) {
    ajax.setVar(("campo"+l), campi_array[l]); // recomended
  }
  ajax.requestFile = "PhpLib/s_text.php";
  ajax.method = "post";
  ajax.element = div;
  //ajax.onLoading = whenLoading;
  //ajax.onLoaded = whenLoading;
  //ajax.onInteractive = whenLoading;
  ajax.onCompletion = whenCompleted;
  ajax.runAJAX();
}  // getDescription

function whenLoading( )
/*****************************************************************************
  Inserisce testo mentra ci colleghiamo al server per prendere la descrizione
                              04/03/2008 11.46
*****************************************************************************/
{
  for ( m=0; m<id_campi_array.length; m++ ) {
    var div = document.getElementById(id_campi_array[m]);
    div.innerHTML = "getting data...";
  }
}  // whenLoading

function whenCompleted()
/*****************************************************************
  Quando ci torna i dati il server, gli stampiamo nel texarea
                                04/03/2008 11.49
*****************************************************************/
{
  //var div = document.getElementById(id_campo);

  if (ajax.responseStatus){
    var string = ajax.response;
    //var string = "Status Code: " + ajax.responseStatus[0] + "</p><p>Status Message: " + ajax.responseStatus[1] + "</p><p>URLString Sent: " + ajax.URLString + "</p>";
    var value_campi = string.split("##");
    for ( m=0; m<id_campi_array.length; m++ ) {
      var pos = value_campi[m].indexOf(":_:"); //cerco inizio valore del campo
      var res = value_campi[m].slice(pos+3);
      var div = document.getElementById(id_campi_array[m]);
      //'&nbsp;' +
      div.innerHTML ="<span=\"testata\">"+ testata_campi_array[m] +"</span>"+ res;
    }
    //div.innerHTML = string;
  }
}  // whenCompleted

//////////
 function changeImage( indice, pos )
 /*****************************************************************
  soprescrive il src della imagine in grande!
                              12/05/2008 12.54
 *****************************************************************/
 {
  var imgg = document.getElementById("big_image");
  /*
   * 11/09/2008: inserisco fadeIn and FadeOut cortesia di jquery
   * Todo: pullire il uso del vecchio ajax, per usare jquery
   */
  $('#big_image').fadeOut("slow", function(){
     this.src = document.MM_p[indice].src;
     this.height = height_array[indice];
     $('#big_image').fadeIn("slow");
    });
  //imgg.src=src_array[indice];
  /* imgg.height = height_array[indice]; */
  getText(code_array[indice]);
  indice_image = parseInt(indice);
  //controllare controlli immagine
  position = parseInt(pos);
//  setSelected();
  setControlsGalleria();
  setControlsImage();
  //title.innerHTML = desc_array[indice];
 }  // changeImage

 function more_galleria()
 /*****************************************************************
  `description`
                              22/05/2008 11.58
 *****************************************************************/
 {
  var moved = more_ima();
  if ( moved ) {
    changeImage(image_index-quante,(limit-1));
  }
 }  // more_galleria

 function less_galleria( )
 /*****************************************************************
  `description`
                              22/05/2008 11.57
 *****************************************************************/
 {
  var moved = less_ima();
  if ( moved ) {
    if ( quante == dim_salto  ) {
      changeImage(image_index-1,(immagine_in_menu-1));
    } else
      changeImage(image_index-limit,0);
  }
 }  // less_galleria


 function more_ima( )
 /*****************************************************************
  carica una foto in più
                              12/05/2008 15.02
 *****************************************************************/
 {
  quante=0;
  var moved = false;
  for ( j=0; j<dim_salto; j++ ) {
    if ( image_index<src_array.length ) {//ultima immagine nel menu
      moveLeft();
      //caricamo l'ultima
      document.images[last_image].src = src_array[image_index];
      document.images[last_image].height = icon_height_array[image_index];
      menu_op = document.getElementById('menu'+(limit-1)+'');
      menu_op.href="javascript:changeImage('"+image_index+"','"+(limit-1)+"')";
      image_index++;
      /*first_image_loaded++;
      last_image_loaded++;*/
      position--;
      moved = true;
      quante++;
    }
  }
  setControlsGalleria();
  //setSelected();
  return moved;
 }  // more


 function less_ima()
 /*****************************************************************

                                14/05/2008 15.32
 *****************************************************************/
 {
  quante = 0;
  var moved = false;
   for ( j=0; j<dim_salto ;j++ ) {
     if ( image_index > limit) { //far apparire il link indietro
       moveRight();
       //carica la prima immagine
       document.images[first_image].src = src_array[image_index-(limit+1)];
       document.images[first_image].height = icon_height_array[image_index-(limit+1)];
       menu_op = document.getElementById('menu0');
       menu_op.href="javascript:changeImage('"+(image_index-(limit+1))+"','0')";
       image_index--;
       /*first_image_loaded--;
       last_image_loaded--;*/
       position++;
       moved = true;
       quante++;
     }
   }
  setControlsGalleria();
  //setSelected();
  return moved;
 }  // less

 function moveLeft( )
 /*****************************************************************
  sposta a le foto a sinistra per inserire una nuova,
  sarebbe una con un numero in più
  La gallery sono le immagine del documento fra first e last image
  perchè prima c'e la foto del autore e poi c'e la immagine di indietro
                                12/05/2008 15.03
 *****************************************************************/
 {
  for ( i=first_image; i<last_image; i++) {
   document.images[i].src = document.images[i+1].src;
   document.images[i].height = document.images[i+1].height;
   menu_op = document.getElementById('menu'+(i-first_image)+'');
   //href_tmp = "javascript:changeImage('"+(image_index-((limit-1)-(i-first_image)))+"')";
   href_tmp = "javascript:changeImage('"+(image_index-((limit-1)-(i-first_image)))+"','"+(i-first_image)+"')";
   menu_op.href=href_tmp;
  }
 }  // moveLeft

 function moveRight()
 /*****************************************************************
  sposta a destra`
                              14/05/2008 15.31
 *****************************************************************/
 {
  for ( i=last_image; i>first_image; i-- ) {
   document.images[i].src = document.images[i-1].src;
   document.images[i].height = document.images[i-1].height;
   menu_op = document.getElementById('menu'+(i-first_image)+'');
   //href_tmp = "javascript:changeImage('"+(image_index+(i-(limit+first_image+1)))+"')";
   href_tmp = "javascript:changeImage('"+(image_index+(i-(limit+first_image+1)))+"','"+(i-first_image)+"')";
   menu_op.href=href_tmp;
  }
 }  // moveRight

 function more_pic( )
 /*****************************************************************
  `avanti in una foto
                              21/05/2008 12.58
 *****************************************************************/
 {
  if ( indice_image<(src_array.length-1) ) {     //premisa 1
    var imgg = document.getElementById("big_image");
    var l=(indice_image+1);
    //position+1 per cambiare alla 4°
    if ( position == (limit-1) ) {
      al = more_ima(); //per forza c'e perche sinò non si comple: premisa1
      position++;
      if ( position >= limit )
        position=position-quante;
    } else {
      if ( (position+1)==(limit-1) ) {
        if ( more_ima() ) {
          position++;
          if (position>=limit) {
            position = position-quante;
          }
        }
       // if (more_ima() && l!=(src_array.length-1)) position=0;
        else position++;
      } else {
        position++;
      }
    }
    imgg.src=src_array[l];
    imgg.height = height_array[l];
    getText(code_array[l]);
    indice_image++;
    //setSelected()
    setControlsImage();
  }
 }  // more_pic

  function less_pic( )
 /*****************************************************************
  `indietro in una foto
                              21/05/2008 12.58
 *****************************************************************/
 {
  if ( indice_image>0 ) {
    var imgg = document.getElementById("big_image");
    var l=(indice_image-1);
    //position -1  per cambiare alla 6
    if ( position ==0 ) {
      al = less_ima(); //per forza
      position--;
      if ( position<0 ) {
        position = position+quante;
      }
    }
    else {
      if ( (position-1) <= 0 ) {
        if (less_ima() ) {
          position--;
          if ( position<0 ) {
            position = position+quante;
          }
        }
        //if (less_ima() && (l!=0)) position = limit-1;
        else position--;
      } else {
        position--;
      }
    }
    imgg.src=src_array[l];
    imgg.height = height_array[l];
    getText(code_array[l]);
    indice_image--;
    //setSelected()
    setControlsImage();
  }
 }  // more_pic

 function setControlsGalleria()
 /*****************************************************************
  imposta i controlli di avanti e indietro
                                14/05/2008 16.04
 *****************************************************************/
 {

  if (immagine_in_menu > 0){ //questa funzione solo ha senso se ci sono i controlli
    if ( image_index==src_array.length ) {//ultima immagine nel menu
      //more = document.getElementById('more_gal');
      var more_img = document.getElementById('more_img');
      more_img.src="layout/img/noavanti_gal.gif";
    } else {
      var more = document.getElementById('more_gal');
      //more.href = "javascript:more_galleria()";
      var more_img = document.getElementById('more_img');
      more_img.src="layout/img/avanti_gal.gif";
    }
    if ( image_index > limit) { //far apparire il link indietro
      var less = document.getElementById('less_gal');
      //less.href="javascript:less_galleria()";
      less_img = document.getElementById('less_img');
      less_img.src="layout/img/indietro_gal.gif";
    } else {
      //less = document.getElementById('less_gal');
      var less_img = document.getElementById('less_img');
      less_img.src="layout/img/noindietro_gal.gif";
    }
    var contatore = document.getElementById("contatore");
    contatore.innerHTML = "immagine "+(indice_image+1)+" di "+src_array.length;
  }
 }  // setControlsGalleria

 function setControlsImage()
 /*****************************************************************
  imposta i controlli di avanti e indietro
                                14/05/2008 16.04
 *****************************************************************/
 {
  if (menu_controls_picture){ //questa funzione solo ha senso se ci sono i controlli
    if ( indice_image==(src_array.length-1) ) {//ultima immagine nel menu
      var more_img = document.getElementById('more_picture_img');
      more_img.src="layout/img/noavanti_gal.gif";
      //document.images[(first_image-1)].src="layout/img/noavanti_gal.gif";
    }
    else {
      var more = document.getElementById('more_picture');
      more.href = "javascript:more_pic()";
      //more.protocol = "";
      var more_img = document.getElementById('more_picture_img');
      more_img.src="layout/img/avanti_gal.gif";
      //document.images[(first_image-1)].src="layout/img/avanti_gal.gif";
    }
    if ( indice_image > 0) { //far apparire il link indietro
      var less = document.getElementById('less_picture');
      less.href="javascript:less_pic()";
      less_img = document.getElementById('less_picture_img');
      less_img.src="layout/img/indietro_gal.gif";
//      document.images[(first_image-2)].src="layout/img/indietro_gal.gif";
    } else {
      less_img = document.getElementById('less_picture_img');
      less_img.src = "layout/img/noindietro_gal.gif";
//      document.images[(first_image-2)].src="layout/img/noindietro_gal.gif";
    }
  }
 }  // setControls

function MM_preloadImages() { //v3.0
  var d=document;
    if(!d.MM_p)
      d.MM_p=new Array();
    var i,j=d.MM_p.length;
    var a = src_array;
    for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){
      d.MM_p[j]=new Image;
      d.MM_p[j++].src=a[i];
    }
}

 /*function setSelected( )*/
 /*****************************************************************
  `marca la immagine come selected
                              21/05/2008 16.36
 *****************************************************************
 {
  for ( k=0; k<limit; k++ ) {
    if ( k==position ) {
    var menu = document.getElementById("menu"+k);
    menu.style.border = "2px solid gray";
    } else {
      var menu = document.getElementById("menu"+k);
      menu.style.border = "";
    }
  }
 }  // setSelected*/

