jQuery.imgAreaSelect={onKeyPress:null};jQuery.imgAreaSelect.init=function(l,c){var I=jQuery('<div></div>'),P=jQuery('<div></div>'),t=jQuery('<div></div>'),Y=jQuery('<div></div>'),Z=jQuery('<div></div>'),ba=jQuery('<div></div>'),bb=jQuery('<div></div>'),h,i,B,r,q,C,u,v,Q,J=0,bc,s,R,S,bd,be,K=10,k=[],L=0,M=1,bo,T,n,f,d,g,e,U,V,a={x1:0,y1:0,x2:0,y2:0,width:0,height:0};var o=I.add(P).add(t);var p=Y.add(Z).add(ba).add(bb);function w(b){return b+B.left+v.left-u.left}function x(b){return b+B.top+v.top-u.top}function y(b){return b-B.left-v.left+u.left}function z(b){return b-B.top-v.top+u.top}function D(b){return b.pageX+v.left-u.left}function E(b){return b.pageY+v.top-u.top}function F(){B=jQuery(l).offset();r=jQuery(l).width();q=jQuery(l).height();if(jQuery(C).is('body'))u=v={left:0,top:0};else{u=jQuery(C).offset();v={left:C.scrollLeft,top:C.scrollTop}}h=w(0);i=x(0)}function G(b){o.css({left:w(a.x1)+'px',top:x(a.y1)+'px',width:Math.max(a.width-c.borderWidth*2,0)+'px',height:Math.max(a.height-c.borderWidth*2,0)+'px'});Y.css({left:h+'px',top:i+'px',width:a.x1+'px',height:q+'px'});Z.css({left:h+a.x1+'px',top:i+'px',width:a.width+'px',height:a.y1+'px'});ba.css({left:h+a.x2+'px',top:i+'px',width:r-a.x2+'px',height:q+'px'});bb.css({left:h+a.x1+'px',top:i+a.y2+'px',width:a.width+'px',height:q-a.y2+'px'});if(b!==false){if(jQuery.imgAreaSelect.keyPress!=bf)jQuery(document).unbind(jQuery.imgAreaSelect.keyPress,jQuery.imgAreaSelect.onKeyPress);if(c.keys)jQuery(document).bind(jQuery.imgAreaSelect.keyPress,jQuery.imgAreaSelect.onKeyPress=bf)}}function A(b){if(!Q){F();Q=true;o.one('mouseout',function(){Q=false})}U=y(D(b))-a.x1;V=z(E(b))-a.y1;k=[];if(c.resizable){if(V<=K)k[L]='n';else if(V>=a.height-K)k[L]='s';if(U<=K)k[M]='w';else if(U>=a.width-K)k[M]='e'}t.css('cursor',k.length?k.join('')+'-resize':c.movable?'move':'')}function bg(b){if(b.which!=1)return false;F();if(c.resizable&&k.length>0){jQuery('body').css('cursor',k.join('')+'-resize');f=w(k[M]=='w'?a.x2:a.x1);g=x(k[L]=='n'?a.y2:a.y1);jQuery(document).mousemove(N);t.unbind('mousemove',A);jQuery(document).one('mouseup',function(){k=[];jQuery('body').css('cursor','');if(c.autoHide)o.add(p).hide();c.onSelectEnd(l,a);jQuery(document).unbind('mousemove',N);t.mousemove(A)})}else if(c.movable){bd=a.x1+h;be=a.y1+i;R=D(b);S=E(b);jQuery(document).mousemove(bh).one('mouseup',function(){c.onSelectEnd(l,a);jQuery(document).unbind('mousemove',bh)})}else jQuery(l).mousedown(b);return false}function W(){d=Math.max(h,Math.min(h+r,f+Math.abs(e-g)*n*(d>=f?1:-1)));e=Math.round(Math.max(i,Math.min(i+q,g+Math.abs(d-f)/n*(e>=g?1:-1))));d=Math.round(d)}function X(){e=Math.max(i,Math.min(i+q,g+Math.abs(d-f)/n*(e>=g?1:-1)));d=Math.round(Math.max(h,Math.min(h+r,f+Math.abs(e-g)*n*(d>=f?1:-1))));e=Math.round(e)}function bi(b,j){d=b;e=j;if(c.minWidth&&Math.abs(d-f)<c.minWidth){d=f-c.minWidth*(d<f?1:-1);if(d<h)f=h+c.minWidth;else if(d>h+r)f=h+r-c.minWidth}if(c.minHeight&&Math.abs(e-g)<c.minHeight){e=g-c.minHeight*(e<g?1:-1);if(e<i)g=i+c.minHeight;else if(e>i+q)g=i+q-c.minHeight}d=Math.max(h,Math.min(d,h+r));e=Math.max(i,Math.min(e,i+q));if(n)if(Math.abs(d-f)/n>Math.abs(e-g))X();else W();if(c.maxWidth&&Math.abs(d-f)>c.maxWidth){d=f-c.maxWidth*(d<f?1:-1);if(n)X()}if(c.maxHeight&&Math.abs(e-g)>c.maxHeight){e=g-c.maxHeight*(e<g?1:-1);if(n)W()}a.x1=y(Math.min(f,d));a.x2=y(Math.max(f,d));a.y1=z(Math.min(g,e));a.y2=z(Math.max(g,e));a.width=Math.abs(d-f);a.height=Math.abs(e-g);G();c.onSelectChange(l,a)}function N(b){d=!k.length||k[M]||n?D(b):w(a.x2);e=!k.length||k[L]||n?E(b):x(a.y2);bi(d,e);return false}function H(b,j){d=(f=b)+a.width;e=(g=j)+a.height;a.x1=y(f);a.y1=z(g);a.x2=y(d);a.y2=z(e);G();c.onSelectChange(l,a)}function bh(b){var j=Math.max(h,Math.min(bd+D(b)-R,h+r-a.width));var m=Math.max(i,Math.min(be+E(b)-S,i+q-a.height));H(j,m);b.preventDefault();return false}function bj(b){F();a.x1=a.x2=y(R=f=d=D(b));a.y1=a.y2=z(S=g=e=E(b));a.width=0;a.height=0;k=[];G();o.add(p).show();jQuery(document).unbind('mouseup',bk).mousemove(N);t.unbind('mousemove',A);c.onSelectStart(l,a);jQuery(document).one('mouseup',function(){if(c.autoHide||(a.width*a.height==0))o.add(p).hide();c.onSelectEnd(l,a);jQuery(document).unbind('mousemove',N);t.mousemove(A)})}function bk(){jQuery(document).unbind('mousemove',bj);o.add(p).hide();a.x1=a.y1=a.x2=a.y2=a.width=a.height=0;c.onSelectChange(l,a);c.onSelectEnd(l,a)}function bl(b){if(b.which!=1)return false;jQuery(document).one('mousemove',bj).one('mouseup',bk);return false}function bm(){F();G(false);f=w(a.x1);g=x(a.y1);d=w(a.x2);e=x(a.y2)}var bf=function(b){var j=c.keys,m=10,O,bn=b.keyCode||b.which;if(!isNaN(j.arrows))m=j.arrows;if(!isNaN(j.shift)&&b.shiftKey)m=j.shift;if(!isNaN(j.ctrl)&&b.ctrlKey)m=j.ctrl;if(!isNaN(j.alt)&&(b.altKey||b.originalEvent.altKey))m=j.alt;if(j.arrows=='resize'||(j.shift=='resize'&&b.shiftKey)||(j.ctrl=='resize'&&b.ctrlKey)||(j.alt=='resize'&&(b.altKey||b.originalEvent.altKey))){switch(bn){case 37:m=-m;case 39:O=Math.max(f,d);f=Math.min(f,d);d=Math.max(O+m,f);if(n)X();break;case 38:m=-m;case 40:O=Math.max(g,e);g=Math.min(g,e);e=Math.max(O+m,g);if(n)W();break;default:return}bi(d,e)}else{f=Math.min(f,d);g=Math.min(g,e);switch(bn){case 37:H(Math.max(f-m,h),g);break;case 38:H(f,Math.max(g-m,i));break;case 39:H(f+Math.min(m,r-y(d)),g);break;case 40:H(f,g+Math.min(m,q-z(e)));break;default:return}}return false};this.setOptions=function(b){c=jQuery.extend(c,b);if(b.x1!=null){a.x1=b.x1;a.y1=b.y1;a.x2=b.x2;a.y2=b.y2;b.show=true}if(b.keys)c.keys=jQuery.extend({shift:1,ctrl:'resize'},b.keys===true?{}:b.keys);C=jQuery(c.parent).get(0);F();s=jQuery(l);while(s.length&&!s.is('body')){if(!isNaN(s.css('z-index'))&&s.css('z-index')>J)J=s.css('z-index');if(s.css('position')=='fixed')bc=true;s=s.parent()}f=w(a.x1);g=x(a.y1);d=w(a.x2);e=x(a.y2);a.width=d-f;a.height=e-g;G();if(b.hide)o.add(p).hide();else if(b.show)o.add(p).show();p.addClass(c.classPrefix+'-outer');I.addClass(c.classPrefix+'-selection');P.addClass(c.classPrefix+'-border1');t.addClass(c.classPrefix+'-border2');o.css({borderWidth:c.borderWidth+'px'});I.css({backgroundColor:c.selectionColor,opacity:c.selectionOpacity});P.css({borderStyle:'solid',borderColor:c.borderColor1});t.css({borderStyle:'dashed',borderColor:c.borderColor2});p.css({opacity:c.outerOpacity,backgroundColor:c.outerColor});n=c.aspectRatio&&(T=c.aspectRatio.split(/:/))?T[0]/T[1]:null;if(c.disable||c.enable===false){o.unbind('mousemove',A).unbind('mousedown',bg);jQuery(l).add(p).unbind('mousedown',bl);jQuery(window).unbind('resize',bm)}else if(c.enable||c.disable===false){if(c.resizable||c.movable)o.mousemove(A).mousedown(bg);if(!c.persistent)jQuery(l).add(p).mousedown(bl);jQuery(window).resize(bm)}jQuery(c.parent).append(p.add(o));c.enable=c.disable=undefined};if(jQuery.browser.msie)jQuery(l).attr('unselectable','on');jQuery.imgAreaSelect.keyPress=jQuery.browser.msie||jQuery.browser.safari?'keydown':'keypress';o.add(p).css({display:'none',position:bc?'fixed':'absolute',overflow:'hidden',zIndex:J>0?J:'0'});I.css({borderStyle:'solid'});initOptions={borderColor1:'#000',borderColor2:'#fff',borderWidth:1,classPrefix:'imgareaselect',movable:true,resizable:true,selectionColor:'#fff',selectionOpacity:0.2,outerColor:'#000',outerOpacity:0.2,parent:'body',onSelectStart:function(){},onSelectChange:function(){},onSelectEnd:function(){}};c=jQuery.extend(initOptions,c);this.setOptions(c)};jQuery.fn.imgAreaSelect=function(b){b=b||{};this.each(function(){if(jQuery(this).data('imgAreaSelect'))jQuery(this).data('imgAreaSelect').setOptions(b);else{if(b.enable===undefined&&b.disable===undefined)b.enable=true;jQuery(this).data('imgAreaSelect',new jQuery.imgAreaSelect.init(this,b))}});return this};
