(function(c){function y(){return c("<div/>")}var z=Math.abs,l=Math.max,s=Math.min,m=Math.round;c.imgAreaSelect=function(n,b){function B(a){return a+J.left-v.left}function O(a){return a+J.top-v.top}function H(a){return a-J.left+v.left}function I(a){return a-J.top+v.top}function E(a){var i=a||ba;a=a||ca;return{x1:m(d.x1*i),y1:m(d.y1*a),x2:m(d.x2*i),y2:m(d.y2*a),width:m(d.x2*i)-m(d.x1*i),height:m(d.y2*a)-m(d.y1*a)}}function ma(a,i,w,U,S){var na=S||ba;S=S||ca;d={x1:m(a/na),y1:m(i/S),x2:m(w/na),y2:m(U/
S)};d.width=B(d.x2)-B(d.x1);d.height=B(d.y2)-B(d.y1)}function K(){if(t.width()){J={left:m(t.offset().left),top:m(t.offset().top)};C=t.width();A=t.height();if(c().jquery=="1.3.2"&&c.browser.safari&&V=="fixed"){J.top+=l(document.documentElement.scrollTop,c("body").scrollTop());J.left+=l(document.documentElement.scrollLeft,c("body").scrollLeft())}v=c.inArray(P.css("position"),["absolute","relative"])+1?{left:m(P.offset().left)-P.scrollLeft(),top:m(P.offset().top)-P.scrollTop()}:V=="fixed"?{left:c(document).scrollLeft(),
top:c(document).scrollTop()}:{left:0,top:0};p=B(0);q=O(0)}}function W(a){if(X){j.css({left:B(d.x1),top:O(d.y1)}).add(Q).width(F=d.width).height(L=d.height);Q.add(r).add(o).css({left:0,top:0});r.width(l(F-r.outerWidth()+r.innerWidth(),0)).height(l(L-r.outerHeight()+r.innerHeight(),0));c(k[0]).css({left:p,top:q,width:d.x1,height:A});c(k[1]).css({left:p+d.x1,top:q,width:F,height:d.y1});c(k[2]).css({left:p+d.x2,top:q,width:C-d.x2,height:A});c(k[3]).css({left:p+d.x1,top:q+d.y2,width:F,height:A-d.y2});
F-=o.outerWidth();L-=o.outerHeight();switch(o.length){case 8:c(o[4]).css({left:F/2});c(o[5]).css({left:F,top:L/2});c(o[6]).css({left:F/2,top:L});c(o[7]).css({top:L/2});case 4:o.slice(1,3).css({left:F});o.slice(2,4).css({top:L})}if(a!==false){c.imgAreaSelect.keyPress!=oa&&c(document).unbind(c.imgAreaSelect.keyPress,c.imgAreaSelect.onKeyPress);if(b.keys)c(document)[c.imgAreaSelect.keyPress](c.imgAreaSelect.onKeyPress=oa)}if(c.browser.msie&&r.outerWidth()-r.innerWidth()==2){r.css("margin",0);setTimeout(function(){r.css("margin",
"auto")},0)}}}function da(a){K();W(a);e=B(d.x1);f=O(d.y1);g=B(d.x2);h=O(d.y2)}function ea(a,i){b.fadeSpeed?a.fadeOut(b.fadeSpeed,i):a.hide()}function M(a){var i=H(a.pageX-v.left)-d.x1;a=I(a.pageY-v.top)-d.y1;if(!fa){K();fa=true;j.one("mouseout",function(){fa=false})}u="";if(b.resizable){if(a<=Y)u="n";else if(a>=d.height-Y)u="s";if(i<=Y)u+="w";else if(i>=d.width-Y)u+="e"}j.css("cursor",u?u+"-resize":b.movable?"move":"");Z&&Z.toggle()}function pa(a){c("body").css("cursor","");if(b.autoHide||d.width*
d.height==0)ea(j.add(k),function(){c(this).hide()});b.onSelectEnd(n,E(),a);c(document).unbind("mousemove",ga);j.mousemove(M)}function qa(a){if(a.which!=1)return false;if(u){c("body").css("cursor",u+"-resize");e=B(d[/w/.test(u)?"x2":"x1"]);f=O(d[/n/.test(u)?"y2":"y1"]);c(document).mousemove(ga).one("mouseup",pa);j.unbind("mousemove",M)}else if(b.movable){ha=p+d.x1-(a.pageX-v.left);ia=q+d.y1-(a.pageY-v.top);j.unbind("mousemove",M);c(document).mousemove(ra).one("mouseup",function(){b.onSelectEnd(n,E());
c(document).unbind("mousemove",ra);j.mousemove(M)})}else t.mousedown(a);return false}function ja(){g=l(p,s(p+C,e+z(h-f)*x*(g>e||-1)));h=m(l(q,s(q+A,f+z(g-e)/x*(h>f||-1))));g=m(g)}function ka(){h=l(q,s(q+A,f+z(g-e)/x*(h>f||-1)));g=m(l(p,s(p+C,e+z(h-f)*x*(g>e||-1))));h=m(h)}function la(){if(z(g-e)<b.minWidth){g=e-b.minWidth*(g<e||-1);if(g<p)e=p+b.minWidth;else if(g>p+C)e=p+C-b.minWidth}if(z(h-f)<b.minHeight){h=f-b.minHeight*(h<f||-1);if(h<q)f=q+b.minHeight;else if(h>q+A)f=q+A-b.minHeight}g=l(p,s(g,
p+C));h=l(q,s(h,q+A));if(x)z(g-e)/x>z(h-f)?ka():ja();if(z(g-e)>b.maxWidth){g=e-b.maxWidth*(g<e||-1);x&&ka()}if(z(h-f)>b.maxHeight){h=f-b.maxHeight*(h<f||-1);x&&ja()}d={x1:H(s(e,g)),x2:H(l(e,g)),y1:I(s(f,h)),y2:I(l(f,h)),width:z(g-e),height:z(h-f)};W();b.onSelectChange(n,E())}function ga(a){g=u==""||/w|e/.test(u)||x?a.pageX-v.left:B(d.x2);h=u==""||/n|s/.test(u)||x?a.pageY-v.top:O(d.y2);la();return false}function T(a,i){g=(e=a)+d.width;h=(f=i)+d.height;d=c.extend(d,{x1:H(e),y1:I(f),x2:H(g),y2:I(h)});
W();b.onSelectChange(n,E())}function ra(a){e=l(p,s(ha+(a.pageX-v.left),p+C-d.width));f=l(q,s(ia+(a.pageY-v.top),q+A-d.height));T(e,f);a.preventDefault();return false}function sa(){K();g=e;h=f;la();u="";if(k.is(":not(:visible)"))j.add(k).hide().fadeIn(b.fadeSpeed||0);X=true;c(document).unbind("mouseup",ta).mousemove(ga).one("mouseup",pa);j.unbind("mousemove",M);b.onSelectStart(n,E())}function ta(){c(document).unbind("mousemove",sa);ea(j.add(k));d={x1:H(e),y1:I(f),x2:H(e),y2:I(f),width:0,height:0};
b.onSelectChange(n,E());b.onSelectEnd(n,E())}function ua(a){if(a.which!=1||k.is(":animated"))return false;K();ha=e=a.pageX-v.left;ia=f=a.pageY-v.top;c(document).one("mousemove",sa).one("mouseup",ta);return false}function $(){da(false)}function va(){wa=true;xa(b=c.extend({classPrefix:"imgareaselect",movable:true,resizable:true,parent:"body",onInit:function(){},onSelectStart:function(){},onSelectChange:function(){},onSelectEnd:function(){}},b));j.add(k).css({visibility:""});if(b.show){X=true;K();W();
j.add(k).hide().fadeIn(b.fadeSpeed||0)}setTimeout(function(){b.onInit(n,E())},0)}function aa(a,i){for(option in i)b[option]!==undefined&&a.css(i[option],b[option])}function xa(a){if(a.parent)(P=c(a.parent)).append(j.add(k));b=c.extend(b,a);K();if(a.handles!=null){o.remove();o=c([]);for(R=a.handles?a.handles=="corners"?4:8:0;R--;)o=o.add(y());o.addClass(b.classPrefix+"-handle").css({position:"absolute",fontSize:0,zIndex:N+1||1});parseInt(o.css("width"))||o.width(5).height(5);if(D=b.borderWidth)o.css({borderWidth:D,
borderStyle:"solid"});aa(o,{borderColor1:"border-color",borderColor2:"background-color",borderOpacity:"opacity"})}ba=b.imageWidth/C||1;ca=b.imageHeight/A||1;if(a.x1!=null){ma(a.x1,a.y1,a.x2,a.y2);a.show=!a.hide}if(a.keys)b.keys=c.extend({shift:1,ctrl:"resize"},a.keys);k.addClass(b.classPrefix+"-outer");Q.addClass(b.classPrefix+"-selection");for(R=0;R++<4;)c(r[R-1]).addClass(b.classPrefix+"-border"+R);aa(Q,{selectionColor:"background-color",selectionOpacity:"opacity"});aa(r,{borderOpacity:"opacity",
borderWidth:"border-width"});aa(k,{outerColor:"background-color",outerOpacity:"opacity"});if(D=b.borderColor1)c(r[0]).css({borderStyle:"solid",borderColor:D});if(D=b.borderColor2)c(r[1]).css({borderStyle:"dashed",borderColor:D});j.append(Q.add(r).add(o).add(Z));if(c.browser.msie){if(D=k.css("filter").match(/opacity=([0-9]+)/))k.css("opacity",D[1]/100);if(D=r.css("filter").match(/opacity=([0-9]+)/))r.css("opacity",D[1]/100)}if(a.hide)ea(j.add(k));else if(a.show&&wa){X=true;j.add(k).fadeIn(b.fadeSpeed||
0);da()}x=(ya=(b.aspectRatio||"").split(/:/))[0]/ya[1];if(b.disable||b.enable===false){j.unbind("mousemove",M).unbind("mousedown",qa);t.add(k).unbind("mousedown",ua);c(window).unbind("resize",$);t.add(t.parents()).unbind("scroll",$)}else if(b.enable||b.disable===false){if(b.resizable||b.movable)j.mousemove(M).mousedown(qa);b.persistent||t.add(k).mousedown(ua);c(window).resize($);t.add(t.parents()).scroll($)}b.enable=b.disable=undefined}var t=c(n),wa,j=y(),Q=y(),r=y().add(y()).add(y()).add(y()),k=
y().add(y()).add(y()).add(y()),o=c([]),Z,p,q,J,C,A,P,v,N=0,V="absolute",ha,ia,ba,ca,Y=10,u,x,X,e,f,g,h,d={x1:0,y1:0,x2:0,y2:0,width:0,height:0},G,ya,R,D,F,L,fa,oa=function(a){var i=b.keys,w,U=a.keyCode||a.which;w=!isNaN(i.alt)&&(a.altKey||a.originalEvent.altKey)?i.alt:!isNaN(i.ctrl)&&a.ctrlKey?i.ctrl:!isNaN(i.shift)&&a.shiftKey?i.shift:!isNaN(i.arrows)?i.arrows:10;if(i.arrows=="resize"||i.shift=="resize"&&a.shiftKey||i.ctrl=="resize"&&a.ctrlKey||i.alt=="resize"&&(a.altKey||a.originalEvent.altKey)){switch(U){case 37:w=
-w;case 39:a=l(e,g);e=s(e,g);g=l(a+w,e);x&&ka();break;case 38:w=-w;case 40:a=l(f,h);f=s(f,h);h=l(a+w,f);x&&ja();break;default:return}la()}else{e=s(e,g);f=s(f,h);switch(U){case 37:T(l(e-w,p),f);break;case 38:T(e,l(f-w,q));break;case 39:T(e+s(w,C-H(g)),f);break;case 40:T(e,f+s(w,A-I(h)));break;default:return}}return false};this.getOptions=function(){return b};this.setOptions=xa;this.getSelection=E;this.setSelection=ma;this.update=da;this.adjust=K;for(G=t;G.length&&!G.is("body");){if(!isNaN(G.css("z-index"))&&
G.css("z-index")>N)N=G.css("z-index");if(G.css("position")=="fixed")V="fixed";G=G.parent()}if(!isNaN(b.zIndex))N=b.zIndex;c.browser.msie&&t.attr("unselectable","on");c.imgAreaSelect.keyPress=c.browser.msie||c.browser.safari?"keydown":"keypress";if(c.browser.opera)Z=y().css({width:"100%",height:"100%",position:"absolute",zIndex:N+2||2});j.add(k).css({visibility:"hidden",position:V,overflow:"hidden",zIndex:N||"0"});j.css({zIndex:N+2||2});Q.add(r).css({position:"absolute"});n.complete||n.readyState==
"complete"||!t.is("img")?va():t.one("load",va)};c.fn.imgAreaSelect=function(n){n=n||{};this.each(function(){if(c(this).data("imgAreaSelect"))c(this).data("imgAreaSelect").setOptions(n);else{if(n.enable===undefined&&n.disable===undefined)n.enable=true;c(this).data("imgAreaSelect",new c.imgAreaSelect(this,n))}});if(n.instance)return c(this).data("imgAreaSelect");return this}})(jQuery);
