﻿jQuery.fn.extend({everyTime:function(c,d,b,e,a){return this.each(function(){jQuery.timer.add(this,c,d,b,e,a)})},oneTime:function(b,c,a){return this.each(function(){jQuery.timer.add(this,b,c,a,1)})},stopTime:function(b,a){return this.each(function(){jQuery.timer.remove(this,b,a)})}});jQuery.extend({timer:{global:[],guid:1,dataKey:"jQuery.timer",regex:/^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,powers:{ms:1,cs:10,ds:100,s:1000,das:10000,hs:100000,ks:1000000},timeParse:function(d){if(d==undefined||d==null){return null}var c=this.regex.exec(jQuery.trim(d.toString()));if(c[2]){var b=parseFloat(c[1]);var a=this.powers[c[2]]||1;return b*a}else{return d}},add:function(c,f,g,d,i,a){var b=0;if(jQuery.isFunction(g)){if(!i){i=d}d=g;g=f}f=jQuery.timer.timeParse(f);if(typeof f!="number"||isNaN(f)||f<=0){return}if(i&&i.constructor!=Number){a=!!i;i=0}i=i||0;a=a||false;var h=jQuery.data(c,this.dataKey)||jQuery.data(c,this.dataKey,{});if(!h[g]){h[g]={}}d.timerID=d.timerID||this.guid++;var e=function(){if(a&&this.inProgress){return}this.inProgress=true;if((++b>i&&i!==0)||d.call(c,b)===false){jQuery.timer.remove(c,g,d)}this.inProgress=false};e.timerID=d.timerID;if(!h[g][d.timerID]){h[g][d.timerID]=window.setInterval(e,f)}this.global.push(c)},remove:function(a,c,b){var e=jQuery.data(a,this.dataKey),d;if(e){if(!c){for(c in e){this.remove(a,c,b)}}else{if(e[c]){if(b){if(b.timerID){window.clearInterval(e[c][b.timerID]);delete e[c][b.timerID]}}else{for(var b in e[c]){window.clearInterval(e[c][b]);delete e[c][b]}}for(d in e[c]){break}if(!d){d=null;delete e[c]}}}for(d in e){break}if(!d){jQuery.removeData(a,this.dataKey)}}}}});jQuery(window).bind("unload",function(){jQuery.each(jQuery.timer.global,function(a,b){jQuery.timer.remove(b)})});
