﻿
var log = {

    disable: function() { 
       this.writeLine = function() {
              console.log("warning: log is disabled but someone is still calling it");
       };
       // disable call chain logging
       jQuery.fn.log = function() {
        console.log("warning: log is disabled but someone is still calling it"); return this;
       };
    },
    // Simple log
    // example: log.writeLine("[event]: message");
    writeLine: function(msg) {
        //console.log(msg);
        if (!$('.log').length) {
            $('<div class="log" style="height:10em;overflow:scroll;border:solid 1px"></div>').insertBefore('#header_container');
        }
        $('.log').each(function() {
          $(this).append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
          .attr({ scrollTop: $(this).attr('scrollHeight') })
          .find('p:nth-child(even)').addClass('even');
        });
    },
    // Serialization utility
    formatObject: function(obj, re) {
        var result = [];
        $.each(obj, function(i, val) {
            if ((re && re.test(i)) || !re)
                result.push(i + ': ' + (typeof val == 'object' ? val.join
                                ? '\'' + val.join(', ') + '\'' : log.formatObject(val) : '\'' + val + '\''));
        });
        return '{' + result.join(', ') + '}';
    }

};
// place this in a call chain like so:
//$(root).find('li.source> input:checkbox').log("sources to uncheck").removeAttr("checked");
jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
  };
