﻿/*
	This plugin is written by Aidan Thomas.
	
	It is to be used as an alternative to alert pop up windows when debugging javascript.
	
	It logs messages to a pop up window ('debug console') and the display of the debug console can be controlled via a url parameter.
	This allows you to run a page in 'debug' mode and the debug console will not appear otherwise.
	This is to prevent the debug console from appearing for other users not running in debug mode and makes this plugin safe to run on live sites.
	
	The plugin accepts the following parameters

	param: (text) - this controls what url parameter to look for, and if it exists and matches the 'paramVal' value open the debug console and start logging messages. (default value is 'debug')
	
	paramVal: (text)  - this controls the value of the url parameter to look for and if the parameter exists and matches this value open the debug console and start logging messages. (default value is 'true')
	
	debugConsoleName: (text) - using this value you can open different debug consoles and write separate messages to each console. (default '')
	
	newLineAfter: (true/false) - setting this to true will append an end line to the message so the next message appears on a new line in the debug console. (default 'true')

	showLastMsgOnly: (true / false) - using this parameter you can get the debug console to only show the last message sent to it. (default is 'false')
	
	focus: (true/false) - setting this to true will set the focus to the debug console
	
	newWindow: (true/false) - setting this to true will close the debug console if already open and open a new debug console
	
	//The parameters are passed as the second variable to the function between {} separated by a comma, e.g. $.debugJS('message',{parameterName1: parameterValue1,{parameterName2: parameterValue2});
	
	// EXAMPLE USAGE //
	
	Using Defaults:
		$.debugJS('message');
		
	Opening A Fresh Console:
		$.debugJS('message',{newWindow: true});
		
	Opening Two Separate Consoles:
		$.debugJS('message',{debugConsoleName: 'console1',newWindow: true});
		$.debugJS('message',{debugConsoleName: 'console1',newWindow: true});
*/

$.extend({ 
	debugUrlVars: function()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0].toLowerCase());
			vars[hash[0].toLowerCase()] = hash[1];
		}
		return vars;
	},
	debugUrlVar: function(name)
	{
		return $.debugUrlVars()[name.toLowerCase()];
	},
	debug: function(msg,settings)
	{
		try
		{
			if($.debugUrlVar(settings.param) == settings.paramVal)
			{
				if ((settings.dwin == null) || (settings.dwin.closed))
				{
					settings.dwin = window.open("","debugconsole"+settings.debugConsoleName,"scrollbars=yes,resizable=yes,height=100,width=300");
					if(settings.newWindow)
						settings.dwin.document.close();
					settings.dwin.document.open("text/html", "replace");
				}
				if(settings.showLastMsgOnly) // see only last message , not all the previous messages
				{
					settings.dwin.document.close();
					settings.dwin.document.open("text/html", "replace");
				}
				if(settings.newLineAfter) // append the end line character
					msg += '<br>';
				settings.dwin.document.writeln(''+msg);
				if(settings.focus) // set the focus to the alert window
					settings.dwin.focus(); 
			}
		}
		catch(err)
		{
			txt="There was an error running the debug script.\n\n";
			txt+="Error description: " + err.description + "\n\n";
			txt+="Click OK to continue.\n\n";
			alert(txt);
		}
	},
	debugJS: function(msg,settings)
	{
		settings = jQuery.extend(
		{
			param: 'debug',
			paramVal: 'true',
			debugConsoleName: '',
			newLineAfter: true,
			showLastMsgOnly: false,
			focus: false,
			newWindow: false
		}, settings);

		$.debug(msg,settings);
	}
});

