// A Logger
// Copyright 2011 Christian Wirkus, http://wasserschorle.de/license

// Object LOG with the methods info, log, error, warn.
// Turn off by LOG.off(). On by LOG.on(). Default is on.
// use it like this:

// LOG.off();

// LOG.error("it's off man");
// LOG.info("it's off man");
// LOG.log("it's off man");
// LOG.warn("it's off man");

// LOG.on();

// LOG.error("LOG.error");
// LOG.info("LOG.info");
// LOG.log("LOG.log");
// LOG.warn("LOG.warn");
// LOG.log("" + LOG);

var LOG = (function () {
    var empty, log, maker, on;

    empty = function () {};

    // if there is no log, there's gonna be nothing
    if (typeof console !== "object" || !console || !console.log) {
        return {
            error: empty,
            info: empty,
            log: empty,
            warn: empty,
            off: empty,
            on: empty,
            toString: function () {
                return "There is no log :(";
            }
        };
    }

    on = true;

    maker = function (type) {
        // resisted the temptation of a ternary :)

        // defaults to log (log has to exist, see above)
        if (!console[type]) {
            return log;
        }

        // if possible, make it look exactly like the built-in log
        if (console[type].apply) {
            return function () {
                if (!on) {
                    return;
                }
                console[type].apply(console, arguments);
            };
        }

        // if apply not applicable (well), just give the array :(
        return function () {
            if (!on) {
                return;
            }
            console[type](Array.prototype.slice.call(arguments));
        };
    };

    log = maker("log");

    return {
        log: log,
        error: maker("error"),
        info: maker("info"),
        warn: maker("warn"),

        off: function () {
            on = false;
            return on;
        },
        on: function () {
            on = true;
            return on;
        },
        toString: function () {
            return "Object LOG with the methods info, log, error, warn. " +
                "Turn off by LOG.off(). On by LOG.on(). Default is on. " +
                "LOG is now " + (on ? "on." : "off.");
        }
    };
}());

// LOG.log("" + LOG);

