$(function() {
  initDatepickerRange();
  initAvailabilityCalendar();
  initCookieInfo();
  initCookieAcceptButtonAction();
  initTooltips();
  initRequestForm();
});


function initDatepickerRange() {
  // Doku: https://uxsolutions.github.io/bootstrap-datepicker/
  
  $('input[data-type="date"]').datepicker({
    format: "dd.mm.yyyy",
    language: "de",
    daysOfWeekHighlighted: "0,6",
    todayHighlight: true,
    orientation: "top auto"
  });
}

function initAvailabilityCalendar() {  
  $('.calendar').each(function() {
    var $cal = $(this);
    var unavailableDates = JSON.parse($cal.parent().find('input:first').val());

    $cal.availabilityCalendar(unavailableDates);
  });
}

function initTooltips() {
  $('[data-toggle="tooltip"]').tooltip({
    placement: 'bottom'
  });
}

function initCookieInfo() {
  var $section = $('section.cookie');
  var accepted = localStorage.getItem('Dein.Equipment.Cookie.Accepted');
  if(accepted) {
    $section.remove();
  }
}

function initCookieAcceptButtonAction() {
  var $section = $('section.cookie');
  var $acceptBtn = $section.find('button:first');
  
  $acceptBtn.click(function(e) {
    e.preventDefault();
    localStorage.setItem('Dein.Equipment.Cookie.Accepted', true);
    $section.remove();
  });
}

function initRequestForm() {
  var $form = $('form.request');
  var action = $form.attr('data-action-replacement');
  var $submitBtn = $form.find('button[type="submit"]:first');
  var buttonLabel = $form.attr('data-buttonlabel-replacement');
  
  setTimeout(function() {
    $form.attr('action', action);
    $submitBtn.html(buttonLabel);
  }, 3000);
}