/**
* @fileOverview All XML related functions and constructors
* @name XML JS Library
*/

/**
* @namespace Contains all XML related functions and constructors
*/
var fsXml = {
    /**
    * load an xml file of the given string
    * @param {string} sFile The path to the XML file to be parsed
    * @returns {object} An XML Document
    */
    open: function(sFile) {
        var xmlDoc = null;
        if (document.implementation && document.implementation.createDocument)
        { /* Firefox/Safari/Chrome */
            var xhr = new XMLHttpRequest();
            xhr.open("GET", sFile,false);
            xhr.setRequestHeader("Content-Type","text/xml");
            xhr.send(null);
            xmlDoc = xhr.responseXML;
        }
        else if (window.ActiveXObject)
        { /* IE */
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

        }
        else
        {
           var xhr = new XMLHttpRequest();
           xhr.open("GET", fs.getContainingFolder() + '/xmls/languages/en/labels.xml',false);
           xhr.setRequestHeader("Content-Type","text/xml");
           xhr.send(null);
           xmlDoc = xhr.responseXML;
        }
        xmlDoc.async = false;
        try {
            xmlDoc.load(sFile);
        } catch(e) {
            //alert(e);
        }

        return xmlDoc;
    }
    ,
    /**
    * Retrieve an array of node that matches the supplied xpath string
    * @param {object} oXmlDoc The XML document to be accessed
    * @param {string} sXpath The string of the xpath
    * @return {array} An array of nodes that matched the xpath string, empty array if nothing is matched
    */
    getElementsByXpath: function(oXmlDoc, sXpath) {
        if (window.ActiveXObject) {/* IE */
            oXmlDoc.setProperty("SelectionLanguage","XPath");
            var aNode = null;

            if (aNode = oXmlDoc.selectNodes(sXpath)) {
                return aNode;
            } else {
                return [];
            }
        } else if (document.implementation && document.implementation.createDocument) {/* Firefox */
            var oXpathResult = oXmlDoc.evaluate(sXpath, oXmlDoc, null, XPathResult.ANY_TYPE, null);
            var oNode = oXpathResult.iterateNext();
            var aNode = [];
            while(oNode){
                aNode.push(oNode);
                oNode = oXpathResult.iterateNext();
            }

            return aNode;
        }
    }
    ,
    /**
    * Retrieve a single node that matches the supplied xpath string
    * @param {object} oXmlDoc1 The XML document to be accessed
    * @param {string} sXpath The string of the xpath
    * @return {object} The node that matched the xpath string, null if nothing is selected
    */
    getElementByXpath: function(oXmlDoc,sXpath) {
        if (window.ActiveXObject) {/* IE */
            if (oXmlDoc.firstChild == null) {
                throw new fsXml.error.EmptyXmlError();
            }
            oXmlDoc.setProperty("SelectionLanguage","XPath");
            var oSingleNode = oXmlDoc.selectSingleNode(sXpath);
            return oSingleNode;

        } else if (document.implementation && document.implementation.createDocument) {/* Firefox */
            try {
                var nsResolver = oXmlDoc.createNSResolver( oXmlDoc.ownerDocument == null ? oXmlDoc.documentElement : oXmlDoc.ownerDocument.documentElement );
                var oXpathResult = oXmlDoc.evaluate(sXpath, oXmlDoc, nsResolver, XPathResult.ANY_TYPE, null);
                var oNode = oXpathResult.iterateNext();
                return oNode;
            } catch(e) {
                if (oXmlDoc.firstChild == null) {
                    throw new fsXml.error.EmptyXmlError();
                }
            }
        }
    }
    ,
    getTextByXpath: function(oXmlDoc,sXpath) {
        var oNode = fsXml.getElementByXpath(oXmlDoc,sXpath);
        if(oNode.nodeValue != ''){
            return oNode.nodeValue;
        } else if (oNode.textContent != '') {
            return oNode.textContent;
        }
    }
    ,
    error: {
        EmptyXmlError:function() {
            this.name = 'EmptyXmlError';
            this.message = 'The first child of the XML object is null, possibly a reference to a non-existing XML document.';

        }
    }
};
