!function(i){"function"==typeof define&&define.amd?define(["jquery"],i):i("object"==typeof exports?require("jquery"):window.jQuery||window.Zepto)}(function(i){var t=function(t){var e=i.Deferred(),n=i("
");return n.attr("src",t).load(function(){e.resolve({width:n.get(0).width,height:n.get(0).height})}).each(function(){this.complete&&i(this).trigger("load")}),e},e={width:null,height:null,originalWidth:null,originalHeight:null,$element:null},n=function(i,t){return this.eventsInitialized=!1,this.options=i,this.element=Object.create(e),this.element.$element=t,this.imageMain=Object.create(e),this.imageMask=Object.create(e),this.container=Object.create(e),this.cropZone=Object.create(e),this.isRunning=!1,this._init(),this};n.prototype={_init:function(){var i=this;if(this.isRunning===!0)throw new Error("Plugin is still running. Can't initialize.");this.isRunning=!0,this._isContainerExistingInDom()&&this.container.$element.addClass(this.options.cropperContainerClassLoading),t(this.options.imageMain).then(function(e){return i.imageMain.originalHeight=e.height,i.imageMain.originalWidth=e.width,i._isMaskDefinedInOptions()?t(i.options.imageMask).then(function(t){i.imageMask.originalHeight=t.height,i.imageMask.originalWidth=t.width,i._refreshContainer()}):void i._refreshContainer()})},changeImageMain:function(i){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't change imageMain.");return"string"!=typeof i||null!==this.imageMain.$element&&this.imageMain.$element.attr("src")===i?!1:(this.options.imageMain=i,void this._init())},changeImageMaskAndItsOptions:function(t,e){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't change imageMask and its options.");return"string"!=typeof t||null!==this.imageMask.$element&&this.imageMask.$element.attr("src")===t||"object"!=typeof e?!1:(this.options.imageMask=t,this.options.maskOptions=i["extends"](!0,this.options.maskOptions,e),void this._init())},changeImageMask:function(i){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't change imageMask.");return"string"!=typeof i||null!==this.imageMask.$element&&this.imageMask.$element.attr("src")===i?!1:(this.options.imageMask=i,void this._init())},changeMaskOptions:function(t){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't change maskOptions.");return"object"!=typeof t?!1:(this.options.maskOptions=i["extends"](!0,this.options.maskOptions,t),void this._init())},getCropPosition:function(){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't get crop position.");var i=this,t=0,e=0,n=0,s=0;return this._isMaskDefinedInOptions()?(n=(i.options.maskOptions.left-i.imageMain.$element.position().left/i.container.width*100)/100*i.container.width/i.imageMain.width*100,s=(i.options.maskOptions.top-i.imageMain.$element.position().top/i.container.height*100)/100*i.container.height/i.imageMain.height*100,t=i.options.maskOptions.height*i.container.height/100/(i.container.height-(i.container.height-i.imageMain.height))*100,e=i.options.maskOptions.width*i.container.width/100/(i.container.width-(i.container.width-i.imageMain.width))*100):(n=i.cropZone.$element.find('[class*="'+i.options.cropZoneClass+'-selection"]').position().left/i.container.width*100,s=i.cropZone.$element.find('[class*="'+i.options.cropZoneClass+'-selection"]').position().top/i.container.height*100,t=i.cropZone.$element.find('[class*="'+i.options.cropZoneClass+'-selection"]').height()/i.container.height*100,e=i.cropZone.$element.find('[class*="'+i.options.cropZoneClass+'-selection"]').width()/i.container.width*100),{left:Math.min(n,100),top:Math.min(s,100),height:Math.min(t,100),width:Math.min(e,100)}},setCurrentCropPosition:function(t){var e=null,n=null;"width"===this.imageMain.adjustedSize&&i.isNumeric(t.y)?this._isMaskDefinedInOptions&&(e=t.y*this.imageMain.height/100,n=this.options.maskOptions.top*this.container.height/100,this._setPositionTopOfImageMain(-((e-n)/this.container.height*100))):"height"===this.imageMain.adjustedSize&&i.isNumeric(t.x)&&this._isMaskDefinedInOptions&&(e=t.x*this.imageMain.width/100,n=this.options.maskOptions.left*this.container.width/100,this._setPositionLeftOfImageMain(-((e-n)/this.container.width*100)))},enableCrop:function(){return this.options.enableCrop===!0?!1:(this.options.enableCrop=!0,void this._updateContainerClass())},disableCrop:function(){return this.options.enableCrop===!1?!1:(this.options.enableCrop=!1,void this._updateContainerClass())},_refreshContainer:function(){var i=this,t=function(){i.container.$element.removeClass(i.options.cropperContainerClassLoading),"function"==typeof i.options.onInitialized&&i.options.onInitialized.call(i,{coords:i.getCropPosition(),imageMain:i.options.imageMain})};this._isContainerExistingInDom()===!1&&this._createContainerDOMElement(),this._isImageMaskExistingInDom()===!0&&this._isMaskDefinedInOptions()===!1&&(this._removeDomImageMask(),this._removeImageMainStyles()),this._updateContainerClass(),this._hideInitializingElement(),this._isImageMainExistingInDom()===!1?this._createDomImageMain():this._updateDomImageMain(),this._isMaskDefinedInOptions()===!0?(1==this._isCropZoneExistingInDom()&&this._removeDomCropZone(),this._isImageMaskExistingInDom()===!1?this._createDomImageMask():this._updateDomImageMask()):0==this._isCropZoneExistingInDom()&&this._createDomCropZone(),setTimeout(function(){i.imageMain.$element.removeAttr("style"),i._isMaskDefinedInOptions()===!0?i._setPositionOfImageMain():i._setPositionOfCropZone(),i._setContainerCssHeight("auto"),i.eventsInitialized===!1&&i._initEvents(),i._udpateElementsSizes(),i.isRunning=!1,t()},0)},_initEvents:function(){this._onPluginMoveCurrentCrop(),this._onWindowsResize(),this._onMoveImageOrCropZone()},_onPluginMoveCurrentCrop:function(){var i=this;this.element.$element.on("imageMaskCropper:moveCurrentCrop",function(t,e){i.setCurrentCropPosition(e),"function"==typeof i.options.onMoveCrop&&i.options.onMoveCrop.call(i,{coords:i.getCropPosition()})})},_udpateElementsSizes:function(){this._setMaxWidthOfContainer(),this._updateCacheSizeOfContainer(),this._updateCacheSizeOfElement(),this._updateCacheSizeOfImageMain(),1==this._isMaskDefinedInOptions()&&this._updateCacheSizeOfImageMask()},_onWindowsResize:function(){var t=this;i(window).resize(function(){t._udpateElementsSizes()})},_onMoveImageOrCropZone:function(){var t=this,e=null,n=null,s=function(){return t._isMaskDefinedInOptions()?t.imageMain.$element:t.cropZone.$element.find('[class*="'+t.options.cropZoneClass+'-selection"]')}(),o={down:function(i){t.options.enableCrop===!0&&(i.preventDefault(),e={x:i.pageX||function(){return"undefined"!=typeof i.originalEvent?i.originalEvent.touches[0].pageX:null}(),y:i.pageY||function(){return"undefined"!=typeof i.originalEvent?i.originalEvent.touches[0].pageY:null}()},n={top:s.position().top,left:s.position().left})},move:function(i){var s={x:null,y:null};null!==e&&null!==n&&(s.x=e.x-(i.pageX||i.originalEvent.touches[0].pageX),s.y=e.y-(i.pageY||i.originalEvent.touches[0].pageY),t._moveCropZoneOrImage(n,s))},end:function(i){e=null,n=null}};this.options.isMobile?window.PointerEvent?(t.container.$element.on("pointerdown",o.down),t.container.$element.on("pointermove",o.move),t.container.$element.on("pointerup",o.end)):(t.container.$element.on("touchstart",o.down),t.container.$element.on("touchmove",o.move),t.container.$element.on("touchend",o.end)):(t.container.$element.on("mousedown",o.down),i(document).on("mousemove",o.move),i(document).on("mouseup mouseleave",o.end))},_moveCropZoneOrImage:function(i,t){if(this.isRunning===!0)throw new Error("Plugin is still running. Can't move.");return this._isMaskDefinedInOptions()?this._moveImageInMask(i,t):this._moveCropZone(i,t)},_moveImageInMask:function(i,t){var e=this,n=null;"width"===this.imageMain.adjustedSize?(n=this._getTopPercentOfContainer(i.top-t.y),this._setPositionTopOfImageMain(n)):(n=this._getLeftPercentOfContainer(i.left-t.x),this._setPositionLeftOfImageMain(n)),"function"==typeof e.options.onMoveCrop&&e.options.onMoveCrop.call(e,{coords:e.getCropPosition()})},_moveCropZone:function(t,e){var n=this,s=null,o={};"width"===this.imageMain.adjustedSize?(s=Math.max(function(i){return Math.min(i,100-n.options.maskOptions.width)}(this._getLeftPercentOfContainer(t.left-e.x)),0),o.left=s):(s=Math.max(function(i){return Math.min(i,100-n.options.maskOptions.height)}(this._getTopPercentOfContainer(t.top-e.y)),0),o.top=s),this.options.maskOptions=i.extend(!0,this.options.maskOptions,o),this._setPositionOfCropZone()},_getTopPercentOfContainer:function(i){return i/this.container.height*100},_getLeftPercentOfContainer:function(i){return i/this.container.width*100},_hideInitializingElement:function(){this.element.$element.hide()},_createContainerDOMElement:function(){this.container.$element=i("
").addClass(this.options.cropperContainerClass+" "+this.options.cropperContainerClassLoading).insertAfter(this.element.$element)},_updateContainerClass:function(){this._isMaskDefinedInOptions()?this.container.$element.addClass(this.options.cropperContainerClassHasMask):this.container.$element.removeClass(this.options.cropperContainerClassHasMask),this.options.enableCrop===!0?this.container.$element.addClass(this.options.cropperContainerClassEnabledCrop):this.container.$element.removeClass(this.options.cropperContainerClassEnabledCrop)},_updateDomImageMain:function(){this.options.imageMain!==this.imageMain.$element.attr("src")&&(this.imageMain.$element.attr("src",this.options.imageMain),this._updateCacheSizeOfImageMain())},_updateDomImageMask:function(){this.options.imageMask!==this.imageMask.$element.attr("src")&&(this.imageMask.$element.attr("src",this.options.imageMask),this._updateCacheSizeOfImageMask())},_setPositionOfCropZone:function(){this._adjustImageMainToMaskOrCropZone(),this._updateCSSForCropZoneSelection(),this._updateCSSForCropZoneBefore(),this._updateCSSForCropZoneAfter()},_updateCSSForCropZoneSelection:function(){var i=this.options,t={};t="height"===this.imageMain.adjustedSize?{top:i.maskOptions.top+"%",left:"0",width:"100%",height:i.maskOptions.height+"%"}:{top:"0",left:i.maskOptions.left+"%",width:i.maskOptions.width+"%",height:"100%"},this.cropZone.$element.find('[class*="'+i.cropZoneClass+'-selection"]').css(t)},_updateCSSForCropZoneBefore:function(){var i=this.options,t={left:"0",top:"0"};"height"===this.imageMain.adjustedSize?(t.height=i.maskOptions.top+"%",t.width="100%"):(t.height="100%",t.width=i.maskOptions.left+"%"),this.cropZone.$element.find('[class*="'+i.cropZoneClass+'-before"]').css(t)},_updateCSSForCropZoneAfter:function(){var i=this.options,t={};t="height"===this.imageMain.adjustedSize?{top:i.maskOptions.top+i.maskOptions.height+"%",left:"0",height:100-(i.maskOptions.top+i.maskOptions.height)+"%",width:"100%"}:{top:"0",left:i.maskOptions.left+i.maskOptions.width+"%",width:100-(i.maskOptions.left+i.maskOptions.width)+"%",height:"100%"},this.cropZone.$element.find('[class*="'+i.cropZoneClass+'-after"]').css(t)},_setPositionOfImageMain:function(){this._adjustImageMainToMaskOrCropZone(),("width"===this.imageMain.adjustedSize&&"undefined"==typeof topPercent||"height"===this.imageMain.adjustedSize&&"undefined"==typeof leftPercent)&&this._centerImageMainInMask()},_setPositionTopOfImageMain:function(i,t){var e=0;return this.imageMain.$element.css("top",i+"%"),t===!0?!1:(this._getPercentTopOfImageMain()>this.options.maskOptions.top&&this._setPositionTopOfImageMain(this.options.maskOptions.top,!0),e=100-(this.options.maskOptions.top+this.options.maskOptions.height),void(this._getPercentBottomOfImageMain()>e&&this._setPositionBottomOfImageMain(e,!0)))},_setPositionBottomOfImageMain:function(i,t){var e=(this.options.maskOptions.top+this.options.maskOptions.height)*this.container.height/100;this._setPositionTopOfImageMain(-((this.imageMain.height-e)/this.container.height*100),t)},_setPositionLeftOfImageMain:function(i,t){var e=0;return this.imageMain.$element.css("left",i+"%"),t===!0?!1:(this._getPercentLeftOfImageMain()>this.options.maskOptions.left&&this._setPositionLeftOfImageMain(this.options.maskOptions.left,!0),e=100-(this.options.maskOptions.left+this.options.maskOptions.width),void(this._getPercentRightOfImageMain()>e&&this._setPositionRightOfImageMain(e,!0)))},_setPositionRightOfImageMain:function(i,t){var e=(this.options.maskOptions.left+this.options.maskOptions.width)*this.container.width/100;this._setPositionLeftOfImageMain(-((this.imageMain.width-e)/this.container.width*100),t)},_getPercentLeftOfImageMain:function(){return this.imageMain.$element.position().left/this.container.width*100},_getPercentRightOfImageMain:function(){return 100-(this.imageMain.$element.position().left+this.imageMain.width)/this.container.width*100},_getPercentTopOfImageMain:function(){return this.imageMain.$element.position().top/this.container.height*100},_getPercentBottomOfImageMain:function(){return 100-(this.imageMain.$element.position().top+this.imageMain.height)/this.container.height*100},_centerImageMainInMask:function(){var i=this,t=this.options.maskOptions,e=0,n=this.imageMain.originalHeight/this.imageMain.originalWidth,s=this.imageMask[this.imageMain.adjustedSize]*t[this.imageMain.adjustedSize]/100,o="top";return e=(function(){return"height"===i.imageMain.adjustedSize?(o="left",s/n):s*n}()-this.imageMask[this.imageMain.adjustedSize])/2/this.imageMask[this.imageMain.adjustedSize]*100,"top"===o?this._setPositionTopOfImageMain(-e,!0):this._setPositionLeftOfImageMain(-e,!0)},_adjustImageMainToMaskOrCropZone:function(){var i=this.options.maskOptions,t=this.imageMain.width/this.imageMain.height,e=i.width/i.height;return t-e>0?(this._isMaskDefinedInOptions()===!0&&this.imageMain.$element.css({height:i.height+"%",top:i.top+"%",width:"auto",left:"auto"}),this._updateCacheSizeOfImageMain(),this.imageMain.adjustedSize="height"):(this._isMaskDefinedInOptions()===!0&&this.imageMain.$element.css({width:i.width+"%",left:i.left+"%",height:"auto",top:"auto"}),this._updateCacheSizeOfImageMain(),this.imageMain.adjustedSize="width")},_removeImageMainStyles:function(){this.imageMain.$element.removeAttr("style")},_removeDomImageMask:function(){this.imageMask.$element.remove(),this.imageMask=Object.create(e)},_removeDomCropZone:function(){this.cropZone.$element.remove(),this.cropZone=Object.create(e)},_createDomImageMain:function(){var t=this.options.imageMainClass;this.imageMain.$element=i("
").attr("src",this.options.imageMain||this.element.$element.attr("src")).addClass(t).appendTo(this.container.$element),this._updateCacheSizeOfImageMain()},_createDomCropZone:function(){var t=this.options.cropZoneClass;this.cropZone.$element=i('').appendTo(this.container.$element)},_createDomImageMask:function(){var t=this.options.imageMaskClass;this.imageMask.$element=i("
").attr("src",this.options.imageMask).addClass(t).appendTo(this.container.$element),this._updateCacheSizeOfImageMask()},_setContainerCssHeight:function(i){this.container.$element.height(i),this._updateCacheSizeOfContainer()},_setMaxWidthOfContainer:function(){var t=1,e=i(window).height(),n=0,s=0;i.isNumeric(this.options.maxContainerMargin)&&(t=this._isMaskDefinedInOptions()?this.imageMask.originalWidth/this.imageMask.originalHeight:this.imageMain.originalWidth/this.imageMain.originalHeight,n=e-this.container.$element.height(),s=this.container.$element.width()+n*t,this.container.$element.css("max-width",Math.min(this.container.$element.parent().width(),s-this.options.maxContainerMargin)+"px"))},_getSizeOfGivenObject:function(i){return function(i){return{width:i.width(),height:i.height()}}(i)},_updateCacheSizeOfImageMain:function(){var i=this._getSizeOfGivenObject(this.imageMain.$element);this.imageMain.width=i.width,this.imageMain.height=i.height},_updateCacheSizeOfContainer:function(){var i=this._getSizeOfGivenObject(this.container.$element);this.container.width=i.width,this.container.height=i.height},_updateCacheSizeOfImageMask:function(){var i=this._getSizeOfGivenObject(this.imageMask.$element);this.imageMask.width=i.width,this.imageMask.height=i.height},_updateCacheSizeOfElement:function(){var i=this._getSizeOfGivenObject(this.element.$element);this.element.width=i.width,this.element.height=i.height},_isInitializingElementVisible:function(){return this.element.$element.is(":visible")},_isCropZoneExistingInDom:function(){var t=this.options.cropZoneClass,e=this.container.$element.find(i("."+t)).length>0;return e&&null===this.imageMain.$element&&(this.cropZone.$element=this.container.$element.find(i("."+t))),e},_isImageMaskExistingInDom:function(){var t=this.options.imageMaskClass,e=this.container.$element.find(i("."+t)).length>0;return e&&null===this.imageMain.$element&&(this.imageMask.$element=this.container.$element.find(i("."+t)),this._updateCacheSizeOfImageMask()),e},_isImageMainExistingInDom:function(){var t=this.options.imageMainClass,e=this.container.$element.find(i("."+t)).length>0;return e&&null===this.imageMain.$element&&(this.imageMain.$element=this.container.$element.find(i("."+t)),this._updateCacheSizeOfImageMain()),e},_isContainerExistingInDom:function(){var t=this.options.cropperContainerClass,e=this.element.$element.parent().find(i("."+t)).length>0;return e&&null===this.container.$element&&(this.container.$element=this.element.$element.parent().find(i("."+t)),this._updateCacheSizeOfContainer()),e},_isMaskDefinedInOptions:function(){return"string"==typeof this.options.imageMask&&this.options.imageMask.length>0}},i.fn.imageMaskCropper=function(t){this.maskCropperInstance=null;var e=this,s=i.extend(!0,i.fn.imageMaskCropper.defaults,function(){try{var t=i.parseJSON(e.attr("data-mask-options")),n=e.attr("data-mask-image"),s=e.attr("src"),o={maskOptions:t,imageMask:n,imageMain:s}}catch(a){return{}}return o}(),t);return window.maskCropperInstance=e.maskCropperInstance=new n(s,e),this.maskCropperInstance},i.fn.imageMaskCropper.defaults={imageMask:"",imageMain:"",maskOptions:{left:0,top:0,width:100,height:100},enableCrop:!0,maxContainerMargin:!1,cropperContainerClass:"image-mask-cropper",cropperContainerClassHasMask:"has-mask",cropperContainerClassLoading:"images-loading",cropperContainerClassEnabledCrop:"enabled-crop",cropZoneClass:"cropper-crop-zone",imageMainClass:"cropper-image-main",imageMaskClass:"cropper-image-mask",isMobile:!1,onInitialized:function(){}||!1,onMoveCrop:function(){}||!1}});