(function() {
  window.Persister = {
    clear: function() {
      localStorage.removeItem("bookingWidgetPersisted");
    }
  };

  document.addEventListener("DOMContentLoaded", function() {
    function persist() {
      var destinationElem = document.getElementById("booking-search-widget-destination");
      var destination = {
        value: destinationElem.value,
        "data-id": destinationElem.getAttribute("data-id"),
        "data-type": destinationElem.getAttribute("data-type"),
        "data-raw": destinationElem.getAttribute("data-raw")
      };
      var travelForWork = null;
      if(document.querySelector("#travel-for-business").checked) {
        travelForWork = true;
      }
      if(document.querySelector("#travel-for-leisure").checked) {
        travelForWork = false;
      }
      var checkin = null;
      var checkout = null;
      if(document.querySelector("#booking-datepicker-in").value && window.pickerCheckIn.getDate()) {
        checkin = window.pickerCheckIn.getDate().getTime();
      }
      if(document.querySelector("#booking-datepicker-out").value && window.pickerCheckOut.getDate()) {
        checkout = window.pickerCheckOut.getDate().getTime();
      }
      var dontHaveDates = document.querySelector("#dont-have-dates").checked;
      var rooms = document.getElementById("rooms").value;
      var adults = document.getElementById("adults").value;
      var children = document.getElementById("children").value;

      var childrenAgesSelects = document.querySelectorAll(".children-ages .selects select");
      var childrenAges = [];
      for(var i = 0; i != childrenAgesSelects.length; i++) {
        childrenAges.push(childrenAgesSelects[i].value);
      }

      var data = {
        destination: destination,
        travelForWork: travelForWork,
        checkin: checkin,
        checkout: checkout,
        dontHaveDates: dontHaveDates,
        rooms: rooms,
        adults: adults,
        children: children,
        childrenAges: childrenAges
      };
      localStorage.bookingWidgetPersisted = JSON.stringify(data);
    }

    function restore() {
      if(localStorage.bookingWidgetPersisted) {
        var data;
        try {
          data = JSON.parse(localStorage.bookingWidgetPersisted);
        }
        catch(ex) {
        }
        if(data) {
          var destinationElem = document.getElementById("booking-search-widget-destination");
          for(var k in data.destination) {
            var v = data.destination[k];
            if(v === null || v === "null") {
              continue;
            }
            if(k === "value") {
              destinationElem.value = v;
            }
            else {
              destinationElem.setAttribute(k, v);
            }
          }
          if(data.travelForWork === true) {
            document.querySelector("#travel-for-business").checked = true;
          }
          else if(data.travelForWork === false) {
            document.querySelector("#travel-for-leisure").checked = true;
          }
          if(data.checkin) {
            window.pickerCheckIn.setDate(new Date(data.checkin));
          }
          if(data.checkout) {
            window.pickerCheckOut.setDate(new Date(data.checkout));
          }
          if(data.dontHaveDates) {
            document.querySelector("#dont-have-dates").checked = true;
          }
          document.getElementById("rooms").value = data.rooms;
          document.getElementById("adults").value = data.adults;
          document.getElementById("children").value = data.children;

          // dispatch change event on children select
          var changeEvent = new Event("change", {});
          document.getElementById("children").dispatchEvent(changeEvent);

          var childrenAgesSelects = document.querySelectorAll(".children-ages .selects select");
          if(data.childrenAges) {
            for(var i = 0; i != childrenAgesSelects.length; i++) {
              if(data.childrenAges[i]) {
                childrenAgesSelects[i].value = data.childrenAges[i];
              }
            }
          }
        }
      }
    }

    // ignore setting that user has touched the form, 'cause restoring form can emit user-like events
    window.ignoreUserTouch = true;
    restore();
    window.ignoreUserTouch = false;

    var persistDebounced = window.debounce(persist, 100);
    var events = ["input", "change", "suggestion-chosen"];
    events.forEach(function(name) {
      document.querySelector(".booking-search-widget").addEventListener(name, persistDebounced, false);
    });
  }, false);
})();