/*
----------------------------------------------- 
Toyota
author: Sam Baker
studio:	hothouse interactive
website:www.hothouse.com.au
email: 	info@hothouse.com.au
----------------------------------------------- 
Grade Comparison Tool: Utilities
Simple little utility methods used by the model
and renderer code.
-----------------------------------------------
Copyright Hothouse Interactive
:: www.hothouse.com.au ::
Unauthorised modification / use is a 
criminal offence, and will be prosecuted 
to the fullest extent permitted by law.
All Rights Reserved 
----------------------------------------------- 
*/


// A nifty little text buffer class. Takes advantage of the fact that array
// joins are faster than string additions for large amounts of text.
function TextBuf()
{
	// TextBuf Constructor:
	this.buf = new Array();
	
	
	// Adds a line of text to this buffer.
	this.addLine = function(txt)
	{
		this.buf[this.buf.length] = txt;
	}
	
	
	// Adds the contents of the given TextBuf to this TextBuf.
	this.addBuf = function(buf)
	{
		for (var i in buf.buf) {
			this.addLine(buf[i]);
		}
	}
	
	
	// Returns the contents of this TextBuf as a single string.
	this.toString = function()
	{
		return this.buf.join("\r\n");
	}
}


// Returns a string with n characters.
function repeat(ch, n)
{
	var result = "";

	for (var i=0; i<n; i++)
	    result += ch;
	
	return result;
}


// Describes an object. Level is used to control the indent level.
// NOTE: Currently, arrays are only displayed properly in Firefox.
function describe(obj, level)
{
	var buf = new TextBuf();
	
	for (var i in obj) {
		var o = obj[i];
		
		if (!o.prototype) {
			if (o.describe) {
				buf.addLine(repeat(" ", level) + i + ": Object");
				buf.addLine(o.describe(level+2));
			} else {
				if (o.__proto__ == Array.prototype) {
					buf.addLine(repeat(" ", level) + i + ": Array");
					buf.addLine(describe(o, level+2));
				} else {
	                buf.addLine(repeat(" ", level) + i + ": "+obj[i]);
	            }
	        }
	    }
	}
	
	return buf;
}

