/*
*	Matelli: formation, développement et conseil en informatique
*	http://www.matelli.fr
*
*	contact@matelli.fr
*/

Utils.Browser = {
    IE:     !!(window.attachEvent && !window.opera),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/),
	Firefox2: navigator.userAgent.indexOf("Firefox/2") > -1 && navigator.userAgent.indexOf('KHTML') == -1
};

if(Utils.Browser.WebKit) navigator.onLine = true;

if(!Utils.Browser.IE)
{
	NodeList = function (i){
		var index = -1;
	    this.length = i;

		this.expr = "";

		this.item = function(i) {
	        return (i < 0 || i >= this.length)?null:this[i];
	    };

		this.nextNode = function()
		{
			if(this.length-1 == index)
				return null;
			else
			{
				index++;
				return this[index];
			}
		}

		this.reset = function()
		{
			index = -1;
		}
	};
	NodeList.prototype = [];
	NodeList.prototype.constructor = Array;

	Document.prototype.loadXML = function(xmlStr)
	{
		if(this.xml == "")
			this.appendChild(((new DOMParser()).parseFromString(xmlStr, "text/xml")).documentElement.cloneNode(true));
		else
			this.replaceChild(((new DOMParser()).parseFromString(xmlStr, "text/xml")).documentElement.cloneNode(true), this.firstChild);

		//TO-DO - test success
		return true;
	}

	Node.prototype.__defineGetter__("baseName", function() { return this.localName; });
	Node.prototype.__defineGetter__('text', function() { return this.textContent.replace(/[\f\n\r\t]*/, ""); });
	Node.prototype.__defineSetter__('text', function(v) { this.textContent = v; });
	Node.prototype.__defineGetter__('xml', function() { return (new XMLSerializer()).serializeToString(this); });

	Node.prototype.selectSingleNode = function(query)
	{
		var doc = (this.ownerDocument)?this.ownerDocument:this;
		var ns = doc.createNSResolver(doc.documentElement);

		var result = doc.evaluate(query, this, ns, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

	    //if ((new XMLSerializer()).serializeToString(result.singleNodeValue) == "") return null;

		return result.singleNodeValue;
	}

	Node.prototype.selectNodes = function(query)
	{
		var doc = (this.ownerDocument)?this.ownerDocument:this;
		var ns = doc.createNSResolver(doc.documentElement);

		var result = doc.evaluate(query, this, ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var nodeList = new NodeList(result.snapshotLength);

		for(var i=0; i<result.snapshotLength; i++)
			nodeList[i] = result.snapshotItem(i);

		return nodeList;
	}

	Node.prototype.transformNode = function(xslObj)
	{
		if(Utils.Browser.Firefox2 && document.documentURI.indexOf("#") > -1)
		{
			// TODO: test with network latency

			var xslObjTmp = window.frames.garbageFrame.document.implementation.createDocument("", "", null);
			xslObjTmp.appendChild(window.frames.garbageFrame.document.importNode(xslObj.firstChild, true));
			xslObj = xslObjTmp;
		}

		var p = new XSLTProcessor();
		p.importStylesheet(xslObj);

		return (new XMLSerializer()).serializeToString(p.transformToFragment(this, document));
	}

	Node.prototype.transformNodeToObject = function(xslObj, outputObj)
	{
		var p = new XSLTProcessor();
		p.importStylesheet(xslObj);

		outputObj.appendChild(p.transformToFragment(this, document).firstChild);
	}
}
