define(["modules/forms/module","jcrop"],function(a){"use strict";return a.registerDirective("smartJcrop",["$q",function(a){return{restrict:"A",scope:{coords:"=",options:"=",selection:"="},link:function(b,c,d){var e,f,g,h=a.defer(),i={onSelectHandlers:[],onChangeHandlers:[],onSelect:function(a){angular.forEach(i.onSelectHandlers,function(b){b.call(e,a)})},onChange:function(a){angular.forEach(i.onChangeHandlers,function(b){b.call(e,a)})}};if(d.coords){var j=function(a){b.$apply(function(){b.coords=a})};i.onSelectHandlers.push(j),i.onChangeHandlers.push(j)}var k=$(d.smartJcropPreview),l=k.find(".preview-container"),m=k.find("img");if(k.length&&m.length){var n=function(a){if(parseInt(a.w)>0){var b=l.width()/a.w,c=l.height()/a.h;m.css({width:Math.round(b*f)+"px",height:Math.round(c*g)+"px",marginLeft:"-"+Math.round(b*a.x)+"px",marginTop:"-"+Math.round(c*a.y)+"px"})}};i.onSelectHandlers.push(n),i.onChangeHandlers.push(n)}var o={onSelect:i.onSelect,onChange:i.onChange};if(l.length&&(o.aspectRatio=l.width()/l.height()),d.selection&&b.$watch("selection",function(a,c){if(a!=c){var d="release"==a?[f/2,g/2,f/2,g/2]:a,i="release"==a?function(){e.release()}:angular.noop;h.promise.then(function(){b.options&&b.options.animate?e.animateTo(d,i):e.setSelect(d)})}}),d.options){var p=["bgOpacity","bgColor","bgFade","shade","outerImage","allowSelect","allowMove","allowResize","aspectRatio"];angular.forEach(p,function(a){b.options[a]&&(o[a]=b.options[a]),b.$watch("options."+a,function(b,c){b!=c&&h.promise.then(function(){var c={};c[a]=b,e.setOptions(c)})})}),b.$watch("options.disabled",function(a,b){a!=b&&(a?e.disable():e.enable())}),b.$watch("options.destroyed",function(a,b){a!=b&&(a?e.destroy():r())}),b.$watch("options.src",function(c,d){h=a.defer(),c!=d&&e.setImage(b.options.src,function(){h.resolve()})});var q=function(){e.setOptions({minSize:[b.options.minSizeWidth,b.options.minSizeHeight],maxSize:[b.options.maxSizeWidth,b.options.maxSizeHeight]})};b.$watch("options.minSizeWidth",function(a,b){a!=b&&q()}),b.$watch("options.minSizeHeight",function(a,b){a!=b&&q()}),b.$watch("options.maxSizeWidth",function(a,b){a!=b&&q()}),b.$watch("options.maxSizeHeight",function(a,b){a!=b&&q()})}var r=function(){c.Jcrop(o,function(){e=this;var a=this.getBounds();f=a[0],g=a[1],d.selection&&angular.isArray(b.selection)&&(b.options&&b.options.animate?e.animateTo(b.selection):e.setSelect(b.selection)),h.resolve()})};r()}}}])});