$(function() {
  var $body = $('body');

  function slider_prev_item($slider) {
    var active = parseInt($slider.attr('data-active'));
    if (active > 0) {
      window.requestAnimationFrame(function() {
        $slider.attr('data-active', active - 1);
      });
    }
  }

  function slider_next_item($slider) {
    var active = parseInt($slider.attr('data-active'));
    var last = parseInt($slider.attr('data-last'));
    if (active < last) {
      window.requestAnimationFrame(function() {
        $slider.attr('data-active', active + 1);
      });
    }
  }

  $body.on('click', '.slider__navigation', function(event) {
    event.preventDefault();
    var $this = $(this);
    var $slider = $this.parents('.slider');
    if ($slider.length === 0) {
      $slider = $this.parents('.text_slider');
    }
    var prev = $this.hasClass('slider__navigation--prev');
    if (prev) {
      slider_prev_item($slider);
    } else {
      slider_next_item($slider);
    }
  });

  $body.on('click', '.slider__next', function(event) {
    event.preventDefault();
    var $this = $(this);
    var $slider = $this.parents('.slider');
    slider_next_item($slider);
  });

  window.enable_touch_swipe('.slider', slider_prev_item, slider_next_item);
  window.enable_touch_swipe('.text_slider', slider_prev_item, slider_next_item);
});
