var d = 0, kodikSliderSeason = 0, g = 70, kodikSliderOffset = 0;

!function(a, b) {
  h = {
    player: function(a, d) {
      b.getElementById("ks-video").innerHTML = "";
      var e = b.createElement("iframe");
      if (e.src = a,
        e.width = "100%",
        e.height = "100%",
        e.setAttribute("border", "0"),
        e.setAttribute("frameborder", "0"),
        e.setAttribute("scrolling", "no"),
        e.setAttribute("allowfullscreen", "true"),
        e.setAttribute("allow", "autoplay *; fullscreen *"),
        b.getElementById("ks-video").appendChild(e),
        b.getElementById("ks-episodes")) {
        c = b.getElementById("ks-episodes").getElementsByTagName("span");
        for (var f = 0; f < c.length; f++)
          c[f].removeAttribute("class")
      }
      d.setAttribute("class", "active")
    }, // work fine
    setseason: function(a, d) {

      $("#ks-seasons span").removeClass( "active" );
      $(d).addClass( "active" );

      $('#ks-translations span').hide();
      $('.translatedseason_' + a).show();

      $('.translatedseason_' + a).eq(0).click();
    },
    season: function(a, d) {

      $("#ks-translations span").removeClass( "active" );
      $(d).addClass( "active" );

      $('#ks-episodes li').hide();

      $('#season' + a).css( "left", "0" ).show();
      $('#season' + a + ' span').eq(0).click();

      kodikSliderSeason = a;

      this.moveEpisode(1);
    },
    show: function(a, c, z) {

      if(!a) return false;
      if(z) $("#ks-episodes, .ks-prev-button, .ks-next-button").hide(), $('#ks-player-container').css("height", "460px"), $('#ks-video').css("bottom", "0px");
      if(c) $("#ks-seasons").hide();

      $('#ks-seasons span').eq(0).click();

    },
    moveEpisode: function(a) {
      var episodes = document.getElementById("season" + kodikSliderSeason);
      var episodesElements = episodes.querySelectorAll("span");
      var episodesCount = episodesElements.length;

      var nextButton = document.querySelector(".ks-next-button");
      var prevButton = document.querySelector(".ks-prev-button");
      var visibleEpisodes = countVisibleEpisodes(episodesElements[kodikSliderOffset]);
      var maxSlide = visibleEpisodes - 1;

      // Right
      if (a === 0) {
        var hiddenEpisodes = episodesCount - visibleEpisodes - kodikSliderOffset;

        if (hiddenEpisodes > 0) {
          var slideEpisodes = hiddenEpisodes > maxSlide ? maxSlide : hiddenEpisodes;
          kodikSliderOffset += slideEpisodes;

          episodes.style.left = -(episodesElements[kodikSliderOffset].offsetLeft - 1) + "px";
        }

        updateButtonsState();
      }

      // Left
      if (a === 1) {
        if (kodikSliderOffset > 0) {
          var slideEpisodes = kodikSliderOffset > maxSlide ? maxSlide : kodikSliderOffset;
          kodikSliderOffset -= slideEpisodes;

          episodes.style.left = -(episodesElements[kodikSliderOffset].offsetLeft - 1) + "px";
        }

        updateButtonsState();
      }

      function countVisibleEpisodes(button) {
        var buttonRect = button.getBoundingClientRect();
        var buttonStyle = button.currentStyle || window.getComputedStyle(button);
        var buttonWidth = (buttonRect.right - buttonRect.left) + parseFloat(buttonStyle.marginLeft) + parseFloat(buttonStyle.marginRight);

        var visibleWidth = document.getElementById("ks-episodes").offsetWidth;
        return Math.floor(visibleWidth / buttonWidth);
      }

      function updateButtonsState() {
        if ((kodikSliderOffset + visibleEpisodes) >= episodesCount) $(nextButton).addClass("disabled");
        if ((kodikSliderOffset + visibleEpisodes) < episodesCount) $(nextButton).removeClass("disabled");
        if (kodikSliderOffset === 0) $(prevButton).addClass("disabled");
        if (kodikSliderOffset !== 0) $(prevButton).removeClass("disabled");
      }
    }
  },
  "undefined" != a.kodikSlider && (a.kodikSlider = h)
}(window, document);

!function(t) {
  t.fn.touchwipe = function(e) {
    var s = {
      min_move_x: 20,
      min_move_y: 20,
      wipeLeft: function() {},
      wipeRight: function() {},
      wipeUp: function() {},
      wipeDown: function() {},
      preventDefaultEvents: !0
    };
    return e && t.extend(s, e),
      this.each(function() {
        var u, c, h = !1;
        function a() {
          this.removeEventListener("touchmove", t),
            u = null,
            h = !1
        }
        function t(e) {
          if (s.preventDefaultEvents && e.preventDefault(),
            h) {
            var t = e.touches[0].pageX
              , n = e.touches[0].pageY
              , i = u - t
              , o = c - n;
            Math.abs(i) >= s.min_move_x ? (a(),
              0 < i ? s.wipeLeft() : s.wipeRight()) : Math.abs(o) >= s.min_move_y && (a(),
              0 < o ? s.wipeDown() : s.wipeUp())
          }
        }
        "ontouchstart"in document.documentElement && this.addEventListener("touchstart", function(e) {
          1 == e.touches.length && (u = e.touches[0].pageX,
            c = e.touches[0].pageY,
            h = !0,
            this.addEventListener("touchmove", t, !1))
        }, !1)
      }),
      this
  }
}(jQuery);
jQuery(document).ready(function($) {
  $("#ks-episodes").touchwipe({
    wipeLeft: function() {
      kodikSlider.moveEpisode(0);
    },
    wipeRight: function() {
      kodikSlider.moveEpisode(1);
    },
    min_move_x: 20,
    min_move_y: 20,
    preventDefaultEvents: true
  });

  if (window.kodikserials_ready !== undefined && window.kodikserials_noseasons !== undefined && window.kodikserials_noepisodes !== undefined) {
    kodikSlider.show(kodikserials_ready, kodikserials_noseasons, kodikserials_noepisodes);
  }
});