/*! jquery.mlens.js - magnifying lens jQuery plugin for images by Federica Sibella (@musings.it) - Double licensed MIT and GPLv3 */
!function(e){function r(e){if("string"==typeof e){var r=e.indexOf("_");-1!=r&&(e=e.substr(r+1))}return e}var t=[],i=0,o={init:function(r){var o={lensShape:"square",lensSize:100,borderSize:4,borderColor:"#888",borderRadius:0,imgSrc:"",imgSrc2x:"",lensCss:"",imgOverlay:"",overlayAdapt:!0,zoomLevel:1},n=e.extend({},o,r);this.each(function(){var r=e(this),a=r.data("mlens"),s=e(),d=e(),c=e(),u=e(),g=r.attr("src"),p="auto";("number"!=typeof n.zoomLevel||n.zoomLevel<=0)&&(n.zoomLevel=o.zoomLevel);var l=new Image;""!==n.imgSrc2x&&window.devicePixelRatio>1?(g=n.imgSrc2x,l.onload=function(){p=String(parseInt(this.width/2)*n.zoomLevel)+"px",s.css({backgroundSize:p+" auto"}),u.css({width:p})},l.src=g):(""!==n.imgSrc&&(g=n.imgSrc),l.onload=function(){p=String(parseInt(this.width)*n.zoomLevel)+"px",s.css({backgroundSize:p+" auto"}),u.css({width:p})},l.src=g);var h="background-position: 0px 0px;width: "+String(n.lensSize)+"px;height: "+String(n.lensSize)+"px;float: left;display: none;border: "+String(n.borderSize)+"px solid "+n.borderColor+";background-repeat: no-repeat;position: absolute;",b="position: absolute; width: 100%; height: 100%; left: 0; top: 0; background-position: center center; background-repeat: no-repeat; z-index: 1;";switch(n.overlayAdapt===!0&&(b+="background-position: center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"),n.lensShape){case"square":h=h+"border-radius:"+String(n.borderRadius)+"px;",b=b+"border-radius:"+String(n.borderRadius)+"px;";break;case"":h=h+"border-radius:"+String(n.borderRadius)+"px;",b=b+"border-radius:"+String(n.borderRadius)+"px;";break;case"circle":h=h+"border-radius: "+String(n.lensSize/2+n.borderSize)+"px;",b=b+"border-radius: "+String(n.lensSize/2+n.borderSize)+"px;";break;default:h=h+"border-radius:"+String(n.borderRadius)+"px;",b=b+"border-radius:"+String(n.borderRadius)+"px;"}return r.wrap("<div id='mlens_wrapper_"+i+"' />"),c=r.parent(),c.css({width:r.width()}),s=e("<div id='mlens_target_"+i+"' style='"+h+"' class='"+n.lensCss+"'>&nbsp;</div>").appendTo(c),s.css({backgroundImage:"url('"+g+"')",backgroundSize:p+" auto",cursor:"none"}),u=e("<img style='display:none;width:"+p+";height:auto;max-width:none;max-height;none;' src='"+g+"' />").appendTo(c),""!==n.imgOverlay&&(d=e("<div id='mlens_overlay_"+i+"' style='"+b+"'>&nbsp;</div>"),d.css({backgroundImage:"url('"+n.imgOverlay+"')",cursor:"none"}),d.appendTo(s)),r.attr("data-id","mlens_"+i),s.mousemove(function(t){e.fn.mlens("move",r.attr("data-id"),t)}),r.mousemove(function(t){e.fn.mlens("move",r.attr("data-id"),t)}),s.on("touchmove",function(t){t.preventDefault();var i=t.originalEvent.touches[0]||t.originalEvent.changedTouches[0];e.fn.mlens("move",r.attr("data-id"),i)}),r.on("touchmove",function(t){t.preventDefault();var i=t.originalEvent.touches[0]||t.originalEvent.changedTouches[0];e.fn.mlens("move",r.attr("data-id"),i)}),s.hover(function(){e(this).show()},function(){e(this).hide()}),r.hover(function(){s.show()},function(){s.hide()}),s.on("touchstart",function(r){r.preventDefault(),e(this).show()}),s.on("touchend",function(r){r.preventDefault(),e(this).hide()}),r.on("touchstart",function(e){e.preventDefault(),s.show()}),r.on("touchend",function(e){e.preventDefault(),s.hide()}),r.data("mlens",{image:r,settings:n,target:s,imageTag:u,imgSrc:g,imgWidth:p,imageWrapper:c,overlay:d,instance:i}),a=r.data("mlens"),t[i]=a,i++,t})},move:function(e,i){e=r(e);var o=t[e],n=o.image,a=o.target,s=o.imageTag,d=n.offset(),c=parseInt(i.pageX-d.left),u=parseInt(i.pageY-d.top),g=s.width()/n.width(),p=s.height()/n.height();return c>0&&u>0&&c<n.width()&&u<n.height()&&(c=String(-((i.pageX-d.left)*g-a.width()/2)),u=String(-((i.pageY-d.top)*p-a.height()/2)),a.css({backgroundPosition:c+"px "+u+"px"}),c=String(i.pageX-d.left-a.width()/2),u=String(i.pageY-d.top-a.height()/2),a.css({left:c+"px",top:u+"px"})),o.target=a,t[e]=o,t},update:function(i){var o=r(e(this).attr("data-id")),n=t[o],a=n.image,s=n.target,d=n.overlay,c=n.imageTag,u=n.imgSrc,g=n.settings,p=e.extend({},g,i),l="auto",h=new Image;""!==p.imgSrc2x&&window.devicePixelRatio>1?(u=p.imgSrc2x,h.onload=function(){l=String(parseInt(this.width/2))+"px",s.css({backgroundSize:l+" auto"}),c.css({width:l})},h.src=u):(""!==p.imgSrc&&(u=p.imgSrc),h.onload=function(){l=String(parseInt(this.width)*p.zoomLevel)+"px",s.css({backgroundSize:l+" auto"}),c.css({width:l})},h.src=u);var b="background-position: 0px 0px;width: "+String(p.lensSize)+"px;height: "+String(p.lensSize)+"px;float: left;display: none;border: "+String(p.borderSize)+"px solid "+p.borderColor+";background-repeat: no-repeat;position: absolute;",m="position: absolute; width: 100%; height: 100%; left: 0; top: 0; background-position: center center; background-repeat: no-repeat; z-index: 1;";switch(p.overlayAdapt===!0&&(m+="background-position: center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"),p.lensShape){case"square":b=b+"border-radius:"+String(p.borderRadius)+"px;",m=m+"border-radius:"+String(p.borderRadius)+"px;";break;case"":b=b+"border-radius:"+String(p.borderRadius)+"px;",m=m+"border-radius:"+String(p.borderRadius)+"px;";break;case"circle":b=b+"border-radius: "+String(p.lensSize/2+p.borderSize)+"px;",m=m+"border-radius: "+String(p.lensSize/2+p.borderSize)+"px;";break;default:b=b+"border-radius:"+String(p.borderRadius)+"px;",m=m+"border-radius:"+String(p.borderRadius)+"px;"}return s.attr("style",b),c.attr("src",u),c.css({width:l}),s.css({backgroundImage:"url('"+u+"')",backgroundSize:l+" auto",cursor:"none"}),d.attr("style",m),d.css({backgroundImage:"url('"+p.imgOverlay+"')",cursor:"none"}),n.image=a,n.target=s,n.overlay=d,n.settings=p,n.imgSrc=u,n.imageTag=c,t[o]=n,t},destroy:function(){var i=r(e(this).attr("data-id")),o=t[i];o.target.remove(),o.imageTag.remove(),o.image.unwrap(),o.overlay.remove(),e.removeData(o,"mlens"),this.unbind(),this.element=null}};e.fn.mlens=function(r){return o[r]?o[r].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof r&&r?void e.error("Method "+r+" does not exist on jQuery.mlens"):o.init.apply(this,arguments)}}(jQuery);