/*
 * Telemon: Ajax the Greater's father
 *
 * A Telemon instance provides some useful AJAX methods:
 * t = new Telemon(document);
 *
 * http = t.newHTTPRequest ()
 *   http => a new xmlHTTPRequest object, ready to use
 *
 * http = t.httpGet (uri, callback)
 *   http => an xmlHTTPRequest in asychronous wait state for a GET on 'uri'.
 *           When the GET is complete, callback(uri, http) will be called
 *           automatically.  The callback can operate on http.responseXML
 *           or http.responseText.
 *
 * doc = t.newDocument (uri, onload)
 *   doc => a new Document, initialized to asynchronously load the content of
 *          'uri'.  When the content download is complete, onload(uri, doc)
 *          will be called automatically.
 *
 * xml = t.performXSLTransform (xml, xsl)
 *   xml => an XML fragment containing the XSL transformation of 'xml'
 *          under 'xsl'.  Both 'xml' and 'xsl' should be valid XML documents.
 *
 * t.importXSL (xslURI, callback)
 *   Asynchronously loads an XSL document from 'xslURI' and caches the
 *   result within t's xsl cache.  callback(t, xslURI, xsl) will be
 *   called automatically when the XSL download is complete.
 *
 * xsl = t.getXSL (xslURI)
 *   If t.importXML(xslURI) has previously been called, obtains the XML
 *   content of that XSL document.
 *
 * t.privilegeInit ()
 *   In Mozilla browsers, allow signed javascript to download content
 *   from external sites.  Draws a dialog box for users to authorize
 *   this action, and only works with signed scripts.
 *
 */

function Telemon (document)
{
	this.document = document;
	this.xslCache = [];
	this.xslRequests  = 0;
	this.xslCompleted = 0;

	this.newHTTPRequest = function ()
	{
		var http;

		if (window.XMLHttpRequest) {
			/* gecko, safari */
			http = new XMLHttpRequest();
			/* work around bug in some older mozillae */
			if (http.overrideMimeType)
				http.overrideMimeType('text/xml');
		}
		else if (window.ActiveXObject) {
			/* ie */
			try {
				http = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}

		return http;
	};

	this.httpGet = function(uri, callback) {
		var http = this.newHTTPRequest();
		if (!http) {
			this.error = "cannot create new XMLHttpRequest object";
			return null;
		}

		http.onreadystatechange = function () {
			if (http.readyState != 4)
				return;
			if (callback) {
				callback(uri, http);
			}
		}

		try {
			http.open("GET", uri, true);
			http.send(null);
		}
		catch (e) {
			this.error = "caught " + e + " in httpGet()";
			return null;
		}

		return http;
	};

	this.newDocument = function (uri, onload)
	{
		var doc;

		/* mozilla */
		if (this.document.implementation &&
		    this.document.implementation.createDocument) {
			doc = this.document.implementation.createDocument("", "", null);
			if (uri) {
				doc.addEventListener("load",
						function () { return onload(uri, doc); },
						false);
				doc.load(uri);
			}
		}

		/* msie */
		else if (ActiveXObject) {
			doc = new ActiveXObject("Microsoft.XMLDOM");
			if (uri) {
				doc.onreadystatechange = function () {
					if (doc.readyState == 4) { return onload(uri, doc); }
					return false;
				};
				doc.load(uri);
			}
		}

		else if (uri) {
			return null;
		}

		return doc;
	};

	this.performXSLTransform = function (xml, xsl)		 
	{
		var xslt = new XSLTProcessor();

		var success = xslt.importStylesheet(xsl);
		var owner   = this.newDocument();

		var newxml = xslt.transformToFragment(xml, owner);
		return newxml;
	};

	this.importXSL = function (xslURI, callback)
	{
		var instance = this;
		++this.xslRequests;
		x = this.newDocument(xslURI, function (uri, xml) {
			++instance.xslCompleted;
			instance.xslCache[uri] = xml;
			if (callback)
				callback(instance, uri, xml);
		});
	};

	this.getXSL = function (xslURI)
	{
		return this.xslCache[xslURI];
	}


	// http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php
	// http://www.mozilla.org/projects/security/components/signed-scripts.html
	this.privilegeInit = function ()
	{

		if (netscape.security) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			}
			catch (e) {
				debug("Permission UniversalBrowserRead denied.");
			}
		}
	}

}
