!function(){function a(a){var b;return a=a||0,b=Math.floor(a/60),{days:a>=m.DAYINSECONDS?Math.floor(a/m.DAYINSECONDS):0,hours:a>=3600?Math.floor(a%m.DAYINSECONDS/3600):0,totalMinutes:b,minutes:a>=60?Math.floor(a%3600/60):b,seconds:a%60,totalSeconds:a}}function b(a){return((a=parseInt(a,10))<10&&"0")+a}function c(){return{seconds:0,editable:!1,duration:null,callback:function(){console.log("Time up!")},repeat:!1,countdown:!1,format:null,updateFrequency:500}}function d(){return Math.round((Date.now?Date.now():(new Date).getTime())/1e3)}function e(c){var d=a(c);if(d.days)return d.days+":"+b(d.hours)+":"+b(d.minutes)+":"+b(d.seconds);if(d.hours)return d.hours+":"+b(d.minutes)+":"+b(d.seconds);return d.minutes?d.minutes+":"+b(d.seconds)+" min":d.seconds+" sec"}function f(c,d){for(var e=a(c),f=[{identifier:"%d",value:e.days},{identifier:"%h",value:e.hours},{identifier:"%m",value:e.minutes},{identifier:"%s",value:e.seconds},{identifier:"%g",value:e.totalMinutes},{identifier:"%t",value:e.totalSeconds},{identifier:"%D",value:b(e.days)},{identifier:"%H",value:b(e.hours)},{identifier:"%M",value:b(e.minutes)},{identifier:"%S",value:b(e.seconds)},{identifier:"%G",value:b(e.totalMinutes)},{identifier:"%T",value:b(e.totalSeconds)}],g=0;g<f.length;g++)d=d.replace(f[g].identifier,f[g].value);return d}function g(a){if(!isNaN(Number(a)))return a;a=a.toLowerCase();var b=a.match(/\d+d/),c=a.match(/\d+h/),d=a.match(/\d+m/),e=a.match(/\d+s/);if(!(b||c||d||e))throw new Error("Invalid string passed in durationTimeToSeconds!");var f=0;return b&&(f+=Number(b[0].replace("d",""))*m.DAYINSECONDS),c&&(f+=3600*Number(c[0].replace("h",""))),d&&(f+=60*Number(d[0].replace("m",""))),e&&(f+=Number(e[0].replace("s",""))),f}function h(a){var b,c;return a.indexOf("sec")>0?c=Number(a.replace(/\ssec/g,"")):a.indexOf("min")>0?(a=a.replace(/\smin/g,""),b=a.split(":"),c=Number(60*b[0])+Number(b[1])):a.match(/\d{1,2}:\d{2}:\d{2}:\d{2}/)?(b=a.split(":"),c=Number(b[0]*m.DAYINSECONDS)+Number(3600*b[1])+Number(60*b[2])+Number(b[3])):a.match(/\d{1,2}:\d{2}:\d{2}/)&&(b=a.split(":"),c=Number(3600*b[0])+Number(60*b[1])+Number(b[2])),c}function i(a,b){a.state=b,$(a.element).data("state",b)}function j(a){$(a.element).on("focus",function(){a.pause()}),$(a.element).on("blur",function(){a.totalSeconds=h($(a.element)[a.html]()),a.resume()})}function k(a){if(a.totalSeconds=d()-a.startTime,a.config.countdown)return a.totalSeconds=a.config.duration-a.totalSeconds,0===a.totalSeconds&&(clearInterval(a.intervalId),i(a,m.TIMER_STOPPED),a.config.callback(),$(a.element).data("seconds")),void a.render();a.render(),a.config.duration&&a.totalSeconds>0&&a.totalSeconds%a.config.duration==0&&(a.config.callback&&a.config.callback(),a.config.repeat||(clearInterval(a.intervalId),i(a,m.TIMER_STOPPED),a.config.duration=null))}function l(a,b){if(this.element=a,this.originalConfig=$.extend({},b),this.totalSeconds=0,this.intervalId=null,this.html="html","INPUT"!==a.tagName&&"TEXTAREA"!==a.tagName||(this.html="val"),this.config=n.getDefaultConfig(),b.duration&&(b.duration=n.durationTimeToSeconds(b.duration)),"string"!=typeof b&&(this.config=$.extend(this.config,b)),this.config.seconds&&(this.totalSeconds=this.config.seconds),this.config.editable&&n.makeEditable(this),this.startTime=n.unixSeconds()-this.totalSeconds,this.config.duration&&this.config.repeat&&this.config.updateFrequency<1e3&&(this.config.updateFrequency=1e3),this.config.countdown){if(!this.config.duration)throw new Error("Countdown option set without duration!");if(this.config.editable)throw new Error("Cannot set editable on a countdown timer!");this.config.startTime=n.unixSeconds()-this.config.duration,this.totalSeconds=this.config.duration}}var m={PLUGIN_NAME:"timer",TIMER_RUNNING:"running",TIMER_PAUSED:"paused",DAYINSECONDS:86400},n={getDefaultConfig:c,unixSeconds:d,secondsToPrettyTime:e,secondsToFormattedTime:f,durationTimeToSeconds:g,prettyTimeToSeconds:h,setState:i,makeEditable:j,intervalHandler:k};l.prototype.start=function(){this.state!==m.TIMER_RUNNING&&(n.setState(this,m.TIMER_RUNNING),this.render(),this.intervalId=setInterval(n.intervalHandler.bind(null,this),this.config.updateFrequency))},l.prototype.pause=function(){this.state===m.TIMER_RUNNING&&(n.setState(this,m.TIMER_PAUSED),clearInterval(this.intervalId))},l.prototype.resume=function(){this.state===m.TIMER_PAUSED&&(n.setState(this,m.TIMER_RUNNING),this.config.countdown?this.startTime=n.unixSeconds()-this.config.duration+this.totalSeconds:this.startTime=n.unixSeconds()-this.totalSeconds,this.intervalId=setInterval(n.intervalHandler.bind(null,this),this.config.updateFrequency))},l.prototype.remove=function(){clearInterval(this.intervalId),$(this.element).data(m.PLUGIN_NAME,null),$(this.element).data("seconds",null)},l.prototype.reset=function(){var a=this.originalConfig;this.remove(),$(this.element).timer(a)},l.prototype.render=function(){this.config.format?$(this.element)[this.html](n.secondsToFormattedTime(this.totalSeconds,this.config.format)):$(this.element)[this.html](n.secondsToPrettyTime(this.totalSeconds)),$(this.element).data("seconds",this.totalSeconds)},$.fn.timer=function(a){return a=a||"start",this.each(function(){$.data(this,m.PLUGIN_NAME)instanceof l||$.data(this,m.PLUGIN_NAME,new l(this,a));var b=$.data(this,m.PLUGIN_NAME);"string"==typeof a?"function"==typeof b[a]&&b[a]():b.start()})}}();