/*! grafana - v1.5.4 - 2014-05-13
 * Copyright (c) 2014 Torkel Ödegaard; Licensed Apache License */

define("panels/timepicker/module",["angular","app","underscore","moment","kbn"],function(a,b,c,d,e){var f=a.module("kibana.panels.timepicker",[]);b.useModule(f),f.controller("timepicker",["$scope","$modal","$q","filterSrv",function(a,b,f,g){a.panelMeta={status:"Stable",description:"A panel for controlling the time range filters. If you have time based data,  or if you're using time stamped indices, you need one of these"};var h={status:"Stable",time_options:["5m","15m","1h","6h","12h","24h","2d","7d","30d"],refresh_intervals:["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"]},i=null;c.defaults(a.panel,h),a.filterSrv=g,a.patterns={date:/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/,hour:/^([01]?[0-9]|2[0-3])$/,minute:/^[0-5][0-9]$/,second:/^[0-5][0-9]$/,millisecond:/^[0-9]*$/},a.$on("refresh",function(){a.init()}),a.init=function(){var b=g.timeRange();b&&(a.panel.now="now"===g.timeRange(!1).to?!0:!1,a.time=l(b.from,b.to))},a.customTime=function(){i||(i=b({template:"./app/panels/timepicker/custom.html",persist:!0,show:!1,scope:a,keyboard:!1})),a.input.$setValidity("dummy",!0),a.temptime=k(a.time),a.temptime.from.date.setHours(1,0,0,0),a.temptime.to.date.setHours(1,0,0,0),f.when(i).then(function(a){a.modal("show")})},a.validate=function(b){a.input.$setValidity("dummy",!0);var c=n(b.from.date),d=n(b.to.date),e=b;return a.input.$valid?(c.setHours(e.from.hour,e.from.minute,e.from.second,e.from.millisecond),d.setHours(e.to.hour,e.to.minute,e.to.second,e.to.millisecond),isNaN(c.getTime())||isNaN(d.getTime())||c.getTime()>=d.getTime()?(a.input.$setValidity("dummy",!1),!1):{from:c,to:d}):!1},a.setNow=function(){a.time.to=m(new Date)},a.setAbsoluteTimeFilter=function(b){var d=c.clone(b);a.panel.now&&(d.to="now"),a.panel.filter_id=g.setTime(d),a.time=l(b.from,b.to)},a.setRelativeFilter=function(b){a.panel.now=!0;var c={from:"now-"+b,to:"now"};g.setTime(c),a.time=l(e.parseDate(c.from),new Date)};var j=function(a,b,c){return c=c||"0",a+="",a.length>=b?a:new Array(b-a.length+1).join(c)+a},k=function(a){var b={from:c.clone(a.from),to:c.clone(a.to)};return b.from.date=new Date(b.from.date),b.to.date=new Date(b.to.date),b},l=function(a,b){return{from:m(a),to:m(b)}},m=function(a){return{date:new Date(a),hour:j(a.getHours(),2),minute:j(a.getMinutes(),2),second:j(a.getSeconds(),2),millisecond:j(a.getMilliseconds(),3)}},n=function(a){return a=d(a).clone().toDate(),d(new Date(a.getTime()+6e4*a.getTimezoneOffset())).toDate()}}])});