(function(e){"function"===typeof define&&define.amd?define(["jquery"],e):e(jQuery)})(function(e){var h=e(window),f=function(a,b){b=e.isPlainObject(b)?b:{};this.$image=e(a);this.defaults=e.extend({},f.defaults,this.$image.data(),b);this.init()};f.prototype={construstor:f,init:function(){this.setAspectRatio(this.defaults.aspectRatio);this.render()},render:function(a){var b=this,c=this.$image,d,g;if(!this.active){this.$clone&&this.$clone.remove();g=c.attr("src");d=e('<img src="'+g+'">');d.on("load",
function(){var a;d.off("load");l=this.naturalHeight;(m=this.naturalWidth)&&this.naturalHeight?a={naturalHeight:this.naturalHeight,naturalWidth:this.naturalWidth}:(f.fn.size(d,{height:"auto",width:"auto"}),a=f.fn.size(d),a={naturalHeight:a.height,naturalWidth:a.width});if(a.naturalHeight>f.defaults.heightMax){var c=a.naturalHeight,h=a.naturalWidth,k=f.defaults.heightMax/c;a.naturalHeight=f.defaults.heightMax;a.naturalWidth=h*k}a.naturalWidth>f.defaults.widthMax&&(c=a.naturalHeight,h=a.naturalWidth,
k=f.defaults.widthMax/h,a.naturalWidth=f.defaults.widthMax,a.naturalHeight=c*k);a.naturalHeight>=a.naturalWidth?a.naturalHeight<f.defaults.heightMax&&(c=a.naturalHeight,h=a.naturalWidth,k=f.defaults.heightMax/c,a.naturalHeight=f.defaults.heightMax,a.naturalWidth=h*k):a.naturalWidth<f.defaults.widthMax&&(c=a.naturalHeight,h=a.naturalWidth,k=f.defaults.widthMax/h,a.naturalWidth=f.defaults.widthMax,a.naturalHeight=c*k);e(".img-container").width(a.naturalWidth);e(".img-container").height(a.naturalHeight);
e(".img-container").parent()&&(e(".img-container").parent().width(a.naturalWidth),e(".img-container").parent().height(a.naturalHeight));f.fn.size(d,{height:a.naturalHeight,width:a.naturalWidth});a.aspectRatio=a.naturalWidth/a.naturalHeight;b.src=g;b.image=a;b.active=!0;b.createCropper()});if(e.isFunction(a))c.on("ready.cropper",a);this.$clone=d;c.after(d)}},unrender:function(){this.active&&(this.active=!1,this.removeCropper(),this.src="",this.dragger=this.cropper=this.image=null);return this},rerender:function(a){var b=
this;this.unrender().render(function(){a||b.setData(b.data)})},resize:function(){this.data=this.getData();clearTimeout(this.resizing);this.resizing=setTimeout(e.proxy(this.rerender,this),200)},createCropper:function(){this.$cropper=e(f.template);this.$dragger=this.$cropper.find(".cropper-dragger");f.fn.toggle(this.$image);this.$image.after(this.$cropper);this.$cropper.prepend(this.$clone);this.defaults.modal||f.fn.toggle(this.$cropper.find(".cropper-modal"));this.setPreview();this.addListener()},
removeCropper:function(){this.removeListener();this.$preview=null;this.$clone.remove();this.$dragger=this.$clone=null;this.$cropper.remove();this.$cropper=null;f.fn.toggle(this.$image)},addListener:function(){this.$cropper.bind("mousedown touchstart",e.proxy(this.dragstart,this));this.$cropper.bind("mousemove touchmove",e.proxy(this.dragmove,this));this.$cropper.bind("mouseup mouseleave touchend touchleave",e.proxy(this.dragend,this));h.on("resize",e.proxy(this.resize,this))},removeListener:function(){this.$cropper.unbind("mousedown touchstart",
this.dragstart);this.$cropper.unbind("mousemove touchmove",this.dragmove);this.$cropper.unbind("mouseup mouseleave touchend touchleave",this.dragend);h.off("resize",this.resize)},setPreview:function(){var a=this.defaults.preview;this.$preview=this.$cropper.find(".cropper-preview");"string"===typeof a&&0<a.length&&(this.$preview=this.$preview.add(a));this.$preview.html('<img src="'+this.src+'">');this.setCropper()},setCropper:function(){var a=this.$image.parent(),b=f.fn.size(a),c=this.image,d;0<=c.naturalWidth*
b.height/c.naturalHeight-b.width?(d={height:b.width/c.aspectRatio,width:b.width,left:0},d.top=(b.height-d.height)/2):(d={height:b.height,width:b.height*c.aspectRatio,top:0},d.left=(b.width-d.width)/2);e.each(d,function(a,b){d[a]=Math.round(b)});c.height=d.height;c.width=d.width;c.ratio=c.width/c.naturalWidth;f.fn.position(a);this.$cropper.css({height:d.height,left:d.left,top:d.top,width:d.width});this.cropper=d;this.setDragger()},setDragger:function(){var a=this.cropper,b=this.defaults.aspectRatio||
this.image.aspectRatio,b=0<=a.height*b-a.width?{height:a.width/b,width:a.width,left:0,top:(a.height-a.width/b)/2,maxWidth:a.width,maxHeight:a.width/b}:{height:a.height,width:a.height*b,left:(a.width-a.height*b)/2,top:0,maxHeight:a.height,maxWidth:a.height*b},c=e(".img-container img"),c=c&&c[0]&&c[0].attributes&&c[0].attributes.cropperfullarea&&"true"==c[0].attributes.cropperfullarea.value;b.height*=c?1:0.8;b.width*=c?1:0.8;b.left=(a.width-b.width)/2;b.top=(a.height-b.height)/2;this.dragger=f.fn.round(b);
this.setData(this.defaults.data);this.$image.trigger("ready.cropper").off("ready.cropper")},resetDragger:function(){var a=this.dragger,b=this.cropper;a.width=a.width>a.maxWidth?a.maxWidth:Math.abs(a.width);a.height=a.height>a.maxHeight?a.maxHeight:Math.abs(a.height);a.maxLeft=b.width-a.width;a.maxTop=b.height-a.height;a.left=0>a.left?0:a.left>a.maxLeft?a.maxLeft:a.left;a.top=0>a.top?0:a.top>a.maxTop?a.maxTop:a.top;a=f.fn.round(a);this.$dragger.css({height:a.height,left:a.left,top:a.top,width:a.width});
this.dragger=a;this.preview();this.output()},dragging:function(){var a=this.direction,b=this.dragger,c=this.defaults.aspectRatio,d=this.endX-this.startX,e=this.endY-this.startY,f,h;c&&(f=e*c,h=d/c);switch(a){case "e":b.width+=d;c&&(b.height=b.width/c,b.top-=h/2);0>b.width&&(this.direction="w",b.width=0);break;case "n":b.height-=e;b.top+=e;c&&(b.width=b.height*c,b.left+=f/2);0>b.height&&(this.direction="s",b.height=0);break;case "w":b.width-=d;b.left+=d;c&&(b.height=b.width/c,b.top+=h/2);0>b.width&&
(this.direction="e",b.width=0);break;case "s":b.height+=e;c&&(b.width=b.height*c,b.left-=f/2);0>b.height&&(this.direction="n",b.height=0);break;case "ne":b.height-=e;b.top+=e;b.width=c?b.height*c:b.width+d;0>b.height&&(this.direction="sw",b.height=0,b.width=0);break;case "nw":b.height-=e;b.top+=e;c?(b.width=b.height*c,b.left+=f):(b.width-=d,b.left+=d);0>b.height&&(this.direction="se",b.height=0,b.width=0);break;case "sw":b.width-=d;b.left+=d;b.height=c?b.width/c:b.height+e;0>b.width&&(this.direction=
"ne",b.height=0,b.width=0);break;case "se":b.width+=d;b.height=c?b.width/c:b.height+e;0>b.width&&(this.direction="nw",b.height=0,b.width=0);break;default:b.left+=d,b.top+=e}this.resetDragger();this.startX=this.endX;this.startY=this.endY},output:function(){this.defaults.done(this.getData())},preview:function(){var a=this.cropper,b=this.dragger;this.$preview.each(function(){var c=e(this),d=c.width()/b.width,g={height:a.height,marginLeft:-b.left,marginTop:-b.top,width:a.width};c.css({overflow:"hidden"});
c.find("img").css(f.fn.round(g,function(a){return a*d}))})},enable:function(a){this.render(a)},disable:function(){this.unrender()},setAspectRatio:function(a){if("auto"===a||e.isNumeric(a)&&0<a)this.defaults.aspectRatio="auto"===a?NaN:a,this.active&&this.setDragger()},setData:function(a){var b=this.cropper,c=this.dragger,d=this.defaults.aspectRatio,g=function(a){return"number"===typeof a};this.active&&(e.isPlainObject(a)&&!e.isEmptyObject(a)&&(a=f.fn.transformData(a,this.image.ratio),g(a.x1)&&a.x1<=
b.width&&(c.left=a.x1),g(a.y1)&&a.y1<=b.height&&(c.top=a.y1),d?g(a.width)&&a.width<=b.width?(c.width=a.width,c.height=c.width/d):g(a.height)&&a.height<=b.height?(c.height=a.height,c.width=c.height*d):g(a.x2)&&a.x2<=b.width?(c.width=a.x2-c.left,c.height=c.width/d):g(a.y2)&&a.y2<=b.height&&(c.height=a.y2-c.top,c.width=c.height*d):(g(a.width)&&a.width<=b.width?c.width=a.width:g(a.x2)&&a.x2<=b.width&&(c.width=a.x2-c.left),g(a.height)&&a.height<=b.height?c.height=a.height:g(a.y2)&&a.height<=b.height&&
(c.height=a.y2-c.top))),this.dragger=c,this.resetDragger())},getData:function(){var a=this.dragger,b={};this.active&&(b={x1:a.left*m/this.image.naturalWidth,y1:a.top*l/this.image.naturalHeight,width:a.width*m/this.image.naturalWidth,height:a.height*l/this.image.naturalHeight,x2:(a.left+a.width)*m/this.image.naturalWidth,y2:(a.top+a.height)*l/this.image.naturalHeight},b=f.fn.transformData(b,1/this.image.ratio));return b},setImgSrc:function(a){"string"===typeof a&&0<a.length&&a!==this.src&&(this.$image.attr("src",
a),this.rerender(!0))},getImgInfo:function(){return this.image||{}},dragstart:function(a){var b=f.fn.getOriginalEvent(a).touches,c=a,d;b&&1===b.length&&(c=b[0],this.touchId=c.identifier,d=!0);b=e(c.target).data().direction;f.fn.isDirection(b)&&(this.startX=c.pageX,this.startY=c.pageY,this.direction=b,this.$image.trigger("dragstart"),d&&a.preventDefault())},dragmove:function(a){var b=f.fn.getOriginalEvent(a).changedTouches,c=a,d;if(b&&1===b.length&&(c=b[0],d=!0,c.identifier!==this.touchId))return;
this.direction&&(this.$image.trigger("dragmove"),d&&a.preventDefault(),this.endX=c.pageX,this.endY=c.pageY,this.dragging())},dragend:function(a){var b=f.fn.getOriginalEvent(a).changedTouches,c=a,d;if(b&&1===b.length&&(c=b[0],d=!0,c.identifier!==this.touchId))return;this.direction&&(this.direction="",this.$image.trigger("dragend"),d&&a.preventDefault())}};f.fn={toggle:function(a){a.toggleClass("cropper-hidden")},position:function(a,b){"static"===a.css("position")&&a.css("position",b||"relative")},
size:function(a,b){if(e.isPlainObject(b))a.css(b);else return{height:a.height(),width:a.width()}},round:function(a,b){var c,d;for(d in a)c=a[d],a.hasOwnProperty(d)&&"number"===typeof c&&(a[d]=Math.round(e.isFunction(b)?b(c):c));return a},transformData:function(a,b){var c=this,d={};e.each(a,function(a,f){c.isDataOption(a)&&e.isNumeric(f)&&0<=f&&(d[a]=Math.round(f*b))});return d},getOriginalEvent:function(a){a&&"undefined"!==typeof a.originalEvent&&(a=a.originalEvent);return a},isDataOption:function(a){return/^(x1|y1|x2|y2|width|height)$/i.test(a)},
isDirection:function(a){return/^(\*|e|n|w|s|ne|nw|sw|se)$/i.test(a)}};f.template='<div class="cropper-container"><div class="cropper-modal"></div><div class="cropper-dragger"><span class="cropper-preview"></span><span class="cropper-dashed dashed-h"></span><span class="cropper-dashed dashed-v"></span><span class="cropper-face" data-direction="*"></span><span class="cropper-line line-e" data-direction="e"></span><span class="cropper-line line-n" data-direction="n"></span><span class="cropper-line line-w" data-direction="w"></span><span class="cropper-line line-s" data-direction="s"></span><span class="cropper-point point-e" data-direction="e"></span><span class="cropper-point point-n" data-direction="n"></span><span class="cropper-point point-w" data-direction="w"></span><span class="cropper-point point-s" data-direction="s"></span><span class="cropper-point point-ne" data-direction="ne"></span><span class="cropper-point point-nw" data-direction="nw"></span><span class="cropper-point point-sw" data-direction="sw"></span><span class="cropper-point point-se" data-direction="se"></span></div></div>';
f.defaults={aspectRatio:"auto",data:{},done:function(){},modal:!0,preview:"",heightMax:356,widthMax:356};var l,m;f.setDefaults=function(a){e.extend(f.defaults,a)};e.fn.cropper=function(a,b){var c=this;this.each(function(){var d=e(this),g=d.data("cropper");g||(g=new f(this,a),d.data("cropper",g));"string"===typeof a&&e.isFunction(g[a])&&(c=g[a](b))});return"undefined"!==typeof c?c:this};e.fn.cropper.Constructor=f;e.fn.cropper.setDefaults=f.setDefaults;e(function(){e("img[cropper]").cropper()})});
$(document).ajaxSuccess(function(e,h,f){2>$(".img-container img").length&&setCropper()});$(document).ready(function(){setCropper()});function setCropper(){var e=$(".img-container img");e.cropper({aspectRatio:e&&e[0]&&e[0].attributes&&e[0].attributes.cropperfreeaspectratio&&"true"==e[0].attributes.cropperfreeaspectratio.value?null:1,preview:".img-preview",modal:!0,done:function(h){h=e.cropper("getData");var f="";try{f=JSON.stringify(h)}catch(l){console.log(h)}$(".img-container-data").val(f)}})};
