YAHOO.si.ErrorHandler = 
{
	container:{},
	onFeedbackReady: new YAHOO.util.CustomEvent(
		"feedbackready",
		this,
		true,
		YAHOO.util.CustomEvent.LIST),
	/**
	 * This creates a logger if YAHOO.si.debug is set to true by the developer.
	 * and always creates a jsErrorPanel for displaying errors to the user.
	 */
	setupFeedback:function()
	{
		try
		{
		    if(YAHOO.si.debug)
		    {
		        this.myLogReader = new YAHOO.widget.LogReader({verboseOutput:true});
		        this.myLogReader.hideCategory("info");
		    }
			if(!YAHOO.util.Dom.inDocument("errorPanelContainer"))
			{
				throw "The element 'errorPanelContainer' seems to missing from the document";
			}
			this.container = document.getElementById("errorPanelContainer");		
			this.init();
		}
		catch(e)
		{
			alert("There is a problem setting up the error feedback system:" + e);
		}
	},
	/**
	 * Creates the jsErrorPanel 
	 */
	init:function()
	{
		this.jsErrorPanel = new YAHOO.widget.Panel("jsErrorPanel", 
                        { constraintoviewport: true, 
                            fixedcenter: true, 
                            width: "400px", 
                            zIndex: 1,
                            visible:false});
    	this.jsErrorPanel.setHeader("JavaScript Error");
    	//this.jsErrorPanel.element.style.backgroundColor="pink";
    	this.jsErrorPanel.setBody("");
	    this.render();
	},
	/**
	 * Renders the jsErrorPanel and fires the onFeedbackReady event
	 * if successful.
	 */
	render:function()
	{
		try
		{
			this.jsErrorPanel.render(this.container);
		   	//this.jsErrorPanel.show();                        
	        this.ready = true;
			YAHOO.si.handleErrors = this.handleErrors;
		    this.onFeedbackReady.fire();
		}
		catch(e)
		{
			alert("Can't render the error panel " + e);
		}
	},
	displayErrors:function(err)
	{     
		var msg = "";
		if(typeof(err)==='string')
		{
			msg = err;
		}
		else
		{
			for(var obj in err)
			{
				if(obj!= "stack")
				{
					msg += obj + ":" + err[obj] + "<br>";				
				}
			}
		}
	    this.jsErrorPanel.setBody(msg);
	    this.jsErrorPanel.show();                        
	},
	handleErrors:function(systemError)
	{
	 	var s = "";
	    if(YAHOO.lang.isObject(systemError))
	    {
		    for(var obj in systemError)
		    {
		    	if(systemError[obj])
		    	{
			        s += obj + ":\n" + systemError[obj] + "\n";
		    	}
		    }
		    if(YAHOO.si.debug)
		    {
		       YAHOO.log(systemError,"error",s);
		    }
		    //Something obscure about timing here.
		    //If I put an alert or this timeout, the panel displays.
		    //Otherwise it doesn't.
		    window.setTimeout(function(){
		        YAHOO.si.ErrorHandler.displayErrors(systemError);                  
		    },0);	    	
	    }
	},
	toString:function()
	{
		return "YAHOO.si.ErrorHandler";
	}
};
YAHOO.si.Startup.onDataReady.subscribe(function()
{
	YAHOO.si.ErrorHandler.setupFeedback();
});
