var Slider = {

  settings: {
    adaptiveHeight: true,
    dots: true,
    arrows: true,
  },

  mobileSlider: function(wrap) {
    var self = this;
    if ($(window).width() <= 990) {
      if (!$(wrap).hasClass('slick-initialized')) {
        $(wrap).slick(this.settings);
      }
    }

    $(window).on('resize', function () {
      if ($(window).width() <= 990) {
        if (!$(wrap).hasClass('slick-initialized')) {
          $(wrap).slick(self.settings)
        }
      } else {
        if ($(wrap).hasClass('slick-initialized')) {
          $(wrap).slick('unslick')
        }
      }
    })
  },

  init: function(wrap, mobile) {
    mobile ? this.mobileSlider(wrap) : $(wrap).slick(this.settings)
  }

};

function picturesMove(list, on) {

  for (var p = 0; p < list.length; p++) {
    on ?
      list[p].setAttribute('data-show', '')
      : list[p].removeAttribute('data-show');
  }

}

$(document).ready(function () {

  $('.scrollToform').on('click', function (e) {
    e.preventDefault();

    var selfPos = $(this).offset().top;
    var target = $('form').filter(function () {
      return $(this).offset().top > selfPos ? $(this) : false
    });

    $('html, body').animate({scrollTop: target.offset().top}, 700)
  });

  Slider.init('.reviews__slider');

  (function timer() {

    var timerCases = document.querySelectorAll('.timer__case');

    var futureTime = new Date(Date.now() + 1e6).getTime();

    var tick = function () {
      var currentTime = new Date().getTime();

      var time = futureTime - currentTime;

      var h = Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var m = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
      var s = Math.floor((time % (1000 * 60)) / 1000);

      for (var i = 0; i < timerCases.length; i++) {
        timerCases[i].innerHTML = "<span>" + (h < 10 ? '0' + h : h) +
          "</span>:<span>" + (m < 10 ? '0' + m : m) +
          "</span>:<span>" + (s < 10 ? '0' + s : s === 60 ? '00' : s) + "</span>"
      }

      setTimeout(function () {
        time > 1000 ? tick() : false;
      }, 1000)

    };

    tick()

  })();

  (function scrollEffects() {

    var scrollCase = document.querySelector('.common-bg');
    var scrollBG = document.querySelector('.common-bg__photo');
    var picturesCase = document.querySelector('.symptoms__photo');
    var pictures = document.querySelectorAll('[data-pict]');


    document.addEventListener('scroll', function () {

      var scrollValue = (scrollCase.getBoundingClientRect().top * (-1)) / 2;
      var scrollOff = scrollCase.getBoundingClientRect().top * (-1);
      var scrollCaseHeight = scrollCase.clientHeight;

      if (scrollOff < scrollCaseHeight) {
        scrollCase.getBoundingClientRect().top <= 0
          ? scrollBG.style.transform = "translate(-50%," + scrollValue + "px)"
          : false;
      }

      picturesCase.getBoundingClientRect().top <= (window.innerHeight / 1.8) ?
        picturesMove(pictures, true)
        : picturesMove(pictures);

    });

  })();

});