/*
 *	jQuery logger
 *	copyright 2009
 *
 *	@author 	Boy van Amstel
 *	@version 	1.0
 *
 *	Usage:
 *	// Load the debugger class
 *	$(this).logger({ console: console, enabled: true, title: "My debugger" });
 *	// Log an event
 *	$("div").log("Set div to hidden!", "warn", "Some title").hide();
 *
 *	Minimal setup:
 *	$(this).logger({ enabled: true });
 *	$("div").log("Set div to hidden!").hide();
 *
 *	Multiple messages:
 *	$(this).logger({ enabled: true });
 *	$("div").log(([{ type: "warn", msg: "Set div to hidden!"}, {type: "info", msg: "It's really gone.." }]), null, "Some title");
 *
 *	Changes 1.0:
 *	[11/09/2009]	- First release 
 */
 
jQuery.logger = 
{
	console: window.console,
	enabled: false,
	title: "jQuery.logger",
	defaultLogType: "info",
	defaultLogTitle: "Default",
	build : function(options) {

		// Default settings
		var defaults = {
			console: jQuery.logger.console,
			enabled: jQuery.logger.enabled,
			title: jQuery.logger.title,
			defaultLogType: jQuery.logger.defaultLogType,
			defaultLogTitle: jQuery.logger.defaultLogTitle		
		}

		// Move to options
		var options = jQuery.extend(defaults, options);

		// Attach options to object
		jQuery.logger.console = options.console;
		jQuery.logger.enabled = options.enabled;
		jQuery.logger.title = options.title;
		jQuery.logger.defaultLogType = options.defaultLogType;
		jQuery.logger.defaultLogTitle = options.defaultLogTitle;

		// If the console is set, notify the user everything went fine
		if(options.console)
			this.log("Class loaded", "info", options.title);
			
	},
	// Send a debug message
	debug : function(logs) {

		// Only debug when possible
		if(!logs || !jQuery.logger.console || !jQuery.browser.mozilla || !jQuery.logger.enabled)
			return false;

		// Set group name
		jQuery.logger.console.group(jQuery.logger.title);
		
		// Show the messages
		for(msg in logs.msgs)
			jQuery.logger.console[logs.msgs[msg].type](logs.title + " - "+logs.msgs[msg].msg);
	
		// End group
		jQuery.logger.console.groupEnd();
		
	},
	// Send a log message to jQuery.logger.debug
	log : function(msg, type, title) {
		
		if(typeof(msg) == "object") {
			//alert(msg.length);
			// Send to the debugger	
			jQuery.logger.debug({ 
				title: title,
				msgs: msg 
			});
		
		}
		else {
		
			// Send to the debugger	
			jQuery.logger.debug({ 
				title: title,
				msgs: [{ type: type, msg: msg }] 
			});

		}		
	}
};

jQuery.fn.extend({
		logger: jQuery.logger.build
});	

// Add log functionality to all objects
jQuery.fn.log = function (msg, type, title) {
	if(!type) type = jQuery.logger.defaultLogType;
	if(!title) title = jQuery.logger.defaultLogTitle;
	jQuery.logger.log(msg, type, title);
	return this;
};