var lock_autocomplete = false;
var timerSlideshow = null;
     
$(document).ready(function(){

  testCookies();


// FASTBOOKING
// > PLACEHOLDER JS for Inputs text
    
$(".help").mouseover(function() {              
  $(".help_content", this).show();
}); 
$(".help").mouseout(function() {              
  $(".help_content", this).hide();
});                            

$("input[type='text']").focus(function() {
  inputFocus($(this));
});                             

$("#newsletter input[type='text']").focus(function() {
  inputFocusNewsletter($(this));
});                     

$("input[type='text']").blur(function() {
  inputBlur($(this));
});                       

$(".error input[type='text']").blur(function() {
  $(this).css("backgroundPosition", "left bottom");
});                

$('input[data-default]').each(function(){
  var defaultValue = $(this).attr('data-default');
  if($(this).val() == '')
    {
      $(this).attr('value',defaultValue);
    }

    $(this).focus(function() {
      if($(this).val() == defaultValue)
      {
      $(this).attr('value','');
      $(this).addClass('active');
      }
    })

  
  $(this).blur(function() {
    if($(this).attr('value') == '')
      { $(this).attr('value',defaultValue);
  $(this).removeClass('active');
      }
  })
});
// > PLACEHOLDER JS for Textarea
$('textarea[data-default]').each(function(){
  var defaultValue = $(this).attr('data-default');
  if($(this).val() == '')
    {
      $(this).attr('value',defaultValue);
    }
  $(this).focus(function() {
      if($(this).val() == defaultValue)
      {
    $(this).attr('value','');
    $(this).addClass('active');  
      }
  })
  $(this).blur(function() {
    if($(this).attr('value') == '')
      { $(this).attr('value',defaultValue);
  $(this).removeClass('active');
      }
  })
});

// FASTBOOKING DATE PICKER
 /*
$('#outbound-date, #return-date').datepicker({
  inline: true,          
});         */
                                   


// CUSTOM SELECT
$('select.custom').each(function(){
   
  var selected_value = $("option:selected", this).val();
  var selected_indice = 0;                             
  selectValue = new Array()
  selectName = new Array() 
  $(this).css('display','none');
  // Replace default select
  var $customSelect = '<div id="customselect-'+$(this).attr("id")+'" class="customselect"><span class="value"></span><span class="up">Up</span><span class="down">Down</span></div>';
  $(this).before($customSelect);
  var select_size = 0;
  $(this).children().each(function(){
      selectValue.push($(this).attr('value'));
      selectName.push($(this).next().text());  
      if(selected_value == $(this).attr('value')) {
        selected_indice = select_size;
      }
      select_size++;
     })
  // array with select value   
  var selectArray = new Array(selectValue,selectName); 
  var unitSelect = $(this).attr('data-unit');
  var newSelect = $(this).prev();
                                 
  //newSelect.children('span.value').text(selectArray[0][0]+ ' '+unitSelect);
  if($("option:selected", this).length > 0) {
   newSelect.children('span.value').text(selected_value+ ' '+unitSelect);
  }
  else {
    newSelect.children('span.value').text(selectArray[0][0]+ ' '+unitSelect);
  }
  //$(this).children('option[value='+selectArray[0][0]+']').attr('selected','selected');
  var currentSelected = selected_indice;                    
  // Up button
  newSelect.children('span.up').click(function(){
    if(currentSelected < select_size) { currentSelected++; }
    if(currentSelected == select_size) { currentSelected = 0; }
    newSelect.children('span.value').text(selectArray[0][currentSelected]+' '+unitSelect);
    $(this).parent().next().children('option').removeAttr('selected');
    $(this).parent().next().children('option[value='+selectArray[0][currentSelected]+']').attr('selected','selected');
  })
  // Down button
  newSelect.children('span.down').click(function(){
    if(currentSelected == 0) { currentSelected = select_size - 1; }
    else { currentSelected-- }
  newSelect.children('span.value').text(selectArray[0][currentSelected]+' '+unitSelect);
    $(this).parent().next().children('option').removeAttr('selected');
    $(this).parent().next().children('option[value='+selectArray[0][currentSelected]+']').attr('selected','selected');
  })       
})
  
  // CHECKBOXES & RADIO CUSTOMISATION REVOLUTION OLE
  $('input[type=checkbox],input[type=radio]').prettyCheckboxes({
    checkboxWidth: 13, // The width of your custom checkbox
    checkboxHeight: 13, // The height of your custom checkbox
    className : 'prettyCheckbox', // The classname of your custom checkbox
    display: 'list' // The style you want it to be display (inline or list)
  });


/**
 * BOOKING
 */

$("#booking_type_one-way").change ( function () {
  $('fieldset.return').addClass('opacity');
  $('fieldset.return input').attr('readonly','readonly');
  
}); 

$("#booking_type_return").change ( function () {
  $('fieldset.return').removeClass('opacity');
  $('fieldset.return input').removeAttr('readonly');
}); 

if ( $("#booking_type_one-way:checked").length) {
  $('fieldset.return').addClass('opacity');
  $('fieldset.return input').attr('readonly','readonly');
}


// Check hour 
$('#booking_pickup_departure_1').each( function () { adjustEndTime(); }); // Init...
$('#booking_arrival_departure_1, #booking_arrival_departure_0, #booking_pickup_departure_1, #booking_pickup_departure_0').change( function () {
  if ( $(this).attr('checked') ) {
    adjustEndTime();
  }
});

// init
$('#from-cover, #to-cover').combobox();

$('#from-cover').next().attr('placeholder', $('#from-cover option:selected').html());
$('#to-cover').next().attr('placeholder', $('#to-cover option:selected').html());

if ( !Modernizr.input.placeholder ) {
  $('#from-cover').next().placeholder();
  $('#to-cover').next().placeholder();
}  


// tooltip
$('#booking input[type="text"], #booking .ui-autocomplete-input, #fastbooking  input[type="text"], #fastbooking .ui-autocomplete-input').focus( function (){
  $(this).parent().find('.tooltip').show();
  inputFocus($(this));
});
$('#booking input, #fastbooking input').blur( function (){
  $(this).parent().find('.tooltip').hide();
  inputBlur($(this));
});


$('#from-city, #to-city').autocomplete({
  source: function (request, response) {
    $.ajax({
        url: "/cover/getPlaceByPart/" + request.term,
        dataType: "json",
        success: function( data ) { 
          response( $.map( data.getPlacesByPartResult, function( item ) {
            return {
              label: item,
              value: item
            }
          }));
        },
        statusCode: { 500: function() { error500();}}        
    })},
    minLength: 1,
    autoSelect: true,
    autoFocus: true,
    select: function( event, ui ) {      
      tmp_zipcode = $( "#" + $(this).attr('id').replace(/city/, 'zipcode')).val();
      tmp_zipcode_default = $( "#" + $(this).attr('id').replace(/city/, 'zipcode')).attr('data-default');
      if ( tmp_zipcode != tmp_zipcode_default) {
        $( "#" + $(this).attr('id').replace(/city/, 'zipcode')).val(tmp_zipcode_default);
      }
      var op = {
          street: ui.item.label,
          //postcode: tmp_zipcode != tmp_zipcode_default ? tmp_zipcode : undefined
          postcode : undefined
      };
      
      if ( "from-city" == $(this).attr('id')) {
        city = $("#from-city");
        cover = $("#from-cover");
      } else {
        city = $("#to-city");
        cover = $("#to-cover");
      }
      city.val(op.street);
     
      if (9 == event.which) {
        event.preventDefault();
      }
      cover.parent().find('input').focus();
      loadCoverStreets(op, cover);
    }
  });

// Check (from|to)-city value on blur : 
// The value must be knowledgeable and well-known
$('#from-city, #to-city').blur( function () {
  checkCityValue($(this));  
});


/**
 * SLIDER 
 */
  
  // If slideshow exist, launch slide
  if($('#slideshow'))
  {
      currentslide = 1;
      slides = $('#slideshow div div').length;
      
      // GENERER PAGGER
      var paggerHTML = '<span class="nav" ><ul><li class="prev"><a>&lt;</a></li>';
      for (i=1; i<slides+1; i++) {
        paggerLI = '<li class='+i+'><a>'+i+'</a></li>';
        paggerHTML = paggerHTML+paggerLI;
       }
       
      paggerHTML = paggerHTML+'<li class="next"><a>&gt;</a></li></ul></span>';
       $('#slideshow .wrap').append(paggerHTML);
       // BOUTONS PAGGER
       $('#slideshow .nav li.next').click(function() { 
         //clearInterval(timerSlideshow);
         nextSlide();
       })
       $('#slideshow .nav li.prev').click(function() { 
         //clearInterval(timerSlideshow);
         prevSlide();
       })
       $('#slideshow .nav li').click(function() { 
           if(($(this).attr('class') == 'prev' ) || ($(this).attr('class') == 'next' ))  {
             
           }
           else if(!$(this).hasClass("active")) {
             slideto = $(this).attr('class');
             gotoSlide(slideto);
           }
        });
     
       // ADD CLASS ACTIVE
       $('#slideshow .nav li.1').addClass('active');
         
       
       // TIMER SLIDESHOW
      launchSliderInterval();
  }
  // Nextslide, default transition and click button  
  function nextSlide() {
    var to_slide = 1;
    if(currentslide != slides) { 
      to_slide = parseInt(currentslide) + 1;
    }
    
    gotoSlide(to_slide);
    
  }            
  // Previous slide, prev button
  function prevSlide() {
     var to_slide = slides;
    if(currentslide != 1) { 
      to_slide = currentslide-1;
    }
    gotoSlide(to_slide);
    
  }
  // Go to slide, on click on pagger button
  function gotoSlide(slideto) {
    clearInterval(timerSlideshow);   
    $('#slideshow .wrap div:nth-child('+currentslide+')').fadeOut('800', function(){
      $('#slideshow nav li.'+currentslide).removeClass('active');
      currentslide = slideto;
      $('#slideshow .wrap div:nth-child('+currentslide+')').fadeIn('800', function() {
        updateSlideshowPager(currentslide);
        launchSliderInterval();
      });
    })
  } 
  
  /**
  * update the pager of the slideshow
  * @param page: the new page to highlight
  */
  function updateSlideshowPager(page){
    $('#slideshow .nav li.active').removeClass('active'); 
    $('#slideshow .nav li.'+page).addClass('active'); 
  } 
  
  function launchSliderInterval(){
    timerSlideshow = setInterval(nextSlide,10000);  
  }
    
     
  // FAQ
  $('#intern.faq li a').click(function() {
    if($(this).parent().attr('class') == 'active')
    {
  $(this).next().slideUp(function(){ $(this).parent().removeClass('active'); });
    }
    else 
    { 
      $('#intern.faq li.active ol').slideUp();
      $(this).next().slideDown('fast',function(){
      $('#intern.faq li.active').removeClass('active');
      $(this).parent().addClass('active');
      });
    }
  })
  $('#intern.faq li li a').unbind('click');
  
  // INTERNE ACCORDEON
//  function scrollToanchor(s, t)
//  {
//    $(document).ready(function(){
//      $('#selector').load(s, function() {
//        window.scrollTo(0, $(t).position().top);}
//      );
//    });
//  }
        
  $('.accordion article h2 a').click(function() {  
    if($(this).parent().parent().attr('class') == 'active')
    {
      $(this).parent().next().slideUp(function() { $(this).parent().removeClass('active');})
    }
    else
    {
      $('#intern.accordion article.active .slide').slideUp();

      $(this).parent().parent().find('div.slide').slideDown("slow", function() {
        poslink = $(this).offset();
        $('#intern.accordion article.active').removeClass('active');
        $(this).parent().addClass('active');
        $('html,body').animate({scrollTop : (poslink.top -80) + 'px'} ,"slow");
        
      });
    }
  })
            

function nightcards_manage_display() {
  var nb_nc = $("#people option:selected").val();
  $(".nightcard").each(function(i) {
    if(i < nb_nc) {            
      $(this).removeClass("hidden").addClass("visible");   
    }
    else {                     
      $(this).removeClass("visible").addClass("hidden");  
      $(this).val("");            
    }
  });
}      

$('#customselect-people span.up').click(function(){
  if($(".nightcard").size()>0) {  
    nightcards_manage_display();
  }                    
  return false;
})

$('#customselect-people span.down').click(function(){ 
  if($(".nightcard").size()>0) {  
    nightcards_manage_display();
  }          
  return false;
})
          
$('.nightcard').each(function(i){
  var people_indice = $("#people option").index($("#people option:selected"));
  if(i > 0 && $(this).val() == "" && i > people_indice ) {    
      $(this).removeClass("visible").addClass("hidden"); 
  }
  else {
      $(this).removeClass("hidden").addClass("visible");
  }
})

});                  
