/**
 * This file contains jQuery plugins that are useful accross any site.
 * They should load right after jQuery
 *
 * @author: PC Thomatos
 **/

(function($, global){
/**
 * jQuery.setNS creates Namespaces for js to simplify code.
 * @author: PC Thomatos
 * @usage:
 *
 * several implimentations will work
 *   1) Least code tricky to read:
 * 		jQuery.setNS('com.foo.bar.barFoo').newMethod = function(){};
 *
 * 	 2) More explicit:
 * 	 	var barFoo = SetNS('com.foo.bar.barFoo');
 * 	 	barFoo.newMethod = function(){};
 *
 *	 3) Also explicit and you can easily override or add to any part of the tree:
 *		A) Explicit add newMethod to  barFoo
 *		   jQuery.setNS('com.foo.bar.barFoo');
 *		   com.foo.bar.barFoo.newMethod = function(){};
 *
 *		B) override foo:
 *		   jQuery.setNS('com.foo.bar.barFoo');
 *		   com.foo = function(){}; // everthing that foo was referencing is no longer referenced (bar and below);
 *
 *	   	C) add another property under foo:
 *	   	   jQuery.setNS('com.foo.bar.barFoo');
 *		   com.foo.test = function(){}; // both com.foo.bar and com.foo.test will exist;
 *
 **/
  $.setNS = function(namespaceStr){
	"use strict"; // for browsers that support this only strict within this scope wont break anything else.

  	var splitNS = namespaceStr.split('.'),
  		currNSObj = global,
  		splitNSLength,
  		i;

		for(i = 0, splitNSLength = splitNS.length; i < splitNSLength; i++)
			currNSObj = (currNSObj[splitNS[i]] = currNSObj[splitNS[i]] || {});

	return currNSObj;
  };

/**
 * jQuery.safeDocumentReady wraps jQuery's document ready but fails more gracefully if a function fails to execute.
 * @author: PC Thomatos
 * @usage:
 *
 *	1) Adding to the $(document).ready() Callback:
 *   		Note: wrapping in an anonymous function helps in debugging but you don't need to do it.
 *	   	A) A is better than B for debugging
 * 			jQuery.safeDocumentReady.add(function(){
 *				foobar();
 *			});
 *
 *		B) A is better than B for debugging
 * 			jQuery.safeDocumentReady.add(foobar);
 *
 *	2) Outputting functions that fired:
 *		Use firebug and click on the individual functions they will point to the function in the script tab.
 *		console.log(jQuery.safeDocumentReady.succeeded);
 *
 *	3) Outputting functions that failed to fire:
 *		Use firebug and click on the individual functions they will point to the function in the script tab.
 *		console.log(jQuery.safeDocumentReady.failed);
 *
 **/
  $.safeDocumentReady = (function(){
	"use strict"; // for browsers that support this only strict within this scope wont break anything else.

  	var _readyCallbackSet = false,
  		_callBacksArr = [],
  		_firedArr = [],
  		_failedArr = [],
  		_callBacksLength = 0,
  		_currFn,

  		_runInitFuncs = function(){
  			do{
				try{
					while (_callBacksArr.length > 0){
						_currFn = _callBacksArr.shift();
						_currFn.apply(document, []);
						_firedArr[_firedArr.length] = _currFn;
					}
				}catch(e){
					if(typeof console != 'undefined') console.log('$.safeDocumentReady has failed to fire a function: ' + e);
					_failedArr[_failedArr.length] = _currFn;
				};
	  		} while(_callBacksArr.length > 0);
		},

		_add = function(functionRef){
			// if _readyCallbackSet is false, Set the dom Ready callback, Set _readyCallbackSet to true
			_readyCallbackSet == false && jQuery(document).ready(_runInitFuncs) && (_readyCallbackSet = true);

			if (typeof functionRef === "function" ) {
				_callBacksArr[_callBacksLength] = functionRef;
				_callBacksLength++;
			}else{
				_failedArr[_failedArr.length] = functionRef;
			}
		}
		;// end var statement

	return 	{
				'add' : _add,
				'succeeded' : _firedArr,
				'failed' : _failedArr
			};
  })();

})(jQuery, this || (1,eval)('this'));
