(function($) {
	BBCS.namespace('lock');
	/**
	* A function wrapper to prevent the UI from submitting an Ajax request multiple times 
	* in parallel.  MSGI.lock() fires the callback it is passed, preventing it from firing 
	* again. It then invokes the callback, passing it an "unlock" function it can use to 
	* remove the lock at the appropriate time.
	* @param {function} callback function to be protected (takes an "unlock" function as its argument)
	*/
	BBCS.lock = (function() {
		// array of string representations of functions that are currently locked
		var lockedFns = [];
		
		return function(callback) {
			// convert callback to string to support locking anonymous functions
			var fn = callback.toString();
	
			// define some helper functions, bound to the callback
			function index() { return $.inArray(fn, lockedFns); }
			function locked() { return index() > -1; }
			function lock() { lockedFns.push(fn); }
			function unlock() { lockedFns.splice(index(), 1); }
			
			// if not already locked
			if (!locked()) {
				// lock and fire the callback, passing it a reference to its own unlock function
				lock();
				callback(unlock);
			}
		};
	})();
})(jQuery);

