/*
	Class: Console
	
	This is a simplistic solution to js logging in browsers (IE) that do not
	then this class will provide it as well as a means to access it.
	
	It is simplistic because right now it only accepts a single argument and 
	expects that argument to be a string.  It does not (yet?) accept the range
	of arguments, formats, etc. that the Firebug console does.
	
	As well as defining the Console class this script, upon inclusion into your
	HTML file will check for the existance of a window.console object and if 
	none is found will create one by instantiating this Console class.
	
	Therefore this script should be sourced in your HTML file as early as 

	Todo:
	
	- Improve the log method so that it more closely mirrors the 
	capabilities and 'signature' of the Firebug console class.
	- provide other (possibly empty/dummy) Firebug console methods.
*/

var console;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*                             Constructor                               */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* 
	Constructor: Console()

	Construct a new console instance.
	
	Returns: 
	a new Console instance
	
*/
function Console()
{
	// private log message list
	this.messageBuffer = [];

	/*
		consoleObj.isSafeFallback()
		
		If the consoleObj has this function then it's not the Firebug console. 
		:^)
		
		e.g. <a href="javascript: if ( typeof console.isSafeFallback !== 'undefined') { console.display() } else { alert( 'not the bogus console' }">Show log</a>
	*/
	this.isSafeFallback = function() { return true; };
	this.isFakeOutStyled = false;
	
	// quick and dirty check for js debug flag
	this.isJSDebug = false;
}
	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*                            Public Methods                             */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* 
	Method: log()
	
	Replaces the functionality of a Firebug-enabled Firefox 'console' 
	logger.  This is a simplest case replacement and only supports 
	message insertion and display.
	
	Arguments:
		inMsg - the string message to log
		
*/
Console.prototype.log  = function( inMsg, inLevel, inSource )
{
	this.messageBuffer.push( [inMsg, inLevel, inSource] );
	
	// Trim to a manageable length
	if ( this.messageBuffer.length > 300 )
	{
		this.messageBuffer.push( ["Trimming log buffer", 'info', 'Console'] );
		this.messageBuffer.splice( 0, this.messageBuffer.length - 300 );
	}
	
	this.display();
}

/* 
	Method: display()
	
	Call this function to get the console.log messages displayed.  If the
	document object has an element with an id of 'fakeConsoleOutput' then
	the innerHTML of that element will be changed to a list of log messages.
	
	If no such element exists then a javascript alert will popup.
*/
Console.prototype.display  = function()
{
	var 
		/* our output container */
		outputContainer = 
			document.getElementById( "bebogusconsoleel" ),
		
		// loop counter
		i,
		
		// a log record within the loop
		aRecord,
		
		// the result buffer onto which we will append
		result = ''
		;

	if ( typeof document.cookie !== 'undefined' && document.cookie )
	{
		this.isJSDebug = document.cookie.toString().indexOf( 'jsdebug=1' ) > -1;
	}
	
	if ( this.isJSDebug )
	{
		if ( ! outputContainer && this.isJSDebug )
		{
			outputContainer = this.createContainer();
		}
	
		if ( typeof outputContainer !== 'undefined' && outputContainer )
		{
			for ( i = this.messageBuffer.length - 1; i >= 0 ; i-- )
			{
				aRecord = this.messageBuffer[ i ];
				result += '<li style="border-bottom: 1px solid #efefef">' + aRecord[ 0 ];
				
				if ( aRecord[ 1 ] )
				{
					result += ', ' + aRecord[ 1 ];
				}
				else if ( aRecord[ 2 ] )
				{
					result += ', undef';
				}
				
				if ( aRecord[ 2 ] )
				{
					result += ', ' + aRecord[ 2 ];
				}
				result += '</li>';
			}
			
			outputContainer.innerHTML = '<ul style="width:95%">' + result + "</ul>";
		}
	} // if ( this.isJSDebug )
}

Console.prototype.createContainer = function()
{
	var
		// our output container
		container
		;
	
	if ( typeof document.body !== 'undefined' && document.body )
	{
		container = document.createElement( 'div' );
		
		container.setAttribute( 'style', 'height:15em;width:30em;border:2px solid #666;overflow:scroll;font: sans-serif 9pt;' );
		container.setAttribute( 'id', 'bebogusconsoleel' );
		
		document.body.appendChild( container );
		
		container.innerHTML = '<h3>BasicLog</h3><ul><\/ul>';
		
		// IE hack
		if ( typeof container.style !== 'undefined' 
			&& typeof container.style.cssText !== 'undefined' )
		{
			container.style.cssText = 'height:400px;width:600px;border:2px solid #666;overflow:scroll;';
		}
	}
	else
	{
		this.messageBuffer.push( ["document.body not ready, not creating container", null, "Console"] );
	}
	return container;
}

Console.prototype.containerCreated = function()
{
	return (document.getElementById( 'bebogusconsoleel' ) !== null);
}

/* 
	Method: clearLog()
	
	Clears out the console log.
*/
Console.prototype.clearLog  = function()
{
	this.messageBuffer = new Array();
}

// - - - E N D   C L A S S - - - - - - - - - - - - - - - - - - - - - - - - - - 


/*
	Don't blow on non-FF browsers because of a missing console object
*/
if ( typeof console === "undefined" )
{
	console = new Console();
	console
		.log( "Custom console loaded" );
}