/**
 * @author alexlindsay
 * Some inspiration drawn form http://metamalcolm.wordpress.com/2009/08/06/firebug-console-is-undefined/
 * Assumes presence of $.cookie jQuery plugin
 */

(function($) {
	$.log = {};
	if (null == $.cookie('loggingIsEnabled')) {
		$.cookie('loggingIsEnabled', 'false');
	}
	
	$.log._loggingIsEnabled = ($.cookie('loggingIsEnabled') == 'true');
	var functionNames = ['log','debug','info','warn','error','time','timeEnd','profile','profileEnd','trace','group','groupEnd','dir','dirxml','assert'];
	var logFunctions = {};
	$.each(functionNames, function(functionIndex) {
		var functionName = functionNames[functionIndex];
		$.log[functionName] = function(){
			if (window.console && window.console[functionName] && $.log._loggingIsEnabled) {
				window.console[functionName].apply(window.console, arguments);
			}
			return this;
		};
		$.log[functionName + 'Vars'] = function(params){
//$.log.group(functionName + 'Vars');
			if (window.console && window.console[functionName] && $.log._loggingIsEnabled ) {
//$.log.log(functionName + 'Vars: ' + typeof(params));
				if ('object' == typeof(params)) {
					$.each(params, function(index) {
//$.log.log(['each',index,typeof(params[index]),params[index]]);
						if ('object' == typeof(params[index])) {
//$.log.log(['each object',index,typeof(params[index]),params[index]]);
							$.log.group(index);
//$.log.log(params[index]);
//$.log.log($.log[functionName + 'Vars']);
							$.log[functionName + 'Vars'](params[index]);
							$.log.groupEnd();
						} else {
//$.log.log(['each non-object',index,typeof(params[index]),params[index]]);
							$.log[functionName](index + ': ' + params[index]);
						}
					});
				} else {
//$.log.warn(['else',index,functionName,params]);
					$.log[functionName](params);
				}
			}
//$.log.groupEnd();
			return this;
		}
//		$.fn['$' + functionName] = function() {
//			if (window.console && window.console[functionName]) {
//				window.console[functionName].apply(window.console, arguments);
//			}
//			return this;
//		};
//
	});
	
	$.log.isLoggingEnabled = function() {
		return $.log._loggingIsEnabled;
	}
	$.log.startLogging = function() {
		$.cookie('loggingIsEnabled', 'true');
		$.log._loggingIsEnabled = ($.cookie('loggingIsEnabled') == 'true');
		$.log.info('Logging is now enabled.');
		return this;
	}
	$.log.stopLogging = function() {
		$.log.info('Logging is about to be disabled.');
		$.cookie('loggingIsEnabled', 'false');
		$.log._loggingIsEnabled = ($.cookie('loggingIsEnabled') == 'true');
		return this
	}
	$.log.setLogging = function() {
		if ((arguments.length > 0) && (arguments[0])) {
			$.log.startLogging();
		} else {
			$.log.stopLogging();
		}
		return this
	}
	$.log.groupFunc = function() {
		if ($.log._loggingIsEnabled) {
			var callerFuncName = arguments.callee.caller.toString();
			callerFuncName = callerFuncName.substr('function '.length); // trim off "function "
			callerFuncName = callerFuncName.substr(0, callerFuncName.indexOf('(')); // trim off everything after the function name
			$.log.group(callerFuncName);
		}
		return this;
	}

	$.log.logVars = function(params) {
		if ($.log._loggingIsEnabled && ('object' == typeof(params))) {
			$.each(params, function(index) {
				if ('object' == typeof(params[index])) {
					$.log.group(index);
					$.log.logVars(params[index]);
					$.log.groupEnd();
				} else {
					$.log.log([index,params[index]]);
				}
			});
		}
		return this;
	}

})(jQuery);

