(function(n,t,i){"use strict";function s(n,t,i,r){return n/=r,n--,-i*(n*n*n*n-1)+t}function v(n){return n.complete&&(typeof n.naturalWidth=="undefined"||n.naturalWidth!==0)}function f(n,t){this.container=n;this.onStart=t.onStart||e;this.onMove=t.onMove||e;this.onEnd=t.onEnd||e;this.sliderId=t.sliderId||"slider"+Math.ceil(Math.random()*1e6)}function u(t,i){var r=this;t.is("#iv-container")&&(r._fullPage=!0);r.container=t;i=r.options=n.extend({},u.defaults,i);r.zoomValue=100;t.find(".snap-view").length||t.prepend(h);t.addClass("iv-container");t.css("position")=="static"&&t.css("position","relative");r.snapView=t.find(".iv-snap-view");r.snapImageWrap=t.find(".iv-snap-image-wrap");r.imageWrap=t.find(".iv-image-wrap");r.snapHandle=t.find(".iv-snap-handle");r.zoomHandle=t.find(".iv-zoom-handle");r._viewerId="iv"+Math.floor(Math.random()*1e6)}var e=function(){},o=n("body"),c=n(t),r=n(i),l=15,a=5,h;(function(){for(var r=0,i=["ms","moz","webkit","o"],n=0;n<i.length&&!t.requestAnimationFrame;++n)t.requestAnimationFrame=t[i[n]+"RequestAnimationFrame"],t.cancelAnimationFrame=t[i[n]+"CancelAnimationFrame"]||t[i[n]+"CancelRequestAnimationFrame"];t.requestAnimationFrame||(t.requestAnimationFrame=function(n){var i=(new Date).getTime(),u=Math.max(0,16-(i-r)),f=t.setTimeout(function(){n(i+u)},u);return r=i+u,f});t.cancelAnimationFrame||(t.cancelAnimationFrame=function(n){clearTimeout(n)})})();h='<div class="iv-loader"><\/div> <div class="iv-snap-view"><div class="iv-snap-image-wrap"><div class="iv-snap-handle"><\/div><\/div><div class="iv-zoom-slider"><div class="iv-zoom-handle"><\/div><\/div><\/div><div class="iv-image-view" ><div class="iv-image-wrap" ><\/div><\/div>';n(function(){o.length||(o=n("body"));o.append('<div id="iv-container">'+h+'<div class="iv-close"><\/div><div>')});f.prototype.init=function(){var t=this,i=this.container,n="."+this.sliderId;this.container.on("touchstart"+n+" mousedown"+n,function(i){var f,e;i.preventDefault();var o=(i.type=="touchstart"?"touchmove":"mousemove")+n,s=(i.type=="touchstart"?"touchend":"mouseup")+n,u=i.originalEvent,h=u.clientX||u.touches[0].clientX,c=u.clientY||u.touches[0].clientY,l=t.onStart(i,{x:h,y:c});if(l!==!1){f=function(n){n.preventDefault();u=n.originalEvent;var i=u.clientX||u.touches[0].clientX,r=u.clientY||u.touches[0].clientY;t.onMove(n,{dx:i-h,dy:r-c,mx:i,my:r})};e=function(){r.off(o,f);r.off(s,e);t.onEnd()};r.on(o,f);r.on(s,e)}});return this};u.prototype={constructor:u,_init:function(){function p(t){u.snapView&&(v||n.zoomValue<=100||!n.loaded||(clearTimeout(it),v=!0,n.snapView.css("opacity",1),t||(it=setTimeout(function(){n.snapView.css("opacity",0);v=!1},4e3))))}var n=this,u=n.options,o=!1,y=this.container,t="."+n._viewerId,nt=this.snapHandle,b=this.snapImageWrap,h=this.imageWrap,i=new f(b,{sliderId:n._viewerId,onStart:function(){if(!n.loaded)return!1;var t=nt[0].style;this.curHandleTop=parseFloat(t.top);this.curHandleLeft=parseFloat(t.left);this.handleWidth=parseFloat(t.width);this.handleHeight=parseFloat(t.height);this.width=b.width();this.height=b.height();clearInterval(tt.slideMomentumCheck);cancelAnimationFrame(tt.sliderMomentumFrame)},onMove:function(t,i){var r=this.curHandleLeft+i.dx*100/this.width,u=this.curHandleTop+i.dy*100/this.height;r=Math.max(0,r);r=Math.min(100-this.handleWidth,r);u=Math.max(0,u);u=Math.min(100-this.handleHeight,u);var f=n.containerDim,e=n.imageDim.w*(n.zoomValue/100),o=n.imageDim.h*(n.zoomValue/100),s=e<f.w?(f.w-e)/2:-e*r/100,h=o<f.h?(f.h-o)/2:-o*u/100;nt.css({top:u+"%",left:r+"%"});n.currentImg.css({left:s,top:h})}}).init(),tt=n._imageSlider=new f(h,{sliderId:n._viewerId,onStart:function(t,r){if(!n.loaded)return!1;if(!o){var u=this;i.onStart();u.imgWidth=n.imageDim.w*n.zoomValue/100;u.imgHeight=n.imageDim.h*n.zoomValue/100;u.positions=[r,r];u.startPosition=r;n._clearFrames();u.slideMomentumCheck=setInterval(function(){u.currentPos&&(u.positions.shift(),u.positions.push({x:u.currentPos.mx,y:u.currentPos.my}))},50)}},onMove:function(n,t){if(!o){this.currentPos=t;i.onMove(n,{dx:-t.dx*i.width/this.imgWidth,dy:-t.dy*i.height/this.imgHeight})}},onEnd:function(){function h(){t<=60&&(n.sliderMomentumFrame=requestAnimationFrame(h));f=f+s(t,r/3,-r/3,60);e=e+s(t,u/3,-u/3,60);i.onMove(null,{dx:-(f*i.width/n.imgWidth),dy:-(e*i.height/n.imgHeight)});t++}if(!o){var n=this,r=this.positions[1].x-this.positions[0].x,u=this.positions[1].y-this.positions[0].y;if(Math.abs(r)>30||Math.abs(u)>30){var t=1,f=n.currentPos.dx,e=n.currentPos.dy;h()}}}}).init(),k=0,e,d,g,rt,it,v,w;h.on("mousewheel"+t+" DOMMouseScroll"+t,function(t){var r,i;if(u.zoomOnMouseWheel&&n.loaded&&(n._clearFrames(),r=Math.max(-1,Math.min(1,t.originalEvent.wheelDelta||-t.originalEvent.deltaY)),i=n.zoomValue*(100+r*l)/100,isNaN(i)&&(i=0),i>=100&&i<=u.maxZoom?k=0:k+=Math.abs(r),!(k>a))){t.preventDefault();var f=y.offset(),e=(t.pageX||t.originalEvent.pageX)-f.left,o=(t.pageY||t.originalEvent.pageY)-f.top;n.zoom(i,{x:e,y:o});p()}});h.on("touchstart"+t,function(t){var i,u;if(n.loaded&&(i=t.originalEvent.touches[0],u=t.originalEvent.touches[1],i&&u)){o=!0;var f=y.offset(),h=Math.sqrt(Math.pow(u.pageX-i.pageX,2)+Math.pow(u.pageY-i.pageY,2)),c=n.zoomValue,l={x:(u.pageX+i.pageX)/2-f.left,y:(u.pageY+i.pageY)/2-f.top},e=function(t){t.preventDefault();var i=t.originalEvent.touches[0],r=t.originalEvent.touches[1],u=Math.sqrt(Math.pow(r.pageX-i.pageX,2)+Math.pow(r.pageY-i.pageY,2)),f=c+(u-h)/2;n.zoom(f,l)},s=function(){r.off("touchmove",e);r.off("touchend",s);o=!1};r.on("touchmove",e);r.on("touchend",s)}});e=0;h.on("click"+t,function(t){e==0?(e=Date.now(),d={x:t.pageX,y:t.pageY}):Date.now()-e<500&&Math.abs(t.pageX-d.x)<50&&Math.abs(t.pageY-d.y)<50?(n.zoomValue==u.zoomValue?n.zoom(200):n.resetZoom(),e=0):e=0});g=n.snapView.find(".iv-zoom-slider");rt=new f(g,{sliderId:n._viewerId,onStart:function(t){if(!n.loaded)return!1;this.leftOffset=g.offset().left;this.handleWidth=n.zoomHandle.width();this.onMove(t)},onMove:function(t){var i=(t.pageX||t.originalEvent.touches[0].pageX)-this.leftOffset-this.handleWidth/2,r;i=Math.max(0,i);i=Math.min(n._zoomSliderLength,i);r=100+(u.maxZoom-100)*i/n._zoomSliderLength;n.zoom(r)}}).init();h.on("touchmove"+t+" mousemove"+t,function(){p()});w={};w["mouseenter"+t+" touchstart"+t]=function(){v=!1;p(!0)};w["mouseleave"+t+" touchend"+t]=function(){v=!1;p()};n.snapView.on(w);if(u.refreshOnResize)c.on("resize"+t,function(){n.refresh()});if(n._fullPage){y.on("touchmove"+t+" mousewheel"+t+" DOMMouseScroll"+t,function(n){n.preventDefault()});y.find(".iv-close").on("click"+t,function(){n.hide()})}},zoom:function(n,t){function v(){e++;e<20&&(i._zoomFrame=requestAnimationFrame(v));var b=s(e,u,n-u,20),g=b/u,k=i.imageDim.w*b/100,d=i.imageDim.h*b/100,r=-((t.x-p)*g-t.x),o=-((t.y-w)*g-t.y);r=Math.min(r,h);o=Math.min(o,c);r+k<l&&(r=l-k);o+d<a&&(o=a-d);f.css({height:d+"px",width:k+"px",left:r+"px",top:o+"px"});i.zoomValue=b;i._resizeHandle(k,d,r,o);i.zoomHandle.css("left",(b-100)*i._zoomSliderLength/(y-100)+"px")}n=Math.round(Math.max(100,n));n=Math.min(this.options.maxZoom,n);t=t||{x:this.containerDim.w/2,y:this.containerDim.h/2};var i=this,y=this.options.maxZoom,u=this.zoomValue,f=this.currentImg,r=this.containerDim,p=parseFloat(f.css("left")),w=parseFloat(f.css("top"));i._clearFrames();var e=0,r=i.containerDim,o=i.imageDim,h=(r.w-o.w)/2,c=(r.h-o.h)/2,l=r.w-h,a=r.h-c;v()},_clearFrames:function(){clearInterval(this._imageSlider.slideMomentumCheck);cancelAnimationFrame(this._imageSlider.sliderMomentumFrame);cancelAnimationFrame(this._zoomFrame)},resetZoom:function(){this.zoom(this.options.zoomValue)},_calculateDimensions:function(){var n=this,e=n.currentImg,c=n.container,l=n.snapView,a=e.width(),v=e.height(),r=c.width(),u=c.height(),o=l.innerWidth(),y=l.innerHeight(),t,i,f,s,h;n.containerDim={w:r,h:u};f=a/v;t=a>v&&u>=r||f*u>r?r:f*u;i=t/f;n.imageDim={w:t,h:i};e.css({width:t+"px",height:i+"px",left:(r-t)/2+"px",top:(u-i)/2+"px","max-width":"none","max-height":"none"});s=t>i?o:t*y/i;h=i>t?y:i*o/t;n.snapImageDim={w:s,h:h};n.snapImg.css({width:s,height:h});n._zoomSliderLength=o-n.zoomHandle.outerWidth()},refresh:function(){this.loaded&&(this._calculateDimensions(),this.resetZoom())},_resizeHandle:function(n,t,i,r){var u=this.currentImg,f=n||this.imageDim.w*this.zoomValue/100,e=t||this.imageDim.h*this.zoomValue/100,o=Math.max(-(i||parseFloat(u.css("left")))*100/f,0),s=Math.max(-(r||parseFloat(u.css("top")))*100/e,0),h=Math.min(this.containerDim.w*100/f,100),c=Math.min(this.containerDim.h*100/e,100);this.snapHandle.css({top:s+"%",left:o+"%",width:h+"%",height:c+"%"})},show:function(n,t){this._fullPage&&(this.container.show(),n&&this.load(n,t))},hide:function(){this._fullPage&&this.container.hide()},options:function(n,t){if(!t)return this.options[n];this.options[n]=t},destroy:function(){var n="."+this._viewerId;return this._fullPage?(container.off(n),container.find('[class^="iv"]').off(n)):this.container.remove('[class^="iv"]'),c.off(n),null},load:function(t,i){function o(){r.loaded=!0;r.zoomValue=100;u.show();r.snapImg.show();r.refresh();r.resetZoom();f.find(".iv-loader").hide()}var r=this,f=this.container,e,u;if(f.find(".iv-snap-image,.iv-large-image").remove(),e=this.container.find(".iv-snap-image-wrap"),e.prepend('<img class="iv-snap-image" src="'+t+'" />'),this.imageWrap.prepend('<img class="iv-large-image" src="'+t+'" />'),i&&this.imageWrap.append('<img class="iv-large-image" src="'+i+'" />'),u=this.currentImg=this.container.find(".iv-large-image"),this.snapImg=this.container.find(".iv-snap-image"),r.loaded=!1,f.find(".iv-loader").show(),u.hide(),r.snapImg.hide(),v(u[0]))o();else n(u[0]).on("load",o)}};u.defaults={zoomValue:100,snapView:!0,maxZoom:500,refreshOnResize:!0,zoomOnMouseWheel:!0};t.ImageViewer=function(t,i){var r,f,o,e;return t&&(typeof t=="string"||t instanceof Element||t[0]instanceof Element)||(i=t,t=n("#iv-container")),t=n(t),t.is("img")?(r=t,f=r[0].src,o=r.attr("high-res-src")||r.attr("data-high-res-src"),t=r.wrap('<div class="iv-container" style="display:inline-block; overflow:hidden"><\/div>').parent(),r.css({opacity:0,position:"relative",zIndex:-1})):(f=t.attr("src")||t.attr("data-src"),o=t.attr("high-res-src")||t.attr("data-high-res-src")),e=new u(t,i),e._init(),f&&e.load(f,o),e};n.fn.ImageViewer=function(i){return this.each(function(){var r=n(this),u=t.ImageViewer(r,i);r.data("ImageViewer",u)})}})(window.jQuery,window,document)