(function($) {
   // Options utilisées pour les effets par défaut
   var options = {
      'delay'    : 8   * 1000,
      'duration' : 1.5 * 1000
   };

   $(document).ready(function() {
      var id      = $('body').attr('id');
      if (!id)
          alert("ATTENTION\n la balise body n'a pas d'id !");

      // Register generics
      $('.fade').delay(options.delay).fadeOut(options.duration);
      $('#jsErrMsg').show();


// Formulaire de réservation ------------------------------------------
      var sync_dates = function() {
         // Setup Arrival Date base on nights
         var dfrom     = $('#date_arrivee').datepicker('getDate'),
             nb_nights = parseInt($('#nb_nuits').val()),
             dto       = new Date(dfrom);

         dto.setDate(dfrom.getDate() + nb_nights);

         $('#date_depart').datepicker('setDate', dto);

         // Sync with hidden fields
         $('#fromday').val(dfrom.getDate());
         $('#frommonth').val(dfrom.getMonth() + 1);
         $('#fromyear').val(dfrom.getFullYear());

         $('#today').val(dto.getDate());
         $('#tomonth').val(dto.getMonth() + 1);
         $('#toyear').val(dto.getFullYear());

      };

      $('#date_arrivee.datepicker').datepicker({minDate: 0, numberOfMonths: 2, onSelect: sync_dates });
      $('#date_depart').datepicker({});
      $('#nb_nuits').change(sync_dates);

      // Filter hotels according to the selected city
      var refs = $('#listeHotelsForm').clone().attr('id','');
      $('#city').change(function() {
         var c       = $(this.options[this.selectedIndex]).attr('class'),
             options = $(refs).find('option').clone();
         $('#listeHotelsForm').empty().append(options);
         $('#listeHotelsForm option').each(function() {
            var opt = $(this);
            if (opt.attr('class') != '' && c != '' && opt.attr('class') != c)
               opt.remove();
         });
      });

// Register specifics ==================================================
      switch (id) {
         case 'pageAccueil' : {
             $('#slider1').codaSlider({
               autoSlide: true,
               slideEaseDuration: 3000,
               autoSlideInterval: 5000,
               dynamicTabs: false,
               autoHeight: false
            });
            break;
         }

         case 'pageFicheHotel' : {
// Hotel galleries -----------------------------------------------------
            $('#vignettes a').click(function() {
               var obj  = $(this),
                   zoom = $('#zoom');
                zoom.attr('src',   obj.attr('href'));
                zoom.attr('title', obj.attr('title'));
                $('#legende').html(obj.attr('title'));
               return false;
            });

// Hotel tooltips ------------------------------------------------------
            $('#pageFicheHotel .tooltip').qtip({
               style:{
                  tip:     'leftMiddle',
                  border: { radius : 3 }
               },
               position: {
                  corner: { target: 'rightMiddle', tooltip: 'leftMiddle' },
                  adjust: { x: 0, y: -16 }
               }
            });

// Promo Inline Calendar -----------------------------------------------
            $('#promoForm').submit(function () {
               var s         = new Date($('#pageFicheHotel #calendar').datepicker('getDate'));
               $('#promo_fromday').val(s.getDate());
               $('#promo_frommonth').val(s.getMonth() + 1);
               $('#promo_fromyear').val(s.getFullYear());

               hhotelDispoprice(this);
               return false;
            });

            $('#pageFicheHotel #calendar').datepicker({
               minDate: 0,
               showAnim: '',
               beforeShowDay: function(date) {
                  var b = new Date($('#promo_begin').val()),
                      e = new Date($('#promo_end').val()),
                      s = new Date($('#pageFicheHotel #calendar').datepicker('getDate')),
                      m = new Date(s);
                  m.setDate(s.getDate() + parseInt($('#nights').val()));
                  return [1, (date >= b && date <= e ? 'ui-datepicker-selected' : '') + (date >= s && date < m ? ' ui-state-period' : '')];
               }
            });
            $('#nights').change(function() {
               $('#pageFicheHotel #calendar').datepicker('refresh');
            });
            break;
         }

         case 'pageNewsletter' :
         case 'pageContact' : {

            $('#zeFormValide').validate({
               events : {
                  success : function(msg) {
                     var instance = $.data(this, 'qtip-instance');
                     if (instance)
                        instance.qtip('hide');
                  },
                  failed : function(msg) {
                     var obj = $(this);
                     var instance = $.data(this, 'qtip-instance');
                     if (!instance) {
                        switch (obj.attr('type')) {
                           case 'radio':
                              target = $('input[name="' + obj.attr('name') + '"]:last').parent().find('label'); break;
                           case 'checkbox':
                              target = obj.parent().find('label'); break;
                           default :
                              target = obj;
                        }
                        target.qtip({
                           content: msg,
                           show: false,
                           hide: false,
                           style:{
                              name:    'red',
                              tip:     'leftMiddle',
                              border: { radius : 3 }
                           },
                           position: {
                              corner: { target: 'rightMiddle', tooltip: 'leftMiddle' }
                           }
                        });
                        instance = $.data(this, 'qtip-instance', target);
                     }
                     instance.qtip('show');
                  }
               }
            }).submit(function() {
               $('#todo').val('submit_quest');
            });
            break;
         }

         case 'pageRepondreEmploi' : {
            $('#birthdate.datepicker').datepicker({
               altField  : $('#visitor_birthdate_'),
               altFormat : 'yy-mm-dd',
               changeMonth: true,
               changeYear: true,
               yearRange : '-70:+0',
               maxDate: 0,
               onSelect: function() {
                  $('#birthdate').blur();
               }
            });
            $('#zeFormValide').validate({
               events : {
                  success : function(msg) {
                     var instance = $.data(this, 'qtip-instance');
                     if (instance)
                        instance.qtip('hide');
                  },
                  failed : function(msg) {
                     var obj = $(this);
                     var instance = $.data(this, 'qtip-instance');
                     if (!instance) {
                        switch (obj.attr('type')) {
                           case 'radio':
                              target = $('input[name="' + obj.attr('name') + '"]:last').parent().find('label'); break;
                           case 'checkbox':
                              target = obj.parent().find('label'); break;
                           default :
                              target = obj;
                        }
                        target.qtip({
                           content: msg || 'Ce champ est obligatoire',
                           show: false,
                           hide: false,
                           style:{
                              name:    'red',
                              tip:     'leftMiddle',
                              border: { radius : 3 }
                           },
                           position: {
                              corner: { target: 'rightMiddle', tooltip: 'leftMiddle' }
                           }
                        });
                        instance = $.data(this, 'qtip-instance', target);
                     }
                     instance.qtip('show');
                  }
               }
            }).submit(function() {
               $('#todo').val('submit_quest');
            });
            break;
         }
	
// Page Meilleurs Tarifs ----------------------------------------------
	      case 'pageMeilleurTarif' : {
	    	  $('#btnJeReserveMT').click(function(event) {
	    		  event.preventDefault();
	    		  var l = $('input[name="langue"]').val();
	    		  hhotelSearchMSP('frsidholehotels', l);
	    		  pageTracker._trackPageview('/reservation_meilleur_tarif_' + l + '.php');
	    	  });
	    	  break;
	      }
      }
   });
})(jQuery);

