(function(n,t){"use strict";typeof define=="function"&&define.amd?define(["jquery"],function(n){t(n)}):typeof module=="object"&&module.exports?module.exports=n.EasyZoom=t(require("jquery")):n.EasyZoom=t(n.jQuery)})(this,function(n){"use strict";function t(t,i){this.$target=n(t);this.opts=n.extend({},s,i,this.$target.data());this.isOpen===undefined&&this._init()}var i,r,e,o,u,f,s={loadingNotice:"Loading image",errorNotice:"The image could not be loaded",errorDuration:2500,linkAttribute:"href",preventClicks:!0,beforeShow:n.noop,beforeHide:n.noop,onShow:n.noop,onHide:n.noop,onMove:n.noop};return t.prototype._init=function(){this.$link=this.$target.find("a");this.$image=this.$target.find("img");this.$flyout=n('<div class="easyzoom-flyout" />');this.$notice=n('<div class="easyzoom-notice" />');this.$target.on({"mousemove.easyzoom touchmove.easyzoom":n.proxy(this._onMove,this),"mouseleave.easyzoom touchend.easyzoom":n.proxy(this._onLeave,this),"mouseenter.easyzoom touchstart.easyzoom":n.proxy(this._onEnter,this)});this.opts.preventClicks&&this.$target.on("click.easyzoom",function(n){n.preventDefault()})},t.prototype.show=function(n,t){var u=this;if(this.opts.beforeShow.call(this)!==!1){if(!this.isReady)return this._loadImage(this.$link.attr(this.opts.linkAttribute),function(){(u.isMouseOver||!t)&&u.show(n)});this.$target.append(this.$flyout);var f=this.$target.outerWidth(),s=this.$target.outerHeight(),h=this.$flyout.width(),c=this.$flyout.height(),l=this.$zoom.width(),a=this.$zoom.height();i=l-h;r=a-c;i<0&&(i=0);r<0&&(r=0);e=i/f;o=r/s;this.isOpen=!0;this.opts.onShow.call(this);n&&this._move(n)}},t.prototype._onEnter=function(n){var t=n.originalEvent.touches;this.isMouseOver=!0;t&&t.length!=1||(n.preventDefault(),this.show(n,!0))},t.prototype._onMove=function(n){this.isOpen&&(n.preventDefault(),this._move(n))},t.prototype._onLeave=function(){this.isMouseOver=!1;this.isOpen&&this.hide()},t.prototype._onLoad=function(n){n.currentTarget.width&&(this.isReady=!0,this.$notice.detach(),this.$flyout.html(this.$zoom),this.$target.removeClass("is-loading").addClass("is-ready"),n.data.call&&n.data())},t.prototype._onError=function(){var n=this;this.$notice.text(this.opts.errorNotice);this.$target.removeClass("is-loading").addClass("is-error");this.detachNotice=setTimeout(function(){n.$notice.detach();n.detachNotice=null},this.opts.errorDuration)},t.prototype._loadImage=function(t,i){var r=new Image;this.$target.addClass("is-loading").append(this.$notice.text(this.opts.loadingNotice));this.$zoom=n(r).on("error",n.proxy(this._onError,this)).on("load",i,n.proxy(this._onLoad,this));r.style.position="absolute";r.src=t},t.prototype._move=function(n){var t,c,l;n.type.indexOf("touch")===0?(t=n.touches||n.originalEvent.touches,u=t[0].pageX,f=t[0].pageY):(u=n.pageX||u,f=n.pageY||f);var a=this.$target.offset(),v=f-a.top,y=u-a.left,s=Math.ceil(v*o),h=Math.ceil(y*e);h<0||s<0||h>i||s>r?this.hide():(c=s*-1,l=h*-1,this.$zoom.css({top:c,left:l}),this.opts.onMove.call(this,c,l))},t.prototype.hide=function(){this.isOpen&&this.opts.beforeHide.call(this)!==!1&&(this.$flyout.detach(),this.isOpen=!1,this.opts.onHide.call(this))},t.prototype.swap=function(t,i,r){this.hide();this.isReady=!1;this.detachNotice&&clearTimeout(this.detachNotice);this.$notice.parent().length&&this.$notice.detach();this.$target.removeClass("is-loading is-ready is-error");this.$image.attr({src:t,srcset:n.isArray(r)?r.join():r});this.$link.attr(this.opts.linkAttribute,i)},t.prototype.teardown=function(){this.hide();this.$target.off(".easyzoom").removeClass("is-loading is-ready is-error");this.detachNotice&&clearTimeout(this.detachNotice);delete this.$link;delete this.$zoom;delete this.$image;delete this.$notice;delete this.$flyout;delete this.isOpen;delete this.isReady},n.fn.easyZoom=function(i){return this.each(function(){var r=n.data(this,"easyZoom");r?r.isOpen===undefined&&r._init():n.data(this,"easyZoom",new t(this,i))})},t})