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