traceHTMLfile = 'fs_webkit/trace.html';
traceContent = '';
traceWindow = 0;
refresh = true;
traceOn = true;
function trace(msg)
{
	if (traceWindow){
		traceWindow.showMessage(msg);
	} else {
		window.setTimeout('setupTrace("'+msg+'")',1001); // wait long enough for the child to report back, if its there.
	}
}

function setupTrace(msg){
	if (traceWindow){
		traceWindow.showMessage(msg);
	} else {
		traceContent += msg + '<br/>';
		traceWindow = window.open('','Trace_Window','width=300, height=300, resizable=yes');
		if (!traceWindow){
			// THE POPUP DIDN'T OPEN
			showWarning();
		} else {
			createPopupContents();
		}
	}
}

documentLoaded = false
function showWarning(){
	if (documentLoaded){
		triggerWarning();
	} else {
		window.onload.actions.push(triggerWarning);
	}
}


$(document).ready(function(){
	documentLoaded = true;
});


function triggerWarning(){
	info_window = document.getElementById('fospher_info_container');
	if (info_window == null){
		info_window = document.createElement("div");
		info_window.id = "fospher_info_container";
		document.body.appendChild(info_window);
		info_window.innerHTML = "Hello!";
		info_window.style.position = "absolute";
		info_window.style.width = "300px;";
		info_window.style.backgroundColor = "white";
		info_window.style.border="3px solid red";
		info_window.style.padding="20px";
		info_window.style.left = "50px";
		info_window.style.top = "50px";
	}
	info_window.innerHTML = '<p>The trace function needs to open a popup. <a href="#" onclick="circumventPopupBlocker();return false;">Open the Trace Window</a></p>';
	info_window.style.display="block";
}

function circumventPopupBlocker(){
	traceWindow = window.open('','Trace_Window','width=300, height=300, resizable=yes');
	if (traceWindow){
		window.setTimeout("createPopupContents();",500);
	}
	info_window.style.display="none";
}

function createPopupContents(){
	traceWindow.document.write(windowContents);
}


/* UTILITY FUNCTIONS */

function evaluateJavascript(theFunction){
	eval(theFunction);
}


/* WINDOW CODE */

var windowContents='';
windowContents+='<?xml version="1.0" encoding="iso-8859-1"?>\n';
windowContents+='<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 windowContentsict\/\/EN"\n';
windowContents+='        "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-windowContentsict.dtd">\n';
windowContents+='<html xmlns="http:\/\/www.w3.org\/1999\/xhtml" xml:lang="en" lang="en">\n';
windowContents+='<head>\n';
windowContents+='	<meta http-equiv="content-type" content="text\/html; charset=iso-8859-1" \/>\n';
windowContents+='	<title>Trace<\/title>\n';
windowContents+='	<script type="text\/javascript">\n';
windowContents+='		function clearContent(){\n';
windowContents+='			myDiv = document.getElementById("content");\n';
windowContents+='			myDiv.innerHTML = "";\n';
windowContents+='		};\n';
windowContents+='		\n';
windowContents+='		function showMessage(msg){\n';
windowContents+='			if (opener.traceOn){\n';
windowContents+='				if (opener.refresh){\n';
windowContents+='					if(msg!="clear"){\n';
windowContents+='						msg = msg+\'<hr\/>\';\n';
windowContents+='					}\n';
windowContents+='					opener.refresh = false;\n';
windowContents+='				}\n';
windowContents+='			}\n';
windowContents+='			myDiv = document.getElementById("content");\n';
windowContents+='			if(msg=="clear")\n';
windowContents+='				myDiv.innerHTML = ""; \n';
windowContents+='			else \n';
windowContents+='				myDiv.innerHTML = msg + "<br\/>" + myDiv.innerHTML;\n';
windowContents+='		};\n';
windowContents+='		\n';
windowContents+='		function checkForData(){\n';
windowContents+='			if (opener){\n';
windowContents+='				opener.traceWindow=window;\n';
windowContents+='				if (opener.traceContent){\n';
windowContents+='					showMessage(opener.traceContent);\n';
windowContents+='					opener.traceContent = \'\';\n';
windowContents+='				}\n';
windowContents+='			}\n';
windowContents+='		}\n';
windowContents+='		window.setInterval("checkForData()",1000);\n';
windowContents+='		function investigateVariable(myForm){\n';
windowContents+='			showMessage(myForm.investigate.value+" = "+eval("opener."+myForm.investigate.value)+"\\"");\n';
windowContents+='		};\n';
windowContents+='		\n';
windowContents+='		function evaluateJavascript(myForm){\n';
windowContents+='			opener.evaluateJavascript(myForm.evaluate.value);\n';
windowContents+='		};\n';
windowContents+='		\n';
windowContents+='	<\/script>\n';
windowContents+='	<style type="text\/css">\n';
windowContents+='		form fieldset{\n';
windowContents+='			border:1px solid black;\n';
windowContents+='			margin:10px; padding: 2px 5px;\n';
windowContents+='		}\n';
windowContents+='		\n';
windowContents+='		div#content{\n';
windowContents+='			border:1px solid black;\n';
windowContents+='			padding:10px;\n';
windowContents+='			margin:10px;\n';
windowContents+='			height:115px;\n';
windowContents+='			overflow:auto;\n';
windowContents+='		}\n';
windowContents+='	<\/style>\n';
windowContents+='<\/head>\n';
windowContents+='<body>\n';
windowContents+='	<a href="#" onclick="clearContent();return false;">Clear<\/a>\n';
windowContents+='	<div id="content">\n';
windowContents+='	\n';
windowContents+='	<\/div>\n';
windowContents+='	<form onsubmit="investigateVariable(this);return false;">\n';
windowContents+='		<fieldset>\n';
windowContents+='			<legend>Investigate<\/legend>\n';
windowContents+='			<input type="text" size="20" name="investigate" id="investigate" value="Variable"\/>\n';
windowContents+='			<input type="submit" value=">"\/>\n';
windowContents+='		<\/fieldset>\n';
windowContents+='	<\/form>\n';
windowContents+='	<form onsubmit="evaluateJavascript(this);return false;">\n';
windowContents+='		<fieldset>\n';
windowContents+='			<legend>Evaluate<\/legend>\n';
windowContents+='			<input type="text" size="20" name="evaluate" id="investigate" value="Eval"\/>\n';
windowContents+='			<input type="submit" value=">"\/>\n';
windowContents+='		<\/fieldset>\n';
windowContents+='	<\/form>\n';
windowContents+='<\/body>\n';
windowContents+='<\/html>\n';

