if(!commonspot)
	var commonspot = {};

/*
 * commonspot.exception: exceptions 
 */
commonspot.exception = {};


/**
 * commonspot.exception.BaseClass: base class for commonspot exceptions
 * @param int level - one of the error level constants defined in commonspot.exception
 * 	intended to separate classes of errors that require fundamentally different handling
 * @param string text - explanatory text to tell user or whatever
 * 	optional when handler just branches on value of code
 * @param {int} code - specific identifier for the error
 * 	intended mainly for command-specific errors, where the handler will branch on it
 * 	it's *not* intended that we have a huge library of codes for every error, just those
 * EXAMPLE:
try
{
  throw new commonspot.exception.FatalCommandError('something went wrong', 99);
}
catch(e)
{
  console.info(e)
}
 */
commonspot.exception.BaseClass = function(level, text, code, consoleAlertMethod)
{
	var self = this;
	
	this.level = level;
	this.text = text;
	this.code = code;
	
	// NEEDSWORK: this is a temporary hack until we catch these for real...
	var msg = '';
	if(code)
		msg += 'Code: ' + code + '\n\n';
	if(text)
		msg += text + '\n\n';
	if(msg == '')
		msg = 'An error without a message or error code was thrown. No further information is available.\n\n';
	msg = 'We\'re sorry, an error has occurred.\n\n' + msg;

	commonspot.dialog.client.alert(msg, {title: 'Error'});
};


/*
 * error level constants
 */
commonspot.exception.FATAL_CLIENT_UI_ERROR				= 0; // client UI is confused, needs to reload
commonspot.exception.FATAL_COMMAND_COLLECTION_ERROR	= 1; // entire cmd colection needs to be discarded (not logged in etc)
commonspot.exception.FATAL_COMMAND_ERROR					= 2; // this cmd needs to be disarded (server side error, etc)
commonspot.exception.COMMAND_SPECIFIC_EXCEPTION			= 3; // exception that cmd handler is expected to handle
commonspot.exception.USER_NOTIFICATION						= 4; // tell user
commonspot.exception.VALIDATION_EXCEPTION					= 5; // validation exception


/*
 * specific error classes
 */

/*
 * commonspot.exception.FatalClientUIError
 * a problem so bad that the whole clientUI should reload
 */
commonspot.exception.FatalClientUIError = function(text, code)
{
	commonspot.exception.BaseClass.call(this, commonspot.exception.FATAL_CLIENT_UI_ERROR, text, code, (typeof console == 'undefined' || typeof console.error == 'undefined') ? null : console.error);
};

/*
 * commonspot.exception.FatalCommandCollectionError
 * whole command collection response is trashed or missing; ex: not logged in
 */
commonspot.exception.FatalCommandCollectionError = function(text, code)
{
	commonspot.exception.BaseClass.call(this, commonspot.exception.FATAL_COMMAND_COLLECTION_ERROR, text, code, (typeof console == 'undefined' || typeof console.error == 'undefined') ? null : console.error);
};

/*
 * commonspot.exception.FatalCommandError
 * this cmd is trashed; ex: server side error
 */
commonspot.exception.FatalCommandError = function(text, code)
{
	commonspot.exception.BaseClass.call(this, commonspot.exception.FATAL_COMMAND_ERROR, text, code, (typeof console == 'undefined' || typeof console.error == 'undefined') ? null : console.error);
};

/*
 * commonspot.exception.CommandSpecificException
 * exception expected to be handled by this cmd's response handler
 * not neccessarily an error; ex: page locked by another user, handler will branch on that
 */
commonspot.exception.CommandSpecificException = function(text, code)
{
	commonspot.exception.BaseClass.call(this, commonspot.exception.COMMAND_SPECIFIC_EXCEPTION, text, code);
};

/*
 * commonspot.exception.UserNotification
 * tell the user something, not necessarily an error
 */
commonspot.exception.UserNotification = function(text, code)
{
	commonspot.exception.BaseClass.call(this, commonspot.exception.USER_NOTIFICATION, text, code);
};
