// Fix IE
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

/**
 *
 *
 */
function cambiaImmagine(imgId, newFileName )
{
  
//  alert(newFileName);
  $("#" + imgId).attr("src", newFileName);

}

/**
 * Gestisce lo scorrimento di gallerie di jpg sulle pagine web.
 *
 * @param string indexID l'elemento dove è memorizzato l'indice dell'immagine attualmente visualizzata
 * @param string totalId l'elemento dove è memorizzato il numero totale di immagini
 * @param string imgId l'id dell'immagine
 * @param string prefix percorso dell'immagine, compreso il nome del file.
 * @param string step il passo con cui far procedere la galleria
 */
function gesticiGalleria(indexId, totalId, imgId, prefix, step )
{
  
    var indexElem =  $("#" + indexId);
    var totalElem =  $("#" + totalId);
    //var imgId = 'gallery-thumb';
    var total = Number(totalElem.text());
    var newIndex = (Number(indexElem.text())) % total;

    if (newIndex <= 0) {newIndex = total;}

    newIndex = newIndex + Number(step);
    
    if (newIndex <= 0) {newIndex = total;}
    else if (newIndex > total) {newIndex = 1;}
    

    var newImageFile = prefix  + newIndex + ".jpg";

    cambiaImmagine(imgId, newImageFile);
    
    indexElem.text(newIndex);
}






/**
 * Funzione per invocare la submit di un form
 */
function invioForm(e, formId) {
    var form_invio = $("#" + formId);
    form_invio.submit();

     
    e.preventDefault();  
}

var appLingueLink = {
  "it":"<?php echo url_for('it/home/index') ?>"
  ,
  "en":"<?php echo url_for('en/home/index') ?>"
  ,
  "de":"<?php echo url_for('de/home/index')?>"
  ,
  "fr":"<?php echo url_for('fr/home/index') ?>"

};

var appGlobalAction;

$(document).ready(function() {


  // script per mostra/nascondere la lista delle lingue del sito
  $('#lang_nav_arrow').click(function(e) {
    //alert('Handler for .click() called.');
    var target = $("div#lista_lingue");
    //alert(target.tagName);
    if (target.css("display")=="none") {
      target.css("display","block")
    }
    else {
      target.css("display","none")
    }
    e.preventDefault();
  });

  // script per inviare il form dei contatti
  $('#form_contact_submit').click(function(e) {
    invioForm(e, "form_contact_obj");
  });

  // script per inviare il form di prenotazione
  $('#form_avail_submit').click(function(e) {
    invioForm(e, "form_avail_obj");
  });
});
