var imageEdit;!function(a){imageEdit={iasapi:{},hold:{},postid:"",intval:function(a){return 0|a},setDisabled:function(b,c){c?(b.removeClass("disabled"),a("input",b).removeAttr("disabled")):(b.addClass("disabled"),a("input",b).prop("disabled",!0))},init:function(b){var c=this,d=a("#image-editor-"+c.postid),e=c.intval(a("#imgedit-x-"+b).val()),f=c.intval(a("#imgedit-y-"+b).val());c.postid!=b&&d.length&&c.close(c.postid),c.hold.w=c.hold.ow=e,c.hold.h=c.hold.oh=f,c.hold.xy_ratio=e/f,c.hold.sizer=parseFloat(a("#imgedit-sizer-"+b).val()),c.postid=b,a("#imgedit-response-"+b).empty(),a('input[type="text"]',"#imgedit-panel-"+b).keypress(function(b){var c=b.keyCode;return c>36&&41>c&&a(this).blur(),13==c?(b.preventDefault(),b.stopPropagation(),!1):void 0})},toggleEditor:function(b,c){var d=a("#imgedit-wait-"+b);c?d.height(a("#imgedit-panel-"+b).height()).fadeIn("fast"):d.fadeOut("fast")},toggleHelp:function(b){return a(b).siblings(".imgedit-help").slideToggle("fast"),!1},getTarget:function(b){return a('input[name="imgedit-target-'+b+'"]:checked',"#imgedit-save-target-"+b).val()||"full"},scaleChanged:function(b,c){var d=a("#imgedit-scale-width-"+b),e=a("#imgedit-scale-height-"+b),f=a("#imgedit-scale-warn-"+b),g="",h="";c?(h=""!=d.val()?Math.round(d.val()/this.hold.xy_ratio):"",e.val(h)):(g=""!=e.val()?Math.round(e.val()*this.hold.xy_ratio):"",d.val(g)),h&&h>this.hold.oh||g&&g>this.hold.ow?f.css("visibility","visible"):f.css("visibility","hidden")},getSelRatio:function(b){var c=this.hold.w,d=this.hold.h,e=this.intval(a("#imgedit-crop-width-"+b).val()),f=this.intval(a("#imgedit-crop-height-"+b).val());return e&&f?e+":"+f:c&&d?c+":"+d:"1:1"},filterHistory:function(b,c){var d,e,f,g,h=a("#imgedit-history-"+b).val(),i=[];if(""!=h){if(h=JSON.parse(h),d=this.intval(a("#imgedit-undone-"+b).val()),d>0)for(;d>0;)h.pop(),d--;if(c){if(!h.length)return this.hold.w=this.hold.ow,this.hold.h=this.hold.oh,"";f=h[h.length-1],f=f.c||f.r||f.f||!1,f&&(this.hold.w=f.fw,this.hold.h=f.fh)}for(e in h)g=h[e],g.hasOwnProperty("c")?i[e]={c:{x:g.c.x,y:g.c.y,w:g.c.w,h:g.c.h}}:g.hasOwnProperty("r")?i[e]={r:g.r.r}:g.hasOwnProperty("f")&&(i[e]={f:g.f.f});return JSON.stringify(i)}return""},refreshEditor:function(b,c,d){var e,f,g=this;g.toggleEditor(b,1),e={action:"imgedit-preview",_ajax_nonce:c,postid:b,history:g.filterHistory(b,1),rand:g.intval(1e6*Math.random())},f=a('').on("load",function(){var c,e,g=a("#imgedit-crop-"+b),h=imageEdit;g.empty().append(f),c=Math.max(h.hold.w,h.hold.h),e=Math.max(a(f).width(),a(f).height()),h.hold.sizer=c>e?e/c:1,h.initCrop(b,f,g),h.setCropSelection(b,0),"unknown"!=typeof d&&null!=d&&d(),a("#imgedit-history-"+b).val()&&0==a("#imgedit-undone-"+b).val()?a("input.imgedit-submit-btn","#imgedit-panel-"+b).removeAttr("disabled"):a("input.imgedit-submit-btn","#imgedit-panel-"+b).prop("disabled",!0),h.toggleEditor(b,0)}).on("error",function(){a("#imgedit-crop-"+b).empty().append('

'+imageEditL10n.error+"

"),g.toggleEditor(b,0)}).attr("src",ajaxurl+"?"+a.param(e))},action:function(b,c,d){var e,f,g,h,i,j=this;if(j.notsaved(b))return!1;if(e={action:"image-editor",_ajax_nonce:c,postid:b},"scale"==d){if(f=a("#imgedit-scale-width-"+b),g=a("#imgedit-scale-height-"+b),h=j.intval(f.val()),i=j.intval(g.val()),1>h)return f.focus(),!1;if(1>i)return g.focus(),!1;if(h==j.hold.ow||i==j.hold.oh)return!1;e["do"]="scale",e.fwidth=h,e.fheight=i}else{if("restore"!=d)return!1;e["do"]="restore"}j.toggleEditor(b,1),a.post(ajaxurl,e,function(c){a("#image-editor-"+b).empty().append(c),j.toggleEditor(b,0)})},save:function(b,c){var d,e=this.getTarget(b),f=this.filterHistory(b,0);return""==f?!1:(this.toggleEditor(b,1),d={action:"image-editor",_ajax_nonce:c,postid:b,history:f,target:e,context:a("#image-edit-context").length?a("#image-edit-context").val():null,"do":"save"},a.post(ajaxurl,d,function(c){var d=JSON.parse(c);return d.error?(a("#imgedit-response-"+b).html('

'+d.error+"

"),imageEdit.close(b),void 0):(d.fw&&d.fh&&a("#media-dims-"+b).html(d.fw+" × "+d.fh),d.thumbnail&&a(".thumbnail","#thumbnail-head-"+b).attr("src",""+d.thumbnail),d.msg&&a("#imgedit-response-"+b).html('

'+d.msg+"

"),imageEdit.close(b),void 0)}),void 0)},open:function(b,c){var d,e=a("#image-editor-"+b),f=a("#media-head-"+b),g=a("#imgedit-open-btn-"+b),h=g.siblings(".spinner");g.prop("disabled",!0),h.show(),d={action:"image-editor",_ajax_nonce:c,postid:b,"do":"open"},e.load(ajaxurl,d,function(){e.fadeIn("fast"),f.fadeOut("fast",function(){g.removeAttr("disabled"),h.hide()})})},imgLoaded:function(b){var c=a("#image-preview-"+b),d=a("#imgedit-crop-"+b);this.initCrop(b,c,d),this.setCropSelection(b,0),this.toggleEditor(b,0)},initCrop:function(b,c,d){var e=this,f=a("#imgedit-sel-width-"+b),g=a("#imgedit-sel-height-"+b);e.iasapi=a(c).imgAreaSelect({parent:d,instance:!0,handles:!0,keys:!0,minWidth:3,minHeight:3,onInit:function(){d.children().mousedown(function(a){var c,d,f=!1;a.shiftKey&&(c=e.iasapi.getSelection(),d=e.getSelRatio(b),f=c&&c.width&&c.height?c.width+":"+c.height:d),e.iasapi.setOptions({aspectRatio:f})})},onSelectStart:function(){imageEdit.setDisabled(a("#imgedit-crop-sel-"+b),1)},onSelectEnd:function(a,c){imageEdit.setCropSelection(b,c)},onSelectChange:function(a,b){var c=imageEdit.hold.sizer;f.val(imageEdit.round(b.width/c)),g.val(imageEdit.round(b.height/c))}})},setCropSelection:function(b,c){var d,e=a("#imgedit-minthumb-"+b).val()||"128:128",f=this.hold.sizer;return e=e.split(":"),c=c||0,!c||c.width<3&&c.height<3?(this.setDisabled(a(".imgedit-crop","#imgedit-panel-"+b),0),this.setDisabled(a("#imgedit-crop-sel-"+b),0),a("#imgedit-sel-width-"+b).val(""),a("#imgedit-sel-height-"+b).val(""),a("#imgedit-selection-"+b).val(""),!1):c.width0;)g.pop(),i--;h.val(0),g.push(b),f.val(JSON.stringify(g)),e.refreshEditor(c,d,function(){e.setDisabled(a("#image-undo-"+c),!0),e.setDisabled(a("#image-redo-"+c),!1)})},rotate:function(b,c,d,e){return a(e).hasClass("disabled")?!1:(this.addStep({r:{r:b,fw:this.hold.h,fh:this.hold.w}},c,d),void 0)},flip:function(b,c,d,e){return a(e).hasClass("disabled")?!1:(this.addStep({f:{f:b,fw:this.hold.w,fh:this.hold.h}},c,d),void 0)},crop:function(b,c,d){var e=a("#imgedit-selection-"+b).val(),f=this.intval(a("#imgedit-sel-width-"+b).val()),g=this.intval(a("#imgedit-sel-height-"+b).val());return a(d).hasClass("disabled")||""==e?!1:(e=JSON.parse(e),e.w>0&&e.h>0&&f>0&&g>0&&(e.fw=f,e.fh=g,this.addStep({c:e},b,c)),void 0)},undo:function(b,c){var d=this,e=a("#image-undo-"+b),f=a("#imgedit-undone-"+b),g=d.intval(f.val())+1;e.hasClass("disabled")||(f.val(g),d.refreshEditor(b,c,function(){var c=a("#imgedit-history-"+b),f=""!=c.val()?JSON.parse(c.val()):new Array;d.setDisabled(a("#image-redo-"+b),!0),d.setDisabled(e,g0)}))},setNumSelection:function(b){var c,d,e,f,g,h=a("#imgedit-sel-width-"+b),i=a("#imgedit-sel-height-"+b),j=this.intval(h.val()),k=this.intval(i.val()),l=a("#image-preview-"+b),m=l.height(),n=l.width(),o=this.hold.sizer,p=this.iasapi;return 1>j?(h.val(""),!1):1>k?(i.val(""),!1):(j&&k&&(c=p.getSelection())&&(f=c.x1+Math.round(j*o),g=c.y1+Math.round(k*o),d=c.x1,e=c.y1,f>n&&(d=0,f=n,h.val(Math.round(f/o))),g>m&&(e=0,g=m,i.val(Math.round(g/o))),p.setSelection(d,e,f,g),p.update(),this.setCropSelection(b,p.getSelection())),void 0)},round:function(a){var b;return a=Math.round(a),this.hold.sizer>.6?a:(b=a.toString().slice(-1),"1"==b?a-1:"9"==b?a+1:a)},setRatioSelection:function(b,c,d){var e,f,g=this.intval(a("#imgedit-crop-width-"+b).val()),h=this.intval(a("#imgedit-crop-height-"+b).val()),i=a("#image-preview-"+b).height();return this.intval(a(d).val())?(g&&h&&(this.iasapi.setOptions({aspectRatio:g+":"+h}),(e=this.iasapi.getSelection(!0))&&(f=Math.ceil(e.y1+(e.x2-e.x1)/(g/h)),f>i&&(f=i,c?a("#imgedit-crop-height-"+b).val(""):a("#imgedit-crop-width-"+b).val("")),this.iasapi.setSelection(e.x1,e.y1,e.x2,f),this.iasapi.update())),void 0):(a(d).val(""),void 0)}}}(jQuery);