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