var Wick = Wick || {};
Wick.Zoom = (function () {
  function elevateZoomInit (arg) {

      var getDimentions = function (img) {
          var availableWidth = $('.pdp').innerWidth() - $('.pdp__gallery').innerWidth();
          var availableHeight = 545;
          return {
              width: $(img)[0].naturalWidth < availableWidth ? $(img)[0].naturalWidth : availableWidth,
              height: $(img)[0].naturalHeight < availableHeight ? $(img)[0].naturalHeight : availableHeight
          }
      }

      var dimentions = getDimentions(arg);
    $(arg).elevateZoom(
      {
        zoomWindowFadeIn: 300,
        zoomWindowFadeOut: 300,
        borderSize: 0,
        responsive: true,
        lensSize: 200,
        zoomLens: true,
        zoomWindowOffetx: 30,
        zoomWindowOffety: -62,
          zoomWindowWidth: dimentions.width,
          zoomWindowHeight: dimentions.height
      }
    );
  }

  function elevateZoomDestroy () {
    $('.zoomContainer').remove();
    $('.zoomWindowContainer').remove();
  }

  function bindDesktopEvents () {
    $(document).off('mouseleave.zoom').on('mouseleave.zoom', '.zoomContainer', function () {
      elevateZoomDestroy();
      $(document).trigger('_galleryMouseLeave');
    });

    $(document).off('_slideChange.zoom').on('_slideChange.zoom', function () {
      elevateZoomDestroy();
    });

    $(".gallery-zoom").off('mouseenter.zoom').on('mouseenter.zoom', function () {
      elevateZoomInit(this);
      $(document).trigger('_galleryMouseEnter');
    });
  }

  function unbindDesktopEvents () {
    $(".gallery-zoom").off('.zoom');
    $(document).off('.zoom');
    elevateZoomDestroy();
  }

  function init () {
    if (Wick.Responsive.getCurrentBreakpoint() !== 'down-sm') {
      bindDesktopEvents();
    }
    Wick.Responsive.onBreakpoint(unbindDesktopEvents, 'down-sm');
    Wick.Responsive.onBreakpoint(bindDesktopEvents, 'only-md');
    Wick.Responsive.onBreakpoint(bindDesktopEvents, 'up-lg');
  }

  if ($('.gallery-zoom').length){
    init();
  }
})();

