/*
Summary:  BjaxXlstEngine is a library of Bjax functions.
          For use when needing initiating cross browser XSLT transformations.
Remarks:  Bjax is Ajax plus XSLT, including XPATH, functions
          initiated from browser script.
Version:  0.1
Author:   John Bentley (www.softmake.com.au)

*/
function Bjaxer () {
  if (typeof XSLTProcessor != "undefined" ) {
    var W3CProcessor = new XSLTProcessor();
  }

  /* Class Scoped Variables */
  var Self = this;

  var AjaxSourceXml = new Ajaxer();
  var IsSourceXmlDomDocLoaded = false;

  var AjaxXslt = new Ajaxer();
  var IsXsltXmlDomDocLoaded = false;
  var XsltTemplate = null;
  var XsltNamespaces = "";
/*
  var Parameter = function (name, value) {
    this.name = name;
    this.value = value;
  };

  var Parameters = new Array();

   Usage Example:
    Bjax.setParameter("MyParam", "MyValue"); 
  this.setParameter = function (name, value) {
    Parameters[Parameters.length] = new Parameter(name, value);
  };*/

  function loadSource (sourceUrlOrXmlDomDoc) {
    IsSourceXmlDomDocLoaded = false;
    AjaxSourceXml.load(sourceUrlOrXmlDomDoc, onSourceSuccess);
  }

  // Callback function from AjaxEngine.
  function onSourceSuccess(){
    //alert("onXsltSuccess");
    //if (AjaxSourceXml.responseXML) {
      Self.sourceXmlDomDoc = AjaxSourceXml.responseXML;
      //out("XML Loaded");
      IsSourceXmlDomDocLoaded = true;
      onFileLoaded();
    //}
  }

  function loadXslt(xsltUrlOrXmlDomDoc, xsltNamespaces) {
      IsXsltXmlDomDocLoaded = false;
      // true means isStylesheet (Got to handle that specially)
      AjaxXslt.load(xsltUrlOrXmlDomDoc, onXsltSuccess, true);
  }

  // Callback function from AjaxEngine.
  function onXsltSuccess() {
      Self.xsltXmlDomDoc = AjaxXslt.responseXML;
      Self.xsltXmlDomDoc.setProperty("SelectionLanguage", "XPath"); // For IE 
      Self.xsltXmlDomDoc.setProperty("SelectionNamespaces", XsltNamespaces);
      /*

      // Uses BjaxerXpath.js for Gecko.
      Self.xsltXmlDomDoc.setProperty("SelectionLanguage", "XPath"); // For IE 
      Self.xsltXmlDomDoc.setProperty("SelectionNamespaces", XsltNamespaces);
      
      var xpathExpression =  "//*[@jlb:id='filterNode']/@select";
      var newFilterAttributeValue = "catalog/book[genre = 'Romance']";
      var filterAttributeNode = Self.xsltXmlDomDoc.selectSingleNode(xpathExpression);
      filterAttributeNode.nodeValue =  newFilterAttributeValue; 
      */
      
      IsXsltXmlDomDocLoaded = true;
      Self.loadBasis = AjaxXslt.loadBasis;
      onFileLoaded();
  }
 
  function onFileLoaded() {
    //alert("onFileLoaded");
    if (IsSourceXmlDomDocLoaded && IsXsltXmlDomDocLoaded) {
      //out("onFileLoaded");
     // out("About to import stylesheeet ...");
      if (typeof XSLTProcessor != "undefined") {
        // Gecko
        W3CProcessor.importStylesheet(Self.xsltXmlDomDoc);
      } else if (window.ActiveXObject) {
        // IE
        // Self.xsltXmlDomDoc already loaded.
      } else {
        throw new Error("Your browser my not support XSLTProcessor Not ActiveXObjects. Try Mozilla Firefox or Microsoft Internet Explorer.");
      }
      //out("Importing Stylesheet complete.");
      // If the author has passed a callback function then call it now.
      if (Self.onPrepComplete) {
        Self.onPrepComplete();
      }
    }
    // else we wait for the other file to load.
  }

  /*************
    Public
    *************/
  this.sourceXmlDomDoc = null;
  this.xsltXmlDomDoc = null;
  this.onPrepComplete = null;  // A callback function.
  this.loadBasis = "";

  this.prepareTransform = function (sourceUrlOrXmlDomDoc, xsltUrlOrXmlDomDoc, onPrepComplete, xsltNamespaces) {
    // Just assign the Call back function. Execute it onFileLoaded.
    
    if (onPrepComplete) {
      this.onPrepComplete = onPrepComplete;
    }
    XsltNamespaces = xsltNamespaces;
    loadSource(sourceUrlOrXmlDomDoc);
    loadXslt(xsltUrlOrXmlDomDoc);
  };

  this.getTransformedXmlDomDoc = function () {
    var resultXmlDomDoc = null;

    if (typeof XSLTProcessor != "undefined") {
      resultXmlDomDoc = W3CProcessor.transformToDocument(this.sourceXmlDomDoc);
    } else if (typeof XSLTProcessor == "undefined" && typeof ActiveXObject != "undefined") {
      // eg IE 6.
      resultXmlDomDoc = new ActiveXObject(MSProgId);
      resultXmlDomDoc.async = false;
      resultXmlDomDoc.validateOnParse = true;

      // Parse results into a result DOM Document.
      this.sourceXmlDomDoc.transformNodeToObject(this.xsltXmlDomDoc, resultXmlDomDoc);      

    } else {
      throw new Error("Your Browswer does not support the Transformation.");
    }
    return resultXmlDomDoc;
  };
  
  this.changeXsltAttributeNode = function(xpathExpression, newAttributeValue) { 
    Self.xsltXmlDomDoc.setProperty("SelectionNamespaces", XsltNamespaces);
    var filterAttributeNode = Self.xsltXmlDomDoc.selectSingleNode(xpathExpression);
    filterAttributeNode.nodeValue = newAttributeValue;  
  };
}

