/**************************************************************************************/
/* XSL Transform Class                                                                */
/*                                                                                    */
/* targetElem     = target element (object), optional                                 */
/* cache          = cache array, optional                                             */
/* callBack       = callback function (can also be an array), optional                */
/* loadInfo       = loading info message, optional                                    */
/*                                                                                    */
/* Global variables:                                                                  */
/*                                                                                    */
/* xsltCallBack() = additional callback function, optional                            */
/**************************************************************************************/
function MdxXslTransform(targetElem, cache, callBack, loadInfo) {

/* Member variables *******************************************************************/

	this.targetElem = targetElem;
	this.cache = cache;
	this.callBack = callBack;
	this.loadInfo = loadInfo;
	this.xslPath = '';
	this.xmlPath = '';
	this.xsl = null;
	this.xml = null;
	this.mdxAjax = (typeof(MdxAjax) == 'function') ? new MdxAjax() : null;

/* XSL transform functions ************************************************************/

	/*
	  xslPath = path to XSLT stylesheet, optional
	  xmlPath = path to gateway, optional
	*/
	this.transform = function(xslPath, xmlPath) {
		if(!this.mdxAjax) {
			alert('MdxAjax class needed');
			return false;
		}
		if(this.loadInfo) this.write(this.loadInfo);
		var xmlInfo = '';
		var url = '';
		this.xslPath = xslPath;
		this.xmlPath = xmlPath;

		if(this.cache) {
			if(xslPath && xslPath == this.cache.xslPath) {
				xslPath = '';
				if(xmlPath && xmlPath == this.cache.xmlPath) xmlPath = '';
			}
			else this.cache.xslPath = xslPath;
		}
		if(xmlPath) {
			if(this.cache) {
				this.cache.xmlPath = xmlPath;
				xmlInfo += '[to cache] ';
			}
			url = xmlPath + '&xsl=' + this.xslPath;
			xmlInfo += '<a href="' + url + '" target="_blank">' + url.replace(/&/g, '&amp;') + '</a>';
			this.mdxAjax.makeRequest(url, this.requestHandler.bind(this));
		}
		else if(this.cache) {
			this.xml = this.cache.xml;
			xmlInfo = '[from cache]';
			this.viewOutput();
		}
		return this.infoPrefix(xmlInfo);
	}

	this.requestHandler = function() {
		this.xml = this.mdxAjax.request.responseText;
		if(this.cache) this.cache.xml = this.xml;
		this.viewOutput();
	}

	/*
	  output = HTML output
	*/
	this.write = function(output) {
		if(this.targetElem) {
			if(this.targetElem.closed == false) {
				this.targetElem.document.write(output);
				this.targetElem.document.close();
			}
			else {
				this.targetElem.innerHTML = output;
				this.targetElem.scrollTop = 0;
			}
		}
		else document.write(output);
	}

	this.viewOutput = function() {
		var output = this.xml;
		if(output == '') output = 'ERROR';
		this.write(output);

		if(this.callBack) {
			if(typeof(this.callBack) == 'object') {
				for(var i = 0; i < this.callBack.length; i++) {
					this.execCallBack(this.callBack[i]);
				}
			}
			else this.execCallBack(this.callBack);
		}
		if(typeof(xsltCallBack) == 'function') {
			xsltCallBack(this);
		}
	}

	this.execCallBack = function(callBack) {
		if(typeof(callBack) == 'function') {
			callBack();
		}
	}

	/*
	  msg = info message
	*/
	this.infoPrefix = function(msg) {
		var info = '';

		if(this.targetElem) {
			if(this.targetElem.id) info += this.targetElem.id;
			else if(this.targetElem.name) info += this.targetElem.name;
		}
		info += '&gt; ' + msg;
		return info;
	}
}
