/**
 * console style in your main stylsheet :
 * 
 * .console
 * {
 *     background   : #333333;
 *     border-color : #666666;
 *     border-style : solid;
 *     border-width : 2px;
 * 
 *     padding      : 10px;
 *     margin       : 10px;
 * 
 *     font-family  : Courier New;
 *     font-size    : 12px;
 *     
 *     width        : 680px;
 *     height       : 400px;
 *     
 *     overflow     : auto;
 * }
 * 
 * .debug { color : #FFFFFF; }
 * .error { color : #FF00FF; }
 * .fatal { color : #B40404; }
 * .info  { color : #088A08; }
 * .log   { color : #000000; }
 * .warn  { color : #FF8000; }
 */

/**
 * Creates a new Console instance. 
 * The global console reference in the browser not exist ? Creates a new console to emulate it.
 */
Console = function( target /*HTMLElement*/ ) 
{
    this._count  = 0 ;
    if ( target )
    {
        this.element = document.createElement("div") ;
        this.element.className = "console" ;
        target.appendChild( this.element ) ;
    }
    else
    {
        //
    }
} ;

/**
 * The debug mode (default false).
 */
Console.debug = false ;

/**
 * The verbose mode (default false).
 */
Console.verbose = false ;

/**
 * Initialize the console in the page.
 * @param target The HTMLElement reference to create the console.
 * @param fix The optional Boolean to enforce the console to be created.
 */
Console.initialize = function( target /*HTMLElement*/ , fix /*Boolean*/ ) /*void*/
{
    if ( Console.getParameter("debug") )
    {
        Console.debug = Boolean( Console.getParameter("debug") === "true" ) ;
    }
    if ( Console.getParameter("verbose") )
    {
        Console.verbose = Boolean( Console.getParameter("verbose") === "true" ) ;
    }
    var flag /*Boolean*/ = Boolean( Console.getParameter("console") === "true" ) ;
    if ( flag || fix )
    {
        console = new Console( target ) ;
    }
}

/**
 * Returns the specified parameter in the query of the page uri.
 * @return the specified parameter in the query of the page uri.
 */
Console.getParameter = function( name /*String*/ ) /*String*/
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex   = new RegExp( "[\\?&]"+name+"=([^&#]*)" ) ;
    var results = regex.exec( window.location.href );
    return (results == null ) ? null : results[1] ;
}

/**
 * @extends Object
 */
proto = Console.prototype ;

/**
 * The internal HTMLElement reference to display the messages.
 */
proto.element /*HTMLElement*/ = null ;

/**
 * Display a new debug message.
 */
proto.debug = function( o ) /*void*/
{
    this._log( "debug" , o ) ;
};

/**
 * Display a new fatal message.
 */
proto.fatal = function( o ) /*void*/
{
    this._log( "fatal" , o ) ;
}

/**
 * Display a new error message.
 */
proto.error = function( o ) /*void*/
{
    this._log( "error" , o ) ;
}

/**
 * Display a new info message.
 */
proto.info = function( o ) /*void*/
{
    this._log( "info" , o ) ;
}

/**
 * Display a new log message.
 */
proto.log = function( o ) /*void*/
{
    this._log( "log" , o ) ;
}

/**
 * Returns the String reprensation of the object.
 * @return the String reprensation of the object.
 */
proto.toString = function() /*String*/
{
    return "[Console]" ;
}

/**
 * Display a new warn message.
 */
proto.warn = function( o ) /*void*/
{
    this._log( "warn" , o ) ;
}

/**
 * @private
 */
proto._log = function( level /*String*/ , o /*Object*/ ) /*void*/
{
    var message = this.element.innerHTML ;
    message += '<span class="' + level + '">' + "[" + this._count++ + "] " + o + '</span><br />' ;
    if ( this.element )
    {
        this.element.innerHTML = message ;
        this.element.scrollTop = this.element.scrollHeight;
    }
}

/**
 * @private
 */
delete proto ;

