var Wick = Wick || {};
Wick.GalleryRange = (function(){
  var $galleries = $('.gallery.gallery_range'),
    mainContainer = '.gallery__main',
    thumbContainer = '.gallery__thumbs',
    thumbActive = 'gallery__thumb_active';

  function getMainParams(el, loopedSlides){
    return {
      roundLengths: true,
      watchOverflow: true,
      navigation: {
        nextEl: $(el).find('.gallery__btn-next')[0],
        prevEl: $(el).find('.gallery__btn-prev')[0]
      },
      pagination: {
        el: '.gallery__pagination',
        type: 'bullets',
        bulletClass: 'gallery__pagination-bullet',
        bulletActiveClass: 'gallery__pagination-bullet_active'
      },
      slidesPerView: 1,
      loop: true,
      loopedSlides: loopedSlides,
      autoplay: {
        delay: 5000 //this option intended for specify slider-delay in milliseconds.
      }
    };
  }

  function getThumbParams(el){
    var cnt = $(el).find(thumbContainer)[0],
      slidesPerView = cnt.dataset.slides,
      isLocked = $(cnt).find('.swiper-wrapper').children().length <= slidesPerView,
      width = cnt.offsetWidth,
      spaceBetween = (width - (width > 600 ? 58 : 52) * slidesPerView) / (slidesPerView - 1);

    return {
      roundLengths: true,
      watchOverflow: true,
      navigation: {
        nextEl: $(el).find('.thumbs__btn-next'),
        prevEl: $(el).find('.thumbs__btn-prev')
      },
      spaceBetween: spaceBetween,
      slidesPerView: slidesPerView,
      loop: !isLocked,
      slideToClickedSlide: true
    };
  }

  function ActiveIndex(slider, thumbnails) {
    var self = this;

    this.setActiveImage = function(_index){
      slider.slideTo(_index);
      $(thumbnails.slides).removeClass(thumbActive).eq(_index).addClass(thumbActive);
    };

    this.setActiveThumbnail = function(_index){
      $(thumbnails.slides).removeClass(thumbActive).eq(_index).addClass(thumbActive);
    };

    slider
    .on('slideChange', function(){
      self.setActiveThumbnail(this.realIndex);
      $(document).trigger('_slideChange');
    });

    thumbnails
    .on('tap', function(){
      self.setActiveImage($(this.clickedSlide).index());
    });

    self.setActiveThumbnail(slider.realIndex);
  }

  var createSlider = function(index, gallery){
    var $gallery = $(gallery),
      main = $gallery.find(mainContainer)[0],
      thumbs = $gallery.find(thumbContainer)[0];

    if (!$gallery.is(':visible')){
      return;
    }

    var slider = main.swiper;
    if (slider){
      slider.animating = false;
      slider.slideNext();
      slider.autoplay.start();
      return;
    }

    var sl = new Swiper(main, getMainParams(gallery, thumbs.dataset.slides)),
      th = new Swiper(thumbs, getThumbParams(gallery));

    if (!th.isLocked) {
      sl.controller.control = th;
      th.controller.control = sl;
    } else {
      new ActiveIndex(sl, th);
      th.$el.addClass('is-locked');
    }
  };

  var init = function (){
    $galleries.each(createSlider);
  };

  $(document).ready(function () {
    if (!$galleries.length){
      return;
    }

    init();
  });

  return {
    refresh: function($galleries){
      $galleries.each(createSlider);
    }
  }
})();
