jQuery(document).ready(function(){
  //Liste des locations : eviter les doublons de liens vers une location
  jQuery('.location-liste-image img, .button-descriptif').click(function(){
    var idrecup = jQuery(this).attr('id').substr(4);
    location = jQuery('#link-location-' + idrecup ).attr('href');
  });

  //Formulaire de recherche : sélection de la date de début => datepicker
  var currentTime = new Date();
  var dateDebut = new Date(currentTime.getFullYear(), currentTime.getMonth(), (currentTime.getDate() + (6 - currentTime.getDay())));
  var dateFin = new Date(currentTime.getFullYear() + 1, currentTime.getMonth() + 2, currentTime.getDate());

  jQuery( "#dd" ).datepicker({
    changeMonth: true,
    changeYear: true,
    minDate: dateDebut,
    maxDate: dateFin,
    buttonImageOnly: true,
    showOn: 'both',
    dateFormat: 'dd-mm-yy',
    buttonImage: '/images/icons/icon-calendar.png'
  }, jQuery.datepicker.regional["fr"]);

  //Bouton toutes les régions
  jQuery('#rall').click(function() {
    if(jQuery(this).attr('checked'))
      jQuery('.checkbox_list input').attr('checked', true);
    else
      jQuery('.checkbox_list input').attr('checked', false);
  });

  //Formulaire de recherche : modification critère et fermeture du formulaire
  jQuery('.button-modifier-critere').click(function(){
    if(jQuery('.search-wrapper2').css('display') == 'none') {
      var options = {};
      jQuery('.search-wrapper2').show('blind', options, 500);
      if(jQuery(window).scrollTop() > 220)
        jQuery(window).scrollTop(217);
    }
    return false;
  });
  jQuery('.search-close').click(function(){
    var options = {};
    jQuery('.search-wrapper2').hide('blind', options, 500);
    return false;
  });

  //Navigation Suivant / Précédent Location
  jQuery('.page-next').click(function() {
    if(jQuery('.pagination-num .current').index() < (jQuery('.pagination-num').children().length-2))
      location = jQuery('.pagination-num .current').next().attr('href');
  });
  jQuery('.page-back').click(function() {
    if(jQuery('.pagination-num .current').index() > 1)
      location = jQuery('.pagination-num .current').prev().attr('href');
  });

  //Bouton Aide sur la page Sélection
  jQuery("#dialog-modal").dialog({
    autoOpen: false,
    width: 600,
    draggable: false,
    modal: true
  });
  jQuery('#aide').click(function() {
    jQuery("#dialog-modal").dialog('open');
  });

  ///Bouton contact
  jQuery('.contactez-nous').click(function() {
    location = '/contact';
  });

  ///Bouton contact
  jQuery('#client_newsletter2').click(function() {
    if(jQuery(this).attr('checked'))
      jQuery('.field-mail2').show();
    else
      jQuery('.field-mail2').hide();
  });

  //Affichage des description des modes de paiement
  jQuery('.mp-input').click(function() {
    jQuery('.mode-paiement .mp-description').hide();
    jQuery('.mode-paiement #mp-description-' + jQuery(this).attr('id')).show();
  });

  //Bouton voir la suite
	jQuery('#voir-la-suite').click(function() {
    if(!jQuery('.text-region').hasClass('text-region-total')) {
      jQuery('.text-region').addClass('text-region-total', 1000);
      jQuery('#voir-la-suite').html('Cacher le texte');
    } else {
      jQuery('.text-region').removeClass('text-region-total');
      jQuery('#voir-la-suite').html('Voir la suite...');
    }
		return false;
	});
});

//Validation du formulaire de recherche
function validSearchForm() {
  var valid = true;

  if(jQuery('#dd').val() == '') {
    jQuery('#dd').css({'background-color': '#ff0000', 'color': '#fff'});
    valid = false;
  }

  if(jQuery('#nbs').val() == '0') {
    jQuery('#nbs').css({'background-color': '#ff0000', 'color': '#fff'});
    valid = false;
  }
  
  return valid;
}

//Validation du formulaire de sélection
function validSelectionForm() {
  var valid = true;
  
  if(jQuery('input[name="type_demande"]:checked').length == 0) {
    alert('Veuillez sélectionner un type de demande.');
    valid = false;
  }

  return valid;
}

//Validation du formulaire envoyer à un ami
function validEnvoyerAmiForm() {
  var valid = true;

  if(jQuery('#envoyerami_email').val() == '') {
    jQuery('#envoyerami_email').css({'border-color': '#ff0000'});
    valid = false;
  }

  if(jQuery('#envoyerami_email1').val() == '') {
    jQuery('#envoyerami_email1').css({'border-color': '#ff0000'});
    valid = false;
  }

  if(jQuery('#envoyerami_message').val() == '') {
    jQuery('#envoyerami_message').css({'border-color': '#ff0000'});
    valid = false;
  }
  
  return valid;
}

//Validation du formulaire de paiement
function validPaiementForm() {
  var valid = true;
  var text = '';

  if(jQuery('.mode-paiement input[name="paiement"]:checked').length == 0)
  {
    text += '- Vous devez sélectionner un moyen de paiement';
    valid = false;
  }

  if(!jQuery('#conditions-location').attr('checked'))
  {
    text += (text != '' ? '\n' : '') + '- Vous devez accepter les conditions de location pour continuer';
    valid = false;
  }

  if(!valid)
    alert(text);

  return valid;
}

//Ouverture des photos des locations en popup
function photoNewWindow(url, ref, w, h) {
  var width = (parseInt(w) + 20);
  var height = (parseInt(h) + 20);
  var top = (screen.height - height) / 2;
  var left = (screen.width - width) / 2;
  window.open(url, 'Réf. : ' + ref, config='height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no, top=' + top + ', left=' + left);
}

//Ouverture des photos des locations en popup
function popup(url, title, w, h) {
  var width = (parseInt(w) + 20);
  var height = (parseInt(h) + 20);
  var top = (screen.height - height) / 2;
  var left = (screen.width - width) / 2;
  window.open(url, title, config='height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no, top=' + top + ', left=' + left);
  return false;
}
