var Timer=function(a,b){return this._init(a,b)};Timer.prototype={VERSION:1,_init:function(a,b){this._interval=1e3;this._timer=null;this._cbs=[];this._multipliers=[];this._tickCounts=[];this._canRun=[];this._stoppedThreads=0;this._runOnce=false;this._startedAt=-1;this._pausedAt=-1;if(typeof a=="number")this._interval=a;this.addCallback(b);return this},_preset:function(){this._stoppedThreads=0;this._startedAt=-1;this._pausedAt=-1;for(var a=0;a<this._cbs.length;a++){this._canRun[a]=true;this._tickCounts[a]=0}},_ticks:function(a){var b=this;for(var c=0;c<this._cbs.length;c++){if(typeof this._cbs[c]=="function"&&this._canRun[c]){this._tickCounts[c]++;if(this._tickCounts[c]==this._multipliers[c]){this._tickCounts[c]=0;if(this.runOnce()){this._canRun[c]=false;this._stoppedThreads++}window.setTimeout(b._cbs[c],0)}}}if(this.runOnce()&&this._stoppedThreads==this._cbs.length)this.stop();if(typeof a=="number"){this.stop().start(null,true)}},runOnce:function(a){if(typeof a=="undefined")return this._runOnce;else if(typeof a=="boolean")this._runOnce=a;else alert("Invalid argument for runOnce(...).\n\nUsage: runOnce(true | false) /*Default value: false*/\nor, runOnce() to get status");return this},interval:function(a){if(typeof a=="undefined")return this._interval;else if(typeof a=="number")this._interval=Math.floor(a);return this},stop:function(a){if(this._timer){if(!a)this._pausedAt=-1;try{window.clearInterval(this._timer)}catch(b){}this._timer=null}return this},isStopped:function(){return this._timer==null&&!this.isPaused()},start:function(a,b){if(this.isPaused())return this.resume();if(!this.isStopped())return this;if(!b)this._preset();var c=this._interval;if(typeof a=="number")c=a;var d=this;this._timer=window.setInterval(function(){d._ticks(a)},c);this._startedAt=(new Date).getTime();this._startedAt-=this._interval-c;return this},pause:function(){if(this._timer){this._pausedAt=(new Date).getTime();this.stop(true)}return this},isPaused:function(){return this._pausedAt>=0},resume:function(){if(this.isPaused()){var a=this._interval-(this._pausedAt-this._startedAt)%this._interval;this._pausedAt=-1;this.start(a,true)}return this},restart:function(){return this.stop().start()},addCallback:function(a,b){if(typeof a=="function"){this._cbs.push(a);if(typeof b=="number"){b=Math.floor(b);this._multipliers.push(b<1?1:b)}else this._multipliers.push(1);this._tickCounts.push(0);this._canRun.push(true)}return this},clearCallbacks:function(){this._cbs.length=0;this._multipliers.length=0;this._canRun.length=0;this._tickCounts.length=0;this._stoppedThreads=0;return this}}
