(function(a){function b(l){var q={first:{x:-1,y:-1},second:{x:-1,y:-1},show:false,active:false};var n={};function e(s){if(q.active){l.getPlaceholder().trigger("plotselecting",[g()]);m(s)}}function o(s){if(s.which!=1){return}document.body.focus();if(document.onselectstart!==undefined&&n.onselectstart==null){n.onselectstart=document.onselectstart;document.onselectstart=function(){return false}}if(document.ondrag!==undefined&&n.ondrag==null){n.ondrag=document.ondrag;document.ondrag=function(){return false}}d(q.first,s);q.active=true;a(document).one("mouseup",k)}function k(s){if(document.onselectstart!==undefined){document.onselectstart=n.onselectstart}if(document.ondrag!==undefined){document.ondrag=n.ondrag}q.active=false;m(s);if(f()){j()}else{l.getPlaceholder().trigger("plotunselected",[]);l.getPlaceholder().trigger("plotselecting",[null])}return false}function g(){if(!f()){return null}var u={},t=q.first,s=q.second;a.each(l.getAxes(),function(v,w){if(w.used){var y=w.c2p(t[w.direction]),x=w.c2p(s[w.direction]);u[v]={from:Math.min(y,x),to:Math.max(y,x)}}});return u}function j(){var s=g();l.getPlaceholder().trigger("plotselected",[s]);if(s.xaxis&&s.yaxis){l.getPlaceholder().trigger("selected",[{x1:s.xaxis.from,y1:s.yaxis.from,x2:s.xaxis.to,y2:s.yaxis.to}])}}function h(t,u,s){return u<t?t:(u>s?s:u)}function d(w,t){var v=l.getOptions();var u=l.getPlaceholder().offset();var s=l.getPlotOffset();w.x=h(0,t.pageX-u.left-s.left,l.width());w.y=h(0,t.pageY-u.top-s.top,l.height());if(v.selection.mode=="y"){w.x=w==q.first?0:l.width()}if(v.selection.mode=="x"){w.y=w==q.first?0:l.height()}}function m(s){if(s.pageX==null){return}d(q.second,s);if(f()){q.show=true;l.triggerRedrawOverlay()}else{r(true)}}function r(s){if(q.show){q.show=false;l.triggerRedrawOverlay();if(!s){l.getPlaceholder().trigger("plotunselected",[])}}}function c(s,z){var v,y,x,w,u;w=l.getUsedAxes();for(i=0;i<w.length;++i){v=w[i];if(v.direction==z){u=z+v.n+"axis";if(!s[u]&&v.n==1){u=z+"axis"}if(s[u]){y=s[u].from;x=s[u].to;break}}}if(!s[u]){v=z=="x"?l.getXAxes()[0]:l.getYAxes()[0];y=s[z+"1"];x=s[z+"2"]}if(y!=null&&x!=null&&y>x){var t=y;y=x;x=t}return{from:y,to:x,axis:v}}function p(t,s){var v,u,w=l.getOptions();if(w.selection.mode=="y"){q.first.x=0;q.second.x=l.width()}else{u=c(t,"x");q.first.x=u.axis.p2c(u.from);q.second.x=u.axis.p2c(u.to)}if(w.selection.mode=="x"){q.first.y=0;q.second.y=l.height()}else{u=c(t,"y");q.first.y=u.axis.p2c(u.from);q.second.y=u.axis.p2c(u.to)}q.show=true;l.triggerRedrawOverlay();if(!s&&f()){j()}}function f(){var s=5;return Math.abs(q.second.x-q.first.x)>=s&&Math.abs(q.second.y-q.first.y)>=s}l.clearSelection=r;l.setSelection=p;l.getSelection=g;l.hooks.bindEvents.push(function(t,s){var u=t.getOptions();if(u.selection.mode!=null){s.mousemove(e)}if(u.selection.mode!=null){s.mousedown(o)}});l.hooks.drawOverlay.push(function(v,D){if(q.show&&f()){var t=v.getPlotOffset();var s=v.getOptions();D.save();D.translate(t.left,t.top);var z=a.color.parse(s.selection.color);D.strokeStyle=z.scale("a",0.8).toString();D.lineWidth=1;D.lineJoin="round";D.fillStyle=z.scale("a",0.4).toString();var B=Math.min(q.first.x,q.second.x),A=Math.min(q.first.y,q.second.y),C=Math.abs(q.second.x-q.first.x),u=Math.abs(q.second.y-q.first.y);D.fillRect(B,A,C,u);D.strokeRect(B,A,C,u);D.restore()}})}a.plot.plugins.push({init:b,options:{selection:{mode:null,color:"#e8cfac"}},name:"selection",version:"1.0"})})(jQuery);
