!function(i){"function"==typeof define&&define.amd?define(["jquery"],i):i("object"==typeof exports?require("jquery"):window.jQuery||window.Zepto)}(function(i){function t(i){s.innerHTML+=i}var e="imageMaskCropperStyles",n=function(t){var e=i.Deferred();return i("<img>").attr("src",t).load(function(){e.resolve(t)}).each(function(){this.complete&&i(this).trigger("load")}),e},s=function(){var i=document.createElement("style");return i.setAttribute("id",e),i.appendChild(document.createTextNode("")),document.head.appendChild(i),i}(),o=function(e){return"true"===i("html").attr("data-mask-cropper-inserted-css")?!1:(t("@-webkit-keyframes svg-loader{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}@-moz-keyframes svg-loader{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}@-o-keyframes svg-loader{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}@keyframes svg-loader{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}"),t("."+e.cropperContainerClass+" { text-align: center;overflow:hidden;cursor: move; position: relative; }"),t("."+e.cropperContainerClass+':before { z-index: 11;content: "";display: none;width: 32px;height: 32px;position: absolute;top: 50%;left: 50%;bottom: 50%;margin-top: -16px;margin-left: -15px;background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzMiAzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIiBmaWxsPSJibGFjayI+ICA8cGF0aCBvcGFjaXR5PSIuMjUiIGQ9Ik0xNiAwIEExNiAxNiAwIDAgMCAxNiAzMiBBMTYgMTYgMCAwIDAgMTYgMCBNMTYgNCBBMTIgMTIgMCAwIDEgMTYgMjggQTEyIDEyIDAgMCAxIDE2IDQiLz4gIDxwYXRoIGQ9Ik0xNiAwIEExNiAxNiAwIDAgMSAzMiAxNiBMMjggMTYgQTEyIDEyIDAgMCAwIDE2IDR6Ij48L3BhdGg+PC9zdmc+) no-repeat center center;-webkit-animation: svg-loader 800ms infinite;-moz-animation: svg-loader 800ms infinite;-o-animation: svg-loader 800ms infinite;animation: svg-loader 800ms infinite; }'),t("."+e.cropperContainerClass+':after { z-index: 10;content: "";display: none;width: 100%;height: 100%;position: absolute;top: 0;left: 0;background-color: #fff; opacity: 0.8}'),t("."+e.cropperContainerClass+"."+e.cropperContainerClassLoading+":before { display: block; }"),t("."+e.cropperContainerClass+"."+e.cropperContainerClassLoading+":after { display: block; }"),t("."+e.cropperContainerClass+" ."+e.imageMainClass+" { position: relative; max-width: 100%; }"),t("."+e.cropperContainerClass+"."+e.cropperContainerClassHasMask+" ."+e.imageMainClass+" { position: absolute; max-width: none;}"),t("."+e.cropperContainerClass+"."+e.cropperContainerClassHasMask+" ."+e.imageMaskClass+" { position: relative;width: 100%; }"),t('[class*="'+e.cropZoneClass+'-"] { position: absolute; }'),t("."+e.cropZoneClass+"-selection:before,."+e.cropZoneClass+"-selection span,."+e.cropZoneClass+"-selection:after {position: absolute;opacity: 0.8;background: rgba(255,255,255,0.1) url(data:image/gif;base64,R0lGODlhCAAIAJEAAKqqqv///wAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQJCgAAACwAAAAACAAIAAACDZQFCadrzVRMB9FZ5SwAIfkECQoAAAAsAAAAAAgACAAAAg+ELqCYaudeW9ChyOyltQAAIfkECQoAAAAsAAAAAAgACAAAAg8EhGKXm+rQYtC0WGl9oAAAIfkECQoAAAAsAAAAAAgACAAAAg+EhWKQernaYmjCWLF7qAAAIfkECQoAAAAsAAAAAAgACAAAAg2EISmna81UTAfRWeUsACH5BAkKAAAALAAAAAAIAAgAAAIPFA6imGrnXlvQocjspbUAACH5BAkKAAAALAAAAAAIAAgAAAIPlIBgl5vq0GLQtFhpfaIAACH5BAUKAAAALAAAAAAIAAgAAAIPlIFgknq52mJowlixe6gAADs=) top left repeat;}"),t("."+e.cropZoneClass+"-selection:before,."+e.cropZoneClass+'-selection:after { content: "";}'),t("."+e.cropZoneClass+"-selection span:nth-child(1) { height: 100%;width: 1px;left: 0px;top: 0px;}"),t("."+e.cropZoneClass+"-selection span:nth-child(2) { height: 100%;width: 1px;right: 0px;top: 0px;}"),t("."+e.cropZoneClass+"-selection:after { height: 1px;width: 100%;left: 0px;top: 0px;}"),t("."+e.cropZoneClass+"-selection:before { height: 1px;width: 100%;left: 0px;bottom: 0px;}"),t("."+e.cropZoneClass+"-before, ."+e.cropZoneClass+"-after { background-color: rgba(0, 0, 0, 0.4)"),void i("html").attr("data-mask-cropper-inserted-css","true"))},a={width:null,height:null,$element:null},r=function(i,t){return this.eventsInitialized=!1,this.options=i,this.element=Object.create(a),this.element.$element=t,this.imageMain=Object.create(a),this.imageMask=Object.create(a),this.container=Object.create(a),this.cropZone=Object.create(a),this.isRunning=!1,this._init(),this};r.prototype={_init:function(){var i=this,t=function(){i._isContainerExistingInDom()&&i.container.$element.removeClass(i.options.cropperContainerClassLoading),"function"==typeof i.options.onInitialized&&i.options.onInitialized.call(i,{coords:i.getCropPosition(),imageMain:i.options.imageMain})};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),n(this.options.imageMain).then(function(){return i._isMaskDefinedInOptions()?n(i.options.imageMask).then(function(){i._refreshContainer(),t()}):(i._refreshContainer(),void t())})},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)))},_refreshContainer:function(){this._isContainerExistingInDom()===!1&&this._createContainerDOMElement(),this._isImageMaskExistingInDom()===!0&&this._isMaskDefinedInOptions()===!1&&(this._removeDomImageMask(),this._removeImageMainStyles()),this._updateContainerClass(),this._isInitializingElementVisible()===!0&&(this._setContainerCssHeight(this.element.height),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(),this._setContainerCssHeight("auto"),this._isMaskDefinedInOptions()===!0?this._setPositionOfImageMain():this._setPositionOfCropZone(),this.eventsInitialized===!1&&this._initEvents(),this.isRunning=!1},_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()})})},_onWindowsResize:function(){var t=this;i(window).resize(function(){t._updateCacheSizeOfContainer(),t._updateCacheSizeOfElement(),t._updateCacheSizeOfImageMain(),t._updateCacheSizeOfImageMask()})},_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"]')}();t.container.$element.on("mousedown touchstart",function(i){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}}),i(document).on("mousemove touchmove",function(i){var s={};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))}),i(document).on("mouseup touchend mouseleave",function(){null!==e&&(e=null),null!==n&&(n=null)})},_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("<div/>").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)},_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){var t=0;this.imageMain.$element.css("top",i+"%"),this._getPercentTopOfImageMain()>this.options.maskOptions.top&&this._setPositionTopOfImageMain(this.options.maskOptions.top),t=100-(this.options.maskOptions.top+this.options.maskOptions.height),this._getPercentBottomOfImageMain()>t&&this._setPositionBottomOfImageMain(t)},_setPositionBottomOfImageMain:function(i){var t=(this.options.maskOptions.top+this.options.maskOptions.height)*this.container.height/100;this._setPositionTopOfImageMain(-((this.imageMain.height-t)/this.container.height*100))},_setPositionLeftOfImageMain:function(i){var t=0;this.imageMain.$element.css("left",i+"%"),this._getPercentLeftOfImageMain()>this.options.maskOptions.left&&this._setPositionLeftOfImageMain(this.options.maskOptions.left),t=100-(this.options.maskOptions.left+this.options.maskOptions.width),this._getPercentRightOfImageMain()>t&&this._setPositionRightOfImageMain(t)},_setPositionRightOfImageMain:function(i){var t=(this.options.maskOptions.left+this.options.maskOptions.width)*this.container.width/100;this._setPositionLeftOfImageMain(-((this.imageMain.width-t)/this.container.width*100))},_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.height/this.imageMain.width,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):this._setPositionLeftOfImageMain(-e)},_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(a)},_removeDomCropZone:function(){this.cropZone.$element.remove(),this.cropZone=Object.create(a)},_createDomImageMain:function(){var t=this.options.imageMainClass;this.imageMain.$element=i("<img/>").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('<div class="'+t+'"><div class="'+t+'-before"></div><div class="'+t+'-selection"><span></span><span></span></div><div class="'+t+'-after"></div></div>').appendTo(this.container.$element)},_createDomImageMask:function(){var t=this.options.imageMaskClass;this.imageMask.$element=i("<img/>").attr("src",this.options.imageMask).addClass(t).appendTo(this.container.$element),this._updateCacheSizeOfImageMask()},_setContainerCssHeight:function(i){this.container.$element.height(i),this._updateCacheSizeOfContainer()},_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;return this.container.$element.find(i("."+t)).length>0},_isImageMaskExistingInDom:function(){var t=this.options.imageMaskClass;return this.container.$element.find(i("."+t)).length>0},_isImageMainExistingInDom:function(){var t=this.options.imageMainClass;return this.container.$element.find(i("."+t)).length>0},_isContainerExistingInDom:function(){var t=this.options.cropperContainerClass;return this.element.$element.parent().find(i("."+t)).length>0},_isMaskDefinedInOptions:function(){return"string"==typeof this.options.imageMask&&this.options.imageMask.length>0}},i.fn.imageMaskCropper=function(t){this.maskCropperInstance=null;var e=this,n=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 o(n),window.maskCropperInstance=e.maskCropperInstance=new r(n,e),this.maskCropperInstance},i.fn.imageMaskCropper.defaults={imageMask:"",imageMain:"",maskOptions:{left:0,top:0,width:100,height:100},cropperContainerClass:"image-mask-cropper",cropperContainerClassHasMask:"has-mask",cropperContainerClassLoading:"images-loading",cropZoneClass:"cropper-crop-zone",imageMainClass:"cropper-image-main",imageMaskClass:"cropper-image-mask",onInitialized:function(){}||!1,onMoveCrop:function(){}||!1}});