var Cms = Cms || {};

/**
 * @class RC.Console
 * Facade for Firebug's console object. See {@link http://getfirebug.com/logging} for details.
 * @singleton
 */
Cms.Console = (function(){

    /**
     * Checks, if console is available
     * @return {Boolean}
     * @private
     */
    var isAvailable = function(){

        // Check if cookie is set
        return Cms.debug;

    };

    var call = function(name, args){

        if (!isAvailable()) return;
        if (typeof console == 'undefined') return;
        if (typeof console[name] == 'undefined') console.warn('Function "' + name + '" is not implemented.');

        if ($.browser.msie) {

            var callString = [];

            for (var i = 0; i < args.length; i++) {

                callString.push('args[' + i + ']');

            }
            eval('(typeof console[name] == "function") && console[name](' + callString.join(', ') + ');');

        } else {

            console[name].apply(console, args);

        }

    };

    /**
     * Uses Firebug syntax ? arguments can be:
     *   2. RC.Console(Var1, Var2,?) ? as many arguments as you want and they will be joined together in a row
     *   1. RC.Console(Object) ? pass any kind of object and it will be displayed as a hyperlink (elements, functions, arrays, plain ol' objects, etc.)
     *   3. RC.Console(StringFormat, Var1, Var2,?) ? format strings in the great tradition of printf
     * @private
     */
    var self = function(){
        call('log', arguments);
    };

    self.log = function(){
        self.apply(self, arguments);
    };

    self.warn = function(){
        call('warn', arguments);
    };

    self.info = function(a1, a2, a3, a4, a5, a6){
        call('info', arguments);
    };

    self.error = function(){
        call('error', arguments);
    };

    /**
     * Log an interactive listing of an object's properties, like a miniature version of the DOM tab
     * @param {Object} object
     */
    self.dir = function(object){
        call('dir', arguments);
    };

    /**
     * Print a lovely XML outline, like a miniature version of the HTML tab.
     * @param {Object} element HTML or XML element
     */
    self.dirxml = function(element){
        call('dirxml', arguments);
    };

    /**
     * Firebug will write a very informative stack trace to the console
     */
    self.trace = function(){
        call('trace', arguments);
    };

    var lastTime = '';
    var lastProfile = '';
    var lastGroup = '';

    /**
     * Starts timing
     * @param {String} name
     */
    self.time = function(name){
        lastTime = name;
        call('time', arguments);
    };

    /**
     * Finishes timing
     * @param {String} name If omitted - last started timing is used
     */
    self.timeEnd = function(name){
        if (!name) arguments = [lastTime];
        call('timeEnd', arguments);
    };

    /**
     * Starts profiling
     * @param {String} name
     */
    self.profile = function(name){
        lastProfile = name;
        call('profile', arguments);
    };

    /**
     * Finishes profiling
     * @param {String} name If omitted - last started profiling is used
     */
    self.profileEnd = function(name){
        if (!name) arguments = [lastProfile];
        call('profileEnd', arguments);
    };

    /**
     * Start a new indentation block
     * @param {String} name
     */
    self.group = function(name){
        lastGroup = name;
        call('group', arguments);
    };

    /**
     * Close an indentation block
     * @param {String} name If omitted - last started group is used
     */
    self.groupEnd = function(name){
        if (!name) arguments = [lastGroup];
        call('groupEnd', arguments);
    };

    return self;

})();
