/**
 * jquery.Jcrop.js v0.9.8
 * jQuery Image Cropping Plugin
 * @author Kelly Hallman <khallman@gmail.com>
 * Copyright (c) 2008-2009 Kelly Hallman - released under MIT License {{{
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.

 * }}}
 */

(function(f){f.Jcrop=function(v,r){function d(a){return""+parseInt(a)+"px"}function N(a){a=f(a).offset();return[a.left,a.top]}function B(a){return[a.pageX-L[0],a.pageY-L[1]]}function ca(a,l){return function(k){if(b.aspectRatio)switch(a){case "e":k[1]=l.y+1;break;case "w":k[1]=l.y+1;break;case "n":k[0]=l.x+1;break;case "s":k[0]=l.x+1}else switch(a){case "e":k[1]=l.y2;break;case "w":k[1]=l.y2;break;case "n":k[0]=l.x2;break;case "s":k[0]=l.x2}j.setCurrent(k);m.update()}}function da(a){var b=a;O.watchKeys();
return function(a){j.moveOffset([a[0]-b[0],a[1]-b[1]]);b=a;m.update()}}function U(a){switch(a){case "n":return"sw";case "s":return"nw";case "e":return"nw";case "w":return"ne";case "ne":return"sw";case "nw":return"se";case "se":return"nw";case "sw":return"ne"}}function V(a){return function(l){if(b.disabled)return!1;if(a=="move"&&!b.allowMove)return!1;F=!0;var k=B(l);L=N(s);A.setCursor(a=="move"?a:a+"-resize");if(a=="move")A.activateHandlers(da(k),P);else{var k=j.getFixed(),t=U(a),c=j.getCorner(U(t));
j.setPressed(j.getCorner(t));j.setCurrent(c);A.activateHandlers(ca(a,k),P)}l.stopPropagation();l.preventDefault();return!1}}function Q(a){return{x:parseInt(a.x*u),y:parseInt(a.y*y),x2:parseInt(a.x2*u),y2:parseInt(a.y2*y),w:parseInt(a.w*u),h:parseInt(a.h*y)}}function P(){var a=j.getFixed();a.w>b.minSelect[0]&&a.h>b.minSelect[1]?(m.enableHandles(),m.done()):m.release();A.setCursor(b.allowSelect?"crosshair":"default")}function ea(a){j.setCurrent(a);m.update()}function W(){var a=f("<div></div>").addClass(b.baseClass+
"-tracker");f.browser.msie&&a.css({opacity:0,backgroundColor:"white"});return a}function X(a){Y([a[0]/u,a[1]/y,a[2]/u,a[3]/y])}function Y(a){j.setPressed([a[0],a[1]]);j.setCurrent([a[2],a[3]]);m.update()}function Z(a){typeof a!="object"&&(a={});b=f.extend(b,a);if(typeof b.onChange!=="function")b.onChange=function(){};if(typeof b.onSelect!=="function")b.onSelect=function(){}}function R(a){b.allowResize?a?m.enableOnly():m.enableHandles():m.disableHandles();A.setCursor(b.allowSelect?"crosshair":"default");
m.setCursor(b.allowMove?"move":"default");S.css("backgroundColor",b.bgColor);"setSelect"in b&&(X(r.setSelect),m.done(),delete b.setSelect);"trueSize"in b&&(u=b.trueSize[0]/q,y=b.trueSize[1]/o);G=b.maxSize[0]||0;H=b.maxSize[1]||0;I=b.minSize[0]||0;J=b.minSize[1]||0;"outerImage"in b&&(s.attr("src",b.outerImage),delete b.outerImage);m.refresh()}typeof v!=="object"&&(v=f(v)[0]);typeof r!=="object"&&(r={});if(!("trackDocument"in r)&&(r.trackDocument=f.browser.msie?!1:!0,f.browser.msie&&f.browser.version.split(".")[0]==
"8"))r.trackDocument=!0;if(!("keySupport"in r))r.keySupport=f.browser.msie?!1:!0;var b={trackDocument:!1,baseClass:"jcrop",addClass:null,bgColor:"black",bgOpacity:0.6,borderOpacity:0.4,handleOpacity:0.5,handlePad:5,handleSize:9,handleOffset:5,edgeMargin:14,aspectRatio:0,keySupport:!0,cornerHandles:!0,sideHandles:!0,drawBorders:!0,dragEdges:!0,boxWidth:0,boxHeight:0,boundary:8,animationDelay:20,swingSpeed:3,allowSelect:!0,allowMove:!0,allowResize:!0,minSelect:[0,0],maxSize:[0,0],minSize:[0,0],onChange:function(){},
onSelect:function(){}};Z(r);var C=f(v),s=C.clone().removeAttr("id").css({position:"absolute"});s.width(C.width());s.height(C.height());C.after(s).hide();(function(a,b,k){var t=a.width(),c=a.height();t>b&&b>0&&(t=b,c=b/a.width()*a.height());c>k&&k>0&&(c=k,t=k/a.height()*a.width());u=a.width()/t;y=a.height()/c;a.width(t).height(c)})(s,b.boxWidth,b.boxHeight);var q=s.width(),o=s.height(),S=f("<div />").width(q).height(o).addClass(b.baseClass+"-holder").css({position:"relative",backgroundColor:b.bgColor}).insertAfter(C).append(s);
b.addClass&&S.addClass(b.addClass);var $=f("<img />").attr("src",s.attr("src")).css("position","absolute").width(q).height(o),T=f("<div />").width("100%").height("100%").css({zIndex:310,position:"absolute",overflow:"hidden"}).append($),E=f("<div />").width("100%").height("100%").css("zIndex",320),M=f("<div />").css({position:"absolute",zIndex:300}).insertBefore(s).append(T,E),D=b.boundary,K=W().width(q+D*2).height(o+D*2).css({position:"absolute",top:d(-D),left:d(-D),zIndex:290}).mousedown(function(a){if(b.disabled)return!1;
if(!b.allowSelect)return!1;F=!0;L=N(s);m.disableHandles();"crosshair"!=aa&&(A.setCursor("crosshair"),aa="crosshair");var l=B(a);j.setPressed(l);A.activateHandlers(ea,P);O.watchKeys();m.update();a.stopPropagation();a.preventDefault();return!1}),G,H,I,J,u,y,L=N(s),F,aa,ba,fa,j=function(){function a(){if(!b.aspectRatio){var a=d-c,x=f-e;G&&Math.abs(a)>G&&(d=a>0?c+G:c-G);H&&Math.abs(x)>H&&(f=x>0?e+H:e-H);J&&Math.abs(x)<J&&(f=x>0?e+J:e-J);I&&Math.abs(a)<I&&(d=a>0?c+I:c-I);c<0&&(d-=c,c-=c);e<0&&(f-=e,e-=
e);d<0&&(c-=d,d-=d);f<0&&(e-=f,f-=f);d>q&&(a=d-q,c-=a,d-=a);f>o&&(a=f-o,e-=a,f-=a);c>q&&(a=c-o,f-=a,e-=a);e>o&&(a=e-o,f-=a,e-=a);return t(k(c,e,d,f))}var a=b.aspectRatio,x=b.minSize[0]/u,l=b.maxSize[0]/u,j=d-c,p=f-e,n=Math.abs(j),g=Math.abs(p);l==0&&(l=q*10);n/g<a?(n=f,w=g*a,g=j<0?c-w:w+c,g<0?(g=0,h=Math.abs((g-c)/a),n=p<0?e-h:h+e):g>q&&(g=q,h=Math.abs((g-c)/a),n=p<0?e-h:h+e)):(g=d,h=n/a,n=p<0?e-h:e+h,n<0?(n=0,w=Math.abs((n-e)*a),g=j<0?c-w:w+c):n>o&&(n=o,w=Math.abs(n-e)*a,g=j<0?c-w:w+c));g>c?(g-c<
x?g=c+x:g-c>l&&(g=c+l),n=n>e?e+(g-c)/a:e-(g-c)/a):g<c&&(c-g<x?g=c-x:c-g>l&&(g=c-l),n=n>e?e+(c-g)/a:e-(c-g)/a);g<0?(c-=g,g=0):g>q&&(c-=g-q,g=q);n<0?(e-=n,n=0):n>o&&(e-=n-o,n=o);return last=t(k(c,e,g,n))}function l(a){a[0]<0&&(a[0]=0);a[1]<0&&(a[1]=0);a[0]>q&&(a[0]=q);a[1]>o&&(a[1]=o);return[a[0],a[1]]}function k(a,c,b,e){var f=a,d=b,g=c,l=e;b<a&&(f=b,d=a);e<c&&(g=e,l=c);return[Math.round(f),Math.round(g),Math.round(d),Math.round(l)]}function t(a){return{x:a[0],y:a[1],x2:a[2],y2:a[3],w:a[2]-a[0],h:a[3]-
a[1]}}var c=0,e=0,d=0,f=0,j,m;return{flipCoords:k,setPressed:function(a){a=l(a);d=c=a[0];f=e=a[1]},setCurrent:function(a){a=l(a);j=a[0]-d;m=a[1]-f;d=a[0];f=a[1]},getOffset:function(){return[j,m]},moveOffset:function(a){var b=a[0],a=a[1];0>c+b&&(b-=b+c);0>e+a&&(a-=a+e);o<f+a&&(a+=o-(f+a));q<d+b&&(b+=q-(d+b));c+=b;d+=b;e+=a;f+=a},getCorner:function(b){var c=a();switch(b){case "ne":return[c.x2,c.y];case "nw":return[c.x,c.y];case "se":return[c.x2,c.y2];case "sw":return[c.x,c.y2]}},getFixed:a}}(),m=function(){function a(a){a=
f("<div />").css({position:"absolute",opacity:b.borderOpacity}).addClass(b.baseClass+"-"+a);T.append(a);return a}function l(a,c){var b=f("<div />").mousedown(V(a)).css({cursor:a+"-resize",position:"absolute",zIndex:c});E.append(b);return b}function k(a){var c=b.handleSize,e=g,f=c;switch(a){case "n":case "s":c="100%";break;case "e":case "w":f="100%"}return l(a,z++).width(c).height(f).css({top:d(-e+1),left:d(-e+1)})}function t(a){for(i in a)p[a[i]]=l(a[i],z++).css({top:d(-g+1),left:d(-g+1),opacity:b.handleOpacity}).addClass(b.baseClass+
"-handle")}function c(a){var c=Math.round(a.h/2-g),b=Math.round(a.w/2-g);west=-g+1;var e=a.w-g,a=a.h-g;"e"in p&&p.e.css({top:d(c),left:d(e)})&&p.w.css({top:d(c)})&&p.s.css({top:d(a),left:d(b)})&&p.n.css({left:d(b)});"ne"in p&&p.ne.css({left:d(e)})&&p.se.css({top:d(a),left:d(e)})&&p.sw.css({top:d(a)});"b"in p&&p.b.css({top:d(a)})&&p.r.css({left:d(e)})}function e(){var a=j.getFixed();j.setPressed([a.x,a.y]);j.setCurrent([a.x2,a.y2]);m()}function m(){if(x)return o()}function o(){var a=j.getFixed(),e=
a.h;M.width(a.w).height(e);var e=a.x,f=a.y;$.css({top:d(-f),left:d(-e)});M.css({top:d(f),left:d(e)});b.drawBorders&&r.right.css({left:d(a.w-1)})&&r.bottom.css({top:d(a.h-1)});n&&c(a);x||(M.show(),s.css("opacity",b.bgOpacity),x=!0);b.onChange(Q(a))}function q(){n=!0;if(b.allowResize)return c(j.getFixed()),E.show(),!0}function u(){n=!1;E.hide()}function v(a){(ba=a)?u():q()}var x,z=370,r={},p={},n=!1,g=b.handleOffset;b.drawBorders&&(r={top:a("hline").css("top",f.browser.msie?d(-1):d(0)),bottom:a("hline"),
left:a("vline"),right:a("vline")});if(b.dragEdges)p.t=k("n"),p.b=k("s"),p.r=k("e"),p.l=k("w");b.sideHandles&&t(["n","s","e","w"]);b.cornerHandles&&t(["sw","nw","ne","se"]);var y=W().mousedown(V("move")).css({cursor:"move",position:"absolute",zIndex:360});T.append(y);u();return{updateVisible:m,update:o,release:function(){u();M.hide();s.css("opacity",1);x=!1},refresh:e,setCursor:function(a){y.css("cursor",a)},enableHandles:q,enableOnly:function(){n=!0},showHandles:function(){n&&(c(j.getFixed()),E.show())},
disableHandles:u,animMode:v,done:function(){v(!1);e()}}}(),A=function(){function a(a){k(B(a))}function d(e){e.preventDefault();e.stopPropagation();F&&(F=!1,t(B(e)),b.onSelect(Q(j.getFixed())),K.css({zIndex:290}),c&&f(document).unbind("mousemove",a).unbind("mouseup",d),k=function(){},t=function(){});return!1}var k=function(){},t=function(){},c=b.trackDocument;c||K.mousemove(a).mouseup(d).mouseout(d);s.before(K);return{activateHandlers:function(b,j){F=!0;k=b;t=j;K.css({zIndex:450});c&&f(document).mousemove(a).mouseup(d);
return!1},setCursor:function(a){K.css("cursor",a)}}}(),O=function(){function a(a,c,e){b.allowMove&&(j.moveOffset([c,e]),m.updateVisible());a.preventDefault();a.stopPropagation()}var d=f('<input type="radio" />').css({position:"absolute",left:"-30px"}).keypress(function(b){if(b.ctrlKey)return!0;var c=(fa=b.shiftKey?!0:!1)?10:1;switch(b.keyCode){case 37:a(b,-c,0);break;case 39:a(b,c,0);break;case 38:a(b,0,-c);break;case 40:a(b,0,c);break;case 27:m.release();break;case 9:return!0}return nothing(b)}).blur(function(){d.hide()}),
k=f("<div />").css({position:"absolute",overflow:"hidden"}).append(d);b.keySupport&&k.insertBefore(s);return{watchKeys:function(){b.keySupport&&(d.show(),d.focus())}}}();E.hide();R(!0);D={animateTo:function(a){var d=a[2]/u,f=a[3]/y;if(!ba){var a=j.flipCoords(a[0]/u,a[1]/y,d,f),o=j.getFixed(),c=initcr=[o.x,o.y,o.x2,o.y2],e=b.animationDelay,q=c[0],s=c[1],d=c[2],f=c[3],r=a[0]-initcr[0],v=a[1]-initcr[1],A=a[2]-initcr[2],x=a[3]-initcr[3],z=0,B=b.swingSpeed;m.animMode(!0);var p=function(){return function(){z+=
(100-z)/B;c[0]=q+z/100*r;c[1]=s+z/100*v;c[2]=d+z/100*A;c[3]=f+z/100*x;z<100?window.setTimeout(p,e):m.done();z>=99.8&&(z=100);Y(c)}}();window.setTimeout(p,e)}},setSelect:X,setOptions:function(a){Z(a);R()},tellSelect:function(){return Q(j.getFixed())},tellScaled:function(){return j.getFixed()},disable:function(){b.disabled=!0;m.disableHandles();m.setCursor("default");A.setCursor("default")},enable:function(){b.disabled=!1;R()},cancel:function(){m.done();A.activateHandlers(null,null)},focus:O.watchKeys,
getBounds:function(){return[q*u,o*y]},getWidgetSize:function(){return[q,o]},release:m.release,destroy:function(){S.remove();C.show()}};C.data("Jcrop",D);return D};f.fn.Jcrop=function(v){function r(d){var r=v.useImg||d.src,B=new Image;B.onload=function(){f.Jcrop(d,v)};B.src=r}typeof v!=="object"&&(v={});this.each(function(){if(f(this).data("Jcrop"))if(v=="api")return f(this).data("Jcrop");else f(this).data("Jcrop").setOptions(v);else r(this)});return this}})(jQuery);
