/*
	XSL Transform JS Wrapper
	Copyright (C) 2006, 2007. Chonla.

	Version: 2.0
	Author: Chonla
	Update: 23 feb. 2007

	More infomation, visit: http://www.siamexperts.net/
*/

function $xslt()
{
	var _xslt = {
		transform: function(xmldoc, xsldoc) {
			var rtn = Try.these(
				// test for ie xmldom internal transformNode processor
				function() {return xmldoc.transformNode(xsldoc);},
				// mozilla xslt processor
				function() {
					var xsltp = new XSLTProcessor();
					var tempnode = document.createElement("div");
					xsltp.importStylesheet(xsldoc);
					tempnode.innerHTML = "";
					tempnode.appendChild(xsltp.transformToFragment(xmldoc, document));
					return tempnode.innerHTML;
				}
			);
			return rtn;
		},
		transformwrapper: function(xmldocfile, xmloptions, xsldocfile, xsloptions) {
			this.importxml(xmldocfile, xmloptions);
			this.importxsl(xsldocfile, xsloptions);
			return this.JustTransform();
		},
		importxml: function(xmldocfile, xmloptions) {
			var _xmlrequestoptions = {asynchronous:false};
			Object.extend(xmloptions, _xmlrequestoptions);
			var xmlrequest = new Ajax.Request(xmldocfile,xmloptions);	// prototype's Ajax.Request
			this._lastXML = xmlrequest.transport.responseXML;
		},
		importxsl: function(xsldocfile, xsloptions) {
			var _xmlrequestoptions = {asynchronous:false};
			Object.extend(xsloptions, _xmlrequestoptions);
			var xslrequest = new Ajax.Request(xsldocfile,xsloptions);	// prototype's Ajax.Request
			this._lastXSL = xslrequest.transport.responseXML;
		},
		resetxml: function() {
			this._lastXML = null;
		},
		resetxsl: function() {
			this._lastXSL = null;
		},
		justtransform: function() {
			return this.Transform(this._lastXML, this._lastXSL);
		},
		normalize_output: function(str) {
			var pattern = /<\?xml .*?\?>/i;
			str = str.replace(pattern, "");
			return str;
		},
		normalize_transform: function(xmldoc, xsldoc) {
			return this.normalize_output(this.transform(xmldoc, xsldoc));
		}
	};
	return _xslt;
}
