/*
**  jquery.dumper.js -- jQuery plugin for debugging
**  Copyright (c) 2007 Lokkju, Inc <lokkju@lokkju.com> 
**  Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>, other licenses available.
**
**  $LastChangedDate$
**  $LastChangedRevision$
**
**	
**	Usage:
**		$.dump(obj,r)
**			Returns a string
**			Parameters:
**				obj		-	any javascript object, including dom elements
**				r			-	0 or null for html formatted return, 1 for plain text
**
**		$(obj).dump(r)
**			Returns a string
**			Parameters:
**				obj		-	any object or jquery selector
**				r			-	0 or null for html formatted return, 1 for plain text
**
*/

(function($){
	$.extend({
		dump : function (obj,r) {
			e = obj;
			var ns = ["Methods", "Fields", "Unreachables"];
			var as = [[], [], []];
			var p, j, i;
			var protoLevels = 0;
			for (p = e; p; p = p.__proto__){
				for (i=0; i<ns.length; ++i){as[i][protoLevels] = [];}
				++protoLevels;
			}
			for(var a in e){
				var protoLevel = -1;
				try {
					for (p = e; p && (a in p); p = p.__proto__)	{++protoLevel;}
				} catch(er) { protoLevel = 0; }
				var type = 1;
				try {if ((typeof e[a]) == "function"){type = 0;}} catch (er) { type = 2; }
				as[type][protoLevel].push(a);
			}
			function times(s, n) { return n ? s + times(s, n-1) : ""; }
			var pdiv = document.createElement("div");
			var msg = "";
			for (j=0; j<protoLevels; ++j){
				for (i=0;i<ns.length;++i){
					if (as[i][j].length){
						var h= ns[i] + times(" of prototype", j);
						var s = as[i][j].join(", ");
						var type = "propList";
						var newdiv = document.createElement("div");
						newdiv.appendChild(document.createTextNode(s));
						newdiv.className = type;
						var head = document.createElement("strong");
						head.appendChild(document.createTextNode(h + ": "));
						newdiv.insertBefore(head, newdiv.firstChild);								
						msg += h + ":" + "\n";
						msg += s + "\n";
						pdiv.appendChild(newdiv);
						
					}
				}
			}
			if(r > 0){return msg;}
			else {return $(pdiv).html();}
		}
	});
	$.fn.extend({
		dump: function (r) {
			var msg = "";
			this.each(function () {
				msg += $.dump(this,r);
			});
			return msg;
		}
  });
})(jQuery);
