//debug helper class to control popup windows
var DebugHelper = function()
{
    this.Active = true;
    this.ShowException = true;
    this.ShowURL = false;
    this.ShowLastModified = false;
    this.ShowReferrer = false;
    this.VerboseMode = false;
    //reference to the popup window
    this.DebugWindow = null;
    this.CssStyleFile = new String("js/debug/debugWindow.css");
    this.WindowStyle = new String("left=0,top=800,width=1024,height=100,scrollbars=yes,status=no,resizable=yes");
    //no spaces to run correctly on internet explorer
    this.WindowName = new String("JavascriptDebugWindow");
    this.WindowTitle = new String("Swift Javascript Debug");
}




//method to show the debug window
DebugHelper.prototype.ShowWindow = function()
{
	try
	{
	    if( this.Active )
	    {
		    this.DebugWindow = window.open("", this.WindowName, this.WindowStyle);
	  	    this.DebugWindow.opener = window;
	  	    //open the document for writing
	  	    this.DebugWindow.document.open();
	  	    this.DebugWindow.document.write(
	    	    "<html><head><title>" + this.WindowTitle + "</title>\n" +
	    	    "<link rel='stylesheet' type='text/css' href='" + this.CssStyleFile + "' />\n" + 
	    	    "</head><body>\n" +
	    	    "<div id='renderSurface' style='width: 100%; height: 100%;'>"+
	    	    "</div></body></html>\n"
	        );
    	    this.DebugWindow.document.close();
	    }
	}
	catch(ex)
	{
		//ignore exception
	}
}		

//if the debug window exists, then write to it
DebugHelper.prototype.$Write = function(cssClass, message, url, lastModified, referrer) 
{
	try
	{
		if( this.Active )
		{
			if( this.DebugWindow && ! this.DebugWindow.closed )
			{
			
			    var msg = Date()  + " " + message;
			    
			    if( this.ShowURL && url != null )
			        msg += " at " + url;
			        
			    if( this.ShowLastModified && lastModified != null )
			        msg += " last modified in " + lastModified;

			    if( this.ShowReferrer && referrer != null )
			        msg += " referrer " + referrer;

		    	this.DebugWindow.document.getElementById("renderSurface").innerHTML = "<span class='" + cssClass + "'>" + msg + "<br></span>" + this.DebugWindow.document.getElementById("renderSurface").innerHTML;
		  	}
		}
	}
	catch(ex)
	{
		//ignore exception
	}
}

//write a message to debug window
DebugHelper.prototype.Message = function(message)
{
	try
	{
    	this.$Write("debugMessage", message, document.URL,document.lastModified, document.referrer);
	}
	catch(ex)
	{
		//ignore exception
	}
}

//same as debug, plus another style applyied
DebugHelper.prototype.Warn = function(message)
{
	try
	{
    	this.$Write("debugWarn", message, document.URL,document.lastModified, document.referrer);
	}
	catch(ex)
	{
		//ignore exception
	}
}

//same as debug, plus another style applyied
DebugHelper.prototype.FlashDebug = function(message)
{
	try
	{
    	this.$Write("debugFlash", message, document.URL,document.lastModified, document.referrer);
	}
	catch(ex)
	{
		//ignore exception
	}
}

//same as debug, plus a strong style applyied
DebugHelper.prototype.Exception = function(message)
{
	try
	{
	    if( this.ShowException )
	    {
    	    this.$Write("debugException", message, document.URL,document.lastModified, document.referrer);
    	}
	}
	catch(ex)
	{
		//ignore exception
	}
}

//if the debug window exists, then close it
DebugHelper.prototype.HideWindow = function() 
{
	try
	{
	    if( this.DebugWindow && !this.DebugWindow.closed )
	    {
		    this.DebugWindow.close();
    	    this.DebugWindow = null;
  	    }
	}
	catch(ex)
	{
		//ignore exception
	}
}

//create a global debug object
var debugHelper = new DebugHelper();
//you should show the window right here to get loading errors or sintax errors of other pages
//debugHelper.ShowWindow();

//catch generic errors also
function WindowOnError(msg, url, line)
{
	if( debugHelper )
	{
		debugHelper.Exception(msg);
	}
}
window.onerror = WindowOnError;

function _FUNC(f) 
{
	if(!g_debug) return "";
	var tmp = f.toString();
	var re = /(\s*function\s+)(\w+)(\s*\()/m;
	re.exec(tmp);
	return RegExp.$2;
}
