// Copyright (C) 2007 Stephane Lavergne <http://www.imars.com/>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.

// Debugging tools
imars.debug = {

	// Analogue to PHP's print_r().
	// FIXME: Easily reaches max recursion depth. We might need to figure out a
	// way to have a stack to take note of crawled objects and avoid crawling
	// the same one twice if it's referenced multiple times. (If that's the
	// problem at all?)
	print_r: function (input, _indent)
	{
		var output = "";
		if(typeof(_indent) == 'string') {
			var indent = _indent + '    ';
			var paren_indent = _indent + '  ';
		} else {
			output = "\n<pre>\n";
			var indent = '    ';
			var paren_indent = '';
		}
		switch(typeof(input)) {
			case 'boolean':
				output += (input ? 'true' : 'false') + "\n";
				break;
			case 'object':
				if ( input===null ) {
					output += "null\n";
					break;
				}
				output += ((input.reverse) ? 'Array' : 'Object') + " (\n";
				for(var i in input) {
					output += indent + "[" + i + "] => " + imars.debug.print_r(input[i], indent);
				}
				output += paren_indent + ")\n";
				break;
			case 'number':
			case 'string':
			default:
				output += "" + input  + "\n";
		}
		if (typeof(_indent) != 'string') output += "</pre>\n";
		return output;
	}

};
