/*
@author bibby <bibby@surfmerchants.com>
$Id$

Inspired by Knickers Errors, this javascript aims to provide better detail about things that go wrong.
Unlike Knickers, however, the first argument of your Error should be a "new Error()" - this gets you the backtrace ability.
You may optionally provide a message in that error, but one will likely be generated based on the type of error you call.

example use:
if(!foo)
	Error.IllegalArgument( new Error() , 'a number!' , foo);
*/


// include a backtrace?
Error.trace = true;



/*
Only members of Error should really be using this
show errors in the firebug console (TODO: or some place useful for non FB'ers?)
@param mixed (object Error , string)
@return void
*/
Error.log = function(err)
{
	// got firebug?
	if(typeof console == 'undefined')
	{
		// you have no firebug, and I'll have to figure something out for you
		//besides,
		alert(err.message || err);
	}
	else
		if(is(console,'object') && is(console.error,'function'))
		{
			console.error( err.message || err);
			if(Error.trace)
				console.trace(err);
		}
};




/*
Gets details like line number and filename from the error object
@param object Error
@return string
*/
Error.details = function(errorObj)
{
	return "\nLine "+errorObj.lineNumber + ' of ' + errorObj.fileName + ( errorObj.message=='' ? '' : "\n"+errorObj.message); 
}




/**
* Error for 'illegal argument' error types.  
* These types are meant to be used when a parameter of the wrong kind is passed to a function
* They can also be used when a function is operating on a variable that does not have the correct properties
@param object Error
@param string What the function was expecting
@param mixed Whatever the function received instead (message will show type and contents)
@return object Error
*/
Error.IllegalArgument=function(errorObj, expected,received)
{
	errorObj.message = 'Illegal Argument : Expected ' +expected+'. Received '+is(received,'type') + ' ( '+(is(received,'function')?'function()':received)+" )" + Error.details(errorObj);
	Error.log(  errorObj );
	return;
};




/**
* Error for 'illegal procedure' error types.  
* This type is meant to be used when a function is called "out of turn";
* that is, some other prerequisite function must be called first.
@param object Error
@param string message
@return object Error
*/
Error.IllegalProcedure=function(errorObj , message)
{
	errorObj.message = 'Illegal Procedure : '+message + Error.details(errorObj);
	Error.log( errorObj );
	return;
}

/**
Coax IE into telling you a little more about an object.
It wont tell you what it *is*, but it'll tell you what it *has*, and maybe that'll be enough
*/

/** // uncomment as needed
Error.dumpObj=function(obj)
{
	var d = "";
	for(var i in obj)
	{
		d+=i+' : '+ obj[i]+"\n";
	}
	alert(d);
}//*/

// CONSOLE FOR IE?
/**  // yeah right , bbby :P
if(typeof console == 'undefined')
{
	console = document.createElement('DIV');
	console.id = "debug_console";
	XUI.css(console,{
		position:'fixed',
		bottom:'0px',
		left:'0px',
		width:'100%',
		height:'80px',
		overflow:'scroll',
		border:'0',
		borderTop:'2px groove black'
	});
	
	console.log = function()
	{
		this.show();
		this.innerHMTL += "<br />"+XUtil.toArray(arguments).join("&nbsp;&nbsp;&nbsp;&nbsp;");
	};
	
	console.error = function()
	{
		this.show();
		this.innerHMTL +="<br />!!! "+XUtil.toArray(arguments).join("&nbsp;&nbsp;&nbsp;&nbsp;");
	};
	
	console.trace = function(){ return; };
	
	console.show = function()
	{
		if(!is(document.getElementById(this.id)))
		{
			document.body.appendChild(this);
			var close = document.createElement('SPAN');
			close.onclick = function(){ console.hide() };
			close.innerHTML = 'hide';
			XUI.css(close,
			{
				color:'red',
				cursor:'pointer'
			});
			
			this.appendChild(close);
		}
		
		this.style.display = 'block';
	};
	
	console.hide = function()
	{
		this.style.display = 'none';
	};
}
//*/
