'/*(c) Copyright 2007-2008 Slacker, Inc. 16935 West Bernardo Dr. San Diego, CA 92127 - All rights reserved.*/'
/**
 * (c) Copyright 2007 Slacker, Inc.
 * 16935 West Bernardo Dr.
 * San Diego, CA 92127
 * 
 * @author Daniel Baird
 */
/*
	Classes and methods to define custom error handling
*/
verifyNamespace("com.system.Error");

var LastError;
var ErrorTrace;

/**
 * Custom error class
* @constructor
* @param {Object} err {@link Error} object OR standard window.error object
* @param {String} file
* @param {Integer} line
* @param {Boolean} is Fatal
*/
function Error(err, file, line, isFatal) {
	this.version  = "$LastChangedRevision: 1323 $";
	this.isFatal	= isFatal;
	this.message	= err;
	this.line		= line;
	this.file		= file;
	this.StackTrace	= new StackTrace();
	
	HandleErrors(this);
}
/**
 * Stacktrace class.  Allows error to build their trace when the error uses this.
 * @constructor
*/
function StackTrace() {
	this.stack	= new Array();
}
/**
 * Add an error to the trace
 * @param {Error} {@link Error} Error
 */
StackTrace.prototype.add = function (Error) {
	this.stack[this.stack.length]	= Error;
}
/**
 * Print out the stack trace
 * @return {String}
 */
StackTrace.prototype.toString	= function () {
	var out	= new Array();
	var idx	= 0;
	var err;
	out[out.length]	= "Fatal Error - Stack Trace:";
	for (idx=0;idx<this.stack.length;idx++) {
		err	= this.stack[idx];
		out[out.length]	= err.message + " at " + err.file + "(" + err.line + ")";
	}
	return out.join("\n");
}
/**
 * Function to override window.onerror.  Assign window.onerror = HandleErrors to activate
*/
function HandleErrors (err, file, line) {
    require("com.debug.console"); // loads com.system.utils.
	
	// Set error object to start our error chain
	var isFatal	= err.isFatal;
	
	if (!ErrorTrace) {
		if (err.StackTrace) {
			ErrorTrace	= err;
		} else {
			ErrorTrace	= new Error(err, line, file);
		}
	} else {
		if (!err.StackTrace) {
			err	= new Error(err, line, file);
		}
	}
	
	ErrorTrace.StackTrace.add(err);
	
	if (com.debug && com.debug.console) {
	    com.debug.console.log("error","Error: isFatal(" + isFatal + ") - ", ErrorTrace);
	}
	
	LastError	= ErrorTrace;
	
	if (isFatal) {
		ErrorTrace	= null;
		if (Q_STRING && Q_STRING.get('debug')) {
    		if (confirm("An error has occured:\n" + LastError.StackTrace.toString() + 
	       "\nWould you like to debug?")) {
	           try {
     	           debugger;
	           } catch (e) {
	               
	           }
	       }
		} else {
		    alert ("A fatal error has occured.  The application needs to be restarted.");
		    clearWindowUnloadActions();
            location.reload();
		}
	} else {
	    ErrorTrace	= null;
	    if (Q_STRING && Q_STRING.get('debug')) {
	       if (confirm("An error has occured:\n" + LastError.StackTrace.toString() + 
	       "\nWould you like to debug?")) {
	           try {
     	           debugger;
	           } catch (e) {
	               
	           }
	       }
	    }
	}
	
	return true; // Supresses all browser JS alerts and lets us handle it.
}

com.system.Error	= Error;
registerPackage("com.system.Error");
