/**
 * This example is from JavaScript: The Definitive Guide, 3rd Edition.
 * That book and this example were Written by David Flanagan.
 * They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.
 * This example is provided WITHOUT WARRANTY either expressed or implied.
 * You may study, use, modify, and distribute it for any purpose,
 * as long as this notice is retained.
 */

/**
 * CHANGELOG: This code has been heavily re-written by Christopher M. Balz
 * in an object-oriented form, and also to conform to a type-signifying
 * coding convention, to JavaDoc standards -- and most importantly to be 
 * much nicer to read.
 */

/**
 * This is the constructor for the prototype of the StackTrace class.
 */
function PrtStackTrace() {
	// Properties:
	this.strStackTrace = "Stack Trace: \n";	 // This is the string we'll return.
	// Methods:
	this.priGetFuncName = StackTrace_priGetFuncName;
	this.getStackTrace = StackTrace_getStackTrace;
}

StackTrace.prototype = new PrtStackTrace();

/**
 * This is the constructor for the StackTrace class.
 */
function StackTrace() {
	/*-
	 * Loop through the stack of functions, using the caller property of
	 * one arguments object to refer to the next arguments object on the
	 * stack.
	 */
	for(var funCallingFunc = arguments.caller; funCallingFunc != null; funCallingFunc = funCallingFunc.caller) {
		// Add the string-type name of the current function to the return value.
		this.strStackTrace += this.priGetFuncName(funCallingFunc.callee) + "\n"; 
		alert("this.strStackTrace="+this.strStackTrace);
		/*-
		 * Because of a bug in Navigator 4.0, we need this line to break.
		 * funCallingFunc.caller will equal funCallingFunc rather than null when we reach the end 
		 * of the stack. The following line works around this.
		 */
		if (funCallingFunc.caller == funCallingFunc) {
			break;
		}
	}
}

/**
 * This method returns the name of a given function. It does this by
 * converting the function to a string, then using a regular expression
 * to extract the function name from the resulting code.
 * @param funF The function in question, passed as a function literal. 
 */
function StackTrace_priGetFuncName(funF) {
	var strFuncName = funF.toString().match(/function (\w*)/)[1];
	if ((strFuncName == null) || (strFuncName.length == 0)) return "anonymous";
	return strFuncName;
}

/** 
 * This method returns the stack trace string built by the constructor.
 */
function StackTrace_getStackTrace() {
	return this.strStackTrace;
}
