/* @requires  -- jQuery 1.0.3
   @new!      -- array debugging
 */
// global debug switch ... add DEBUG = true; somewhere after jquery.debug.js is loaded to turn debugging on
var DEBUG = true;
// 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
	jQuery(document).ready(
		function(){
		    if(DEBUG) {
			   $(document.body).append('<div id="DEBUG"><ol></ol></div>');
			}
		}
	);
	// 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){ 
		    if(DEBUG) {
		        $('#DEBUG ol').append( '<li>' + msg + '</li>' ); 
		    }
		}
	}
}

/*
 * 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){
	// only if debugging is on
	if( window.DEBUG ){
		// if no firebug, build a debug line from the actual html element if it's an object, or just send the string
		var str = '';
		
		if(!$.isArray(message)) {
		    str += message;
		} else {
	       //print_r();
	       str += "Array [\n";
	       $.each(message, function(i, val) {
	          str += '- - val['+i+']: '+val;
	          str += "\n";
            //$("#" + i).append(document.createTextNode(" - " + val));
          });
          str += ']';
		}
		if( !('firebug' in console) ){
		    if($.isArray(message)) {
		       //print_r();
		       str += "Array [<br>";
		       $.each(message, function(i, val) {
		          str += '- - val['+i+']: '+val;
		          str += '<br>'
               //$("#" + i).append(document.createTextNode(" - " + val));
             });
             str += ']';
		    } else 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;';
			}
		}
		console.debug(str);
	}
};


/*
   if (window['loadFirebugConsole']) {
   	window.loadFirebugConsole();
   } else {
   	if (!window['console']) {
   		window.console = {};
   		window.console.info = alert;
   		window.console.log = alert;
   		window.console.warn = alert;
   		window.console.error = alert;
   	}
   }
   
   */

/*
function encHTML(str){ 
   return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); 
} 
 
(function($){ 
  $.debug = { 
    dump: function(arr, level, enc) { 
      var dumped_text = ""; 
      if(!level) level = 0; 
      var level_padding = ""; 
      for(var j=0;j<level+1;j++) level_padding += "    "; 
      if(typeof(arr) == 'object') { //Array/Hashes/Objects 
       for(var item in arr) { 
        var value = arr[item]; 
        
        if(typeof(value) == 'object') { //If it is an array, 
         dumped_text += level_padding + "'" + item + "' ...\n"; 
         dumped_text += $.debug.dump(value,level+1); 
        }else if(typeof(value) == 'string'){ 
         value = enc == true ? encHTML(value) : value; 
         dumped_text += level_padding + "'" + item + "' => \"" + encHTML(value) + "\"\n"; 
        } else { 
         dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; 
        } 
       } 
      } else { //Stings/Chars/Numbers etc. 
       dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; 
      } 
      return dumped_text; 
    }, 
    print_r: function(obj, contId){ 
      $("#"+contId).removeClass().css({ 
        display: "block", 
        position: "absolute", 
        top: "0px", 
        right: "0px", 
        padding: "10px", 
        width: "700px", 
        height: "auto", 
        background: "#ddd", 
        color: "black", 
        border: "solid 1px black" 
      }).html("<pre>"+$.debug.dump(obj)+"</pre><div id='close-debug'>Close</div>"); 
       
      $("#close-debug").css({cursor: "pointer"}).click(function(){ 
        $("#"+contId).remove(); 
      }); 
    } 
  }; 
})(jQuery);*/

