/*
 * Simple jQuery logger / debugger.
 * Based on: http://jquery.com/plugins/Authoring/
 * See var DEBUG below for turning debugging/logging on and off.
 *
 * @version   20070111
 * @since     2006-07-10
 * @copyright Copyright (c) 2006 Glyphix Studio, Inc. http://www.glyphix.com
 * @author    Brad Brizendine <brizbane@gmail.com>
 * @license   MIT http://www.opensource.org/licenses/mit-license.php
 * @requires  >= jQuery 1.0.3
 *
 * Extensions:
 * Added timestamp to the log
 * Added support for log messages added before document ready
 * @author pauls
 */

/*
 * debug
 * Simply loops thru each jquery item and logs it
 */
jQuery.fn.debug = function() {
  return this.each(function(){
    $.log(this);
  });
};

/*
 * log
 * Send it anything, and it will add a line to the logging console.
 * If firebug is installed, it simple send the item to firebug.
 * If not, it creates a string representation of the html element (if message is an object), or just uses the supplied value (if not an object).
 */
jQuery.log = function(message, warn){
    
  // only if debugging is on
  if( debugmodeon ){
    // if no firebug, build a debug line from the actual html element 
    // if it's an object, or just send the string   
        var time = new Date();
        var current = time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();            
    var str = current + '   ' + message;
    
    if( !('firebug' in console) ){
      if( typeof(message) == 'object' ){
        str = '&lt;';
        str += message.nodeName.toLowerCase();
        for( var i = 0; i < message.attributes.length; i++ ){
          str += ' ' + message.attributes[i].nodeName.toLowerCase() + '="' + message.attributes[i].nodeValue + '"';
        }
        str += '&gt;';
      }
    }
    if (warn) {
      console.warn(str);
    } else {
        console.debug(str);
    }
  }
};

jQuery.log.warn = function(message) {
    if (testdata == true) {
        jQuery.log(message, true);
    }
};

jQuery.log.debug = function(message) {
    if (testdata == true) {
        jQuery.log(message, false);
    }
};

/*
 * debug
 * Simply loops thru each jquery item and logs it
 */
jQuery.log.add = function(msg) {
   // Escape some XML characters
   if (msg != null) {
     msg = msg.replace('<', '&lt;');    
   }
   
   // Save the messages added before the document is ready 
   if (preLoadLogMessages) {
    preLoadLogMessages.push(msg);
   } else {
    $('#DEBUG ol').append('<li><pre>' + msg + '</pre></li>'); 
   }
};

debugmodeon = false;
preLoadLogMessages = [];

// shamelessly ripped off from http://getfirebug.com/
if (!("console" in window) || !("firebug" in console)){
  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
  // create the logging div
  if (debugmodeon) {
    jQuery(document).ready(
      function(){
        
        // When document is ready add the log div.
        $(document.body).append('<div id="DEBUG" style="border : 1px solid #ab1ab2; margin  : 10px; text-align : left"><ol></ol></div><br />');
        
        // Add the messages added before document ready
        var logMessages = preLoadLogMessages; 
        preLoadLogMessages = undefined;
           for (i = 0; i < logMessages.length; i++) {
            var logMessage = logMessages[i];
            jQuery.log.add(logMessage);
           }
      }
    );
  }
  // only if debugging is on
  if( debugmodeon ){
    // attach a function to each of the firebug methods
    window.console = {};
    for (var i = 0; i < names.length; ++i){
      window.console[names[i]] = function(msg){
        jQuery.log.add(msg);
        }
    }
  }
}
