var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();
/* 
liveWithDelay jQuery plugin
Author: Brian Grinstead
MIT license: http://www.opensource.org/licenses/mit-license.php

http://github.com/bgrins/liveWithDelay
http://briangrinstead.com/files/liveWithDelay

Usage: 
	See http://api.jquery.com/live/
	.liveWithDelay( eventType, [ eventData ], handler(eventObject), timeout, throttle )

Examples:
	$("#foo").liveWithDelay("click", function(e) { }, 100);
	$(window).liveWithDelay("resize", { optional: "eventData" }, callback, 1000);
	$(window).liveWithDelay("resize", callback, 1000, true);
*/

(function($) {
$.fn.liveWithDelay = function( type, data, fn, timeout, throttle ) {
	
	if ( $.isFunction( data ) ) {
		throttle = timeout;
		timeout = fn;
		fn = data;
		data = undefined;
	}

	// Allow delayed function to be removed with fn in unlive function
	fn.guid = fn.guid || ($.guid && $.guid++);
	
	// live each separately so that each element has its own delay
	return this.each(function() {
        
        var wait = null;
        
        function cb() {
            var e = $.extend(true, { }, arguments[0]);
            var ctx = this;
            var throttler = function() {
            	wait = null;
            	fn.apply(ctx, [e]);
            };
            
            if (!throttle) { clearTimeout(wait); wait = null; }
            if (!wait) { wait = setTimeout(throttler, timeout); }
        }
        
        cb.guid = fn.guid;
        
        $(this).live(type, data, cb);
	});
	
	
}
})(jQuery);

