var mcport = 19819;

function loadXML(xmlFile)
{
	var $xml = new XMLHttpRequest;
	$xml.open('GET', xmlFile, false);
	$xml.overrideMimeType('text/xml');
	$xml.send(null);
	var xml = $xml.responseXML;
	if (!xml) {
		alert("Unable to load "+xmlFile);
		return null;
	}
	return xml;
}

function transformXML(xmlDoc, xslDoc, element)
{
	var XSLT = new XSLTProcessor;
	XSLT.importStylesheet(xslDoc);
	var e = document.getElementById(element);
	if (e) {
		while (e.firstChild) e.removeChild(e.firstChild);
		e.appendChild(XSLT.transformToFragment(xmlDoc, document));
	}
}

var prefsXml;
var prefsXsl;

function init()
{
	document.getElementById("topfrm").setAttribute("src", "http://www.mediacoderhq.com/app/navbar.html");
	nsCommandLine = window.arguments[0].QueryInterface(Components.interfaces.nsICommandLine);
    if(nsCommandLine.length == 0) return;
	var uriToLoad = nsCommandLine.getArgument(0);
	const nsCI               = Components.interfaces;
	const nsIWebNavigation   = nsCI.nsIWebNavigation;
	document.getElementById("mcex").loadURI(uriToLoad, null, null);
	if (nsCommandLine.length > 2) {
		switch (nsCommandLine.getArgument(1)) {
		case "-title":
			document.getElementById("xulmain").setAttribute("title", nsCommandLine.getArgument(2));
			break;
		case "-port":
			mcport = parseInt(nsCommandLine.getArgument(2));
			break;
		}	
	}
	liteBrowserInit(uriToLoad);
	setTimeout("loadPrefs()", 100)
}

function loadPrefs()
{
	prefsXsl = loadXML("http://www.mcex.org/xslt/prefs.xsl");
	updatePrefs();
}

function updatePrefs()
{
	prefsXml = loadXML("http://127.0.0.1:" + mcport + "/prefs/prefs.xml?text&value&brief");
	transformXML(prefsXml, prefsXsl, "prefsBox");
	setTimeout("updatePrefs()", 5000)
}

function cleanup()
{
}

