/**
 * Copyright (c) 2009, NVIDIA Corporation.  All rights reserved.
 *
 * THE INFORMATION CONTAINED HEREIN IS PROPRIETARY AND CONFIDENTIAL TO
 * NVIDIA, CORPORATION.  USE, REPRODUCTION OR DISCLOSURE TO ANY THIRD PARTY
 * IS SUBJECT TO WRITTEN PRE-APPROVAL BY NVIDIA, CORPORATION.
 * 
 * @author shaworth
 * @revision 2
 **/

function transformer(xslt)
{
  this.xmls = null;
  this.processor = null;
  this.xslt = null;
  this.xslt_file = xslt;
  this.src_doc = null;

  this.init = function()
  {
    if (window.ActiveXObject)
    {
      this.xslt = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
      //this.xslt = new ActiveXObject("Microsoft.XMLDOM");
      this.xslt.async = false;
      this.xslt.load(this.xslt_file);
      var template = new ActiveXObject("MSXML2.XSLTemplate");
      template.stylesheet = this.xslt;
      this.processor = template.createProcessor();
    }
    else
    {
      this.processor = new XSLTProcessor();
      this.xmls = new XMLSerializer();
    }
  }
  
  this.setXSLFile = function(response) {
	  this.xslt = response.responseXML;
  }

  this.init();

  this.transform = function(xmlDoc, xmlDocObject)
  {
	var xmlString = xml2Str(xmlDocObject);
	var output = '';
	var result;
    if (window.ActiveXObject)
    {
      this.src_doc = new ActiveXObject("Microsoft.XMLDOM");
      if (parseInt(navigator.userAgent.substring (navigator.userAgent.indexOf("MSIE")+5))==6) {
      	this.src_doc.loadXML(xmlDoc);
      } else {
      	this.src_doc.loadXML(xmlString);
      }
      this.processor.input = this.src_doc;
      this.processor.transform();
      output = this.processor.output;
    }
    else
    {
      var parser = new DOMParser();
      if (typeof this.processor.transformDocument == 'function') { // Gecko
    	  new Ajax.Request(this.xslt_file,
    		  {
    	  		method: 'get',
    	  		asynchronous: false,
    	  		onSuccess: function(response) {
		  			this.xslt = response.responseXML;
		  			this.processor = new XSLTProcessor();
		  			this.processor.importStylesheet(response.responseXML);
		  			var result = this.processor.transformToDocument(xmlDocObject);
		  			this.xmls = new XMLSerializer();
		  			output = this.xmls.serializeToString(result);
	  			}
    		  }
          );
      } else { // Webkit
    	  new Ajax.Request(this.xslt_file,
    		  {
    	  		method: 'get',
    	  		asynchronous: false,
    	  		onSuccess: function(response) {
		  			this.xslt = response.responseXML;
		  			this.processor = new XSLTProcessor();
		  			this.processor.importStylesheet(response.responseXML);
		  			var result = this.processor.transformToFragment(xmlDocObject, document);
		  			this.xmls = new XMLSerializer();
		  			output = this.xmls.serializeToString(result);
	  			}
    		  }
          );
      }
    }
    return output;
  }
  
  function xml2Str(xmlNode)
  {
    try {
      return (new XMLSerializer()).serializeToString(xmlNode);
    }
    catch (e) {
      try {
        return xmlNode.xml;
      }
      catch (e)
      {
    	  
      }
    }
    return false;
  }
}