/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 *
 **/

jQuery.fn.extend({everyTime:function(d,a,c,b){return this.each(function(){jQuery.timer.add(this,d,a,c,b)})},oneTime:function(d,a,c){return this.each(function(){jQuery.timer.add(this,d,a,c,1)})},stopTime:function(d,a){return this.each(function(){jQuery.timer.remove(this,d,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 a=this.regex.exec(jQuery.trim(d.toString()));if(a[2]){var c=parseFloat(a[1]);var b=this.powers[a[2]]||1;return c*b}else{return d}},add:function(d,a,c,b,e){var g=0;if(jQuery.isFunction(c)){if(!e)e=b;b=c;c=a}a=jQuery.timer.timeParse(a);if(typeof a!='number'||isNaN(a)||a<0)return;if(typeof e!='number'||isNaN(e)||e<0)e=0;e=e||0;var f=jQuery.data(d,this.dataKey)||jQuery.data(d,this.dataKey,{});if(!f[c])f[c]={};b.timerID=b.timerID||this.guid++;var h=function(){if((++g>e&&e!==0)||b.call(d,g)===false)jQuery.timer.remove(d,c,b)};h.timerID=b.timerID;if(!f[c][b.timerID])f[c][b.timerID]=window.setInterval(h,a);this.global.push(d)},remove:function(d,a,c){var b=jQuery.data(d,this.dataKey),e;if(b){if(!a){for(a in b)this.remove(d,a,c)}else if(b[a]){if(c){if(c.timerID){window.clearInterval(b[a][c.timerID]);delete b[a][c.timerID]}}else{for(var c in b[a]){window.clearInterval(b[a][c]);delete b[a][c]}}for(e in b[a])break;if(!e){e=null;delete b[a]}}for(e in b)break;if(!e)jQuery.removeData(d,this.dataKey)}}}});jQuery(window).bind("unload",function(){jQuery.each(jQuery.timer.global,function(d,a){jQuery.timer.remove(a)})});
