﻿
(function($){


    $.fn.icattScheduler = {

        seed : 0,

	    schedule: function(method,milli,type){
		    ///	<summary>
		    ///	
		    ///	</summary>
		    ///	<returns type="undefined" />
    		 
		    if (method) {
		        milli = milli || 1000;
    		    
		        type = type || 'default';

			    this.callRegistry[type] = this.callRegistry[type] || [];
    			
    			// guarantees a unique key without creating a unnessesarily large array
    			this.seed++
    			if (this.seed >= 1000000000) this.seed = 0;
    			
    			var key = this.seed.toString();
    			
			    var id = setTimeout(function(){
	        	        
	        	        //Remove reference befor executing method
	                    $.fn.icattScheduler.callRegistry[type][key] = null;
            	        
            	        //Execute
	                    method.apply();

			    }, milli);
			    
			    this.callRegistry[type][key]= id;
			    
			    return key;
    		
		    }

	    },
    		
	    callRegistry:[],
	    
    	
	    cancel: function(type,key){
		    ///	<summary>
		    ///		Cancels call
		    ///	</summary>
		    ///	<returns type="undefined" />
    		type = type || 'default';
    		if (key) {
    		    var id = this.callRegistry[type][key];
		        clearTimeout(id);
		    } else {
		        for (key in this.callRegistry[type]){
		            var id = this.callRegistry[type][key];
		            clearTimeout(id);
		        }
		    }
	    }
    };

        
})(jQuery);



