jQuery.imgAreaSelect=function(I,Z){var l=jQuery("<div></div>"),m=jQuery("<div></div>"),k=jQuery("<div></div>"),e=jQuery("<div></div>"),n=jQuery("<div></div>"),j=jQuery("<div></div>"),g=jQuery("<div></div>"),r,W,B,S=0,O=false,C,Y,X,J,G,b=10,U=[],E=0,N=1,p,a,R,Q,s,q,h,f,P={x1:0,y1:0,x2:0,y2:0,width:0,height:0};var M=l.add(m).add(k);var D=e.add(n).add(j).add(g);function c(){M.css({left:(P.x1+r.left)+"px",top:(P.y1+r.top)+"px",width:Math.max(P.width-Z.borderWidth*2,0)+"px",height:Math.max(P.height-Z.borderWidth*2,0)+"px"});e.css({left:r.left+"px",top:r.top+"px",width:P.x1+"px",height:B+"px"});n.css({left:r.left+P.x1+"px",top:r.top+"px",width:P.width+"px",height:P.y1+"px"});j.css({left:r.left+P.x2+"px",top:r.top+"px",width:W-P.x2+"px",height:B+"px"});g.css({left:r.left+P.x1+"px",top:r.top+P.y2+"px",width:P.width+"px",height:B-P.y2+"px"})}function T(H){h=H.pageX-P.x1-r.left;f=H.pageY-P.y1-r.top;U=[];if(Z.resizable){if(f<=b){U[E]="n"}else{if(f>=P.height-b){U[E]="s"}}if(h<=b){U[N]="w"}else{if(h>=P.width-b){U[N]="e"}}}k.css("cursor",U.length?U.join("")+"-resize":Z.movable?"move":"")}function L(H){if(H.which!=1){return false}if(Z.resizable&&U.length>0){jQuery("body").css("cursor",U.join("")+"-resize");R=(U[N]=="w"?P.x2:P.x1)+r.left;s=(U[E]=="n"?P.y2:P.y1)+r.top;jQuery(document).mousemove(i);k.unbind("mousemove",T);jQuery(document).one("mouseup",function(){U=[];jQuery("body").css("cursor","");if(Z.autoHide){M.add(D).hide()}Z.onSelectEnd(I,P);jQuery(document).unbind("mousemove",i);k.mousemove(T)})}else{if(Z.movable){J=P.x1+r.left;G=P.y1+r.top;Y=H.pageX;X=H.pageY;jQuery(document).mousemove(A).one("mouseup",function(){Z.onSelectEnd(I,P);jQuery(document).unbind("mousemove",A)})}else{jQuery(I).mousedown(H);return false}}}function K(){Q=Math.max(r.left,Math.min(r.left+W,R+Math.abs(q-s)*a*(Q>R?1:-1)));q=Math.round(Math.max(r.top,Math.min(r.top+B,s+Math.abs(Q-R)/a*(q>s?1:-1))));Q=Math.round(Q)}function o(){q=Math.max(r.top,Math.min(r.top+B,s+Math.abs(Q-R)/a*(q>s?1:-1)));Q=Math.round(Math.max(r.left,Math.min(r.left+W,R+Math.abs(q-s)*a*(Q>R?1:-1))));q=Math.round(q)}function i(H){Q=!U.length||U[N]||a?H.pageX:P.x2+r.left;q=!U.length||U[E]||a?H.pageY:P.y2+r.top;if(Z.minWidth&&Math.abs(Q-R)<Z.minWidth){Q=R-Z.minWidth*(Q<R?1:-1);if(Q<r.left){R=r.left+Z.minWidth}else{if(Q>r.left+W){R=r.left+W-Z.minWidth}}}if(Z.minHeight&&Math.abs(q-s)<Z.minHeight){q=s-Z.minHeight*(q<s?1:-1);if(q<r.top){s=r.top+Z.minHeight}else{if(q>r.top+B){s=r.top+B-Z.minHeight}}}Q=Math.max(r.left,Math.min(Q,r.left+W));q=Math.max(r.top,Math.min(q,r.top+B));if(a){if(Math.abs(Q-R)/a>Math.abs(q-s)){o()}else{K()}}if(Z.maxWidth&&Math.abs(Q-R)>Z.maxWidth){Q=R-Z.maxWidth*(Q<R?1:-1);if(a){o()}}if(Z.maxHeight&&Math.abs(q-s)>Z.maxHeight){q=s-Z.maxHeight*(q<s?1:-1);if(a){K()}}P.x1=Math.min(R,Q)-r.left;P.x2=Math.max(R,Q)-r.left;P.y1=Math.min(s,q)-r.top;P.y2=Math.max(s,q)-r.top;P.width=Math.abs(Q-R);P.height=Math.abs(q-s);c();Z.onSelectChange(I,P);return false}function A(H){R=Math.max(r.left,Math.min(J+H.pageX-Y,r.left+W-P.width));s=Math.max(r.top,Math.min(G+H.pageY-X,r.top+B-P.height));Q=R+P.width;q=s+P.height;P.x1=R-r.left;P.y1=s-r.top;P.x2=Q-r.left;P.y2=q-r.top;c();Z.onSelectChange(I,P);H.preventDefault();return false}function F(H){if(H.which!=1){return false}Y=R=H.pageX;X=s=H.pageY;P.x1=P.x2=R-r.left;P.y1=P.y2=s-r.top;P.width=0;P.height=0;U=[];c();M.add(D).show();jQuery(document).mousemove(i);k.unbind("mousemove",T);Z.onSelectStart(I,P);jQuery(document).one("mouseup",function(){if(Z.autoHide){M.add(D).hide()}Z.onSelectEnd(I,P);jQuery(document).unbind("mousemove",i);k.mousemove(T)});return false}this.setOptions=function(H){Z=jQuery.extend(Z,H);if(H.x1!=null){P.x1=H.x1;P.y1=H.y1;P.x2=H.x2;P.y2=H.y2;H.show=true}W=jQuery(I).width();B=jQuery(I).height();r=jQuery(I).offset();C=jQuery(I);while(C.length&&!C.is("body")){if(!isNaN(C.css("z-index"))&&C.css("z-index")>S){S=C.css("z-index")}if(C.css("position")=="fixed"){O=true}C=C.parent()}R=P.x1+r.left;s=P.y1+r.top;Q=P.x2+r.left;q=P.y2+r.top;P.width=Q-R;P.height=q-s;c();if(H.hide){M.add(D).hide()}else{if(H.show){M.add(D).show()}}M.css({borderWidth:Z.borderWidth+"px"});l.css({backgroundColor:Z.selectionColor,opacity:Z.selectionOpacity});m.css({borderStyle:"solid",borderColor:Z.borderColor1});k.css({borderStyle:"dashed",borderColor:Z.borderColor2});D.css({opacity:Z.outerOpacity,backgroundColor:Z.outerColor});a=Z.aspectRatio&&(p=Z.aspectRatio.split(/:/))?p[0]/p[1]:null;if(Z.disable||Z.enable===false){M.unbind("mousemove",T).unbind("mousedown",L);jQuery(I).add(D).unbind("mousedown",F)}else{if(Z.enable||Z.disable===false){if(Z.resizable||Z.movable){M.mousemove(T).mousedown(L)}jQuery(I).add(D).mousedown(F)}}Z.enable=Z.disable=undefined};if(jQuery.browser.msie){jQuery(I).attr("unselectable","on")}M.add(D).css({display:"none",position:O?"fixed":"absolute",overflow:"hidden",zIndex:S>0?S:null});l.css({borderStyle:"solid"});jQuery("body").append(D.add(M));initOptions={borderColor1:"#000",borderColor2:"#fff",borderWidth:1,movable:true,resizable:true,selectionColor:"#fff",selectionOpacity:0.2,outerColor:"#000",outerOpacity:0.2,onSelectStart:function(){},onSelectChange:function(){},onSelectEnd:function(){}};Z=jQuery.extend(initOptions,Z);this.setOptions(Z)};jQuery.fn.imgAreaSelect=function(A){A=A||{};this.each(function(){if(jQuery(this).data("imgAreaSelect")){jQuery(this).data("imgAreaSelect").setOptions(A)}else{if(A.enable===undefined&&A.disable===undefined){A.enable=true}jQuery(this).data("imgAreaSelect",new jQuery.imgAreaSelect(this,A))}});return this};