
// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
    try {
        if (window.XMLHttpRequest) {
            var req = new XMLHttpRequest();
            // some versions of Moz do not support the readyState property
            // and the onreadystate event so we patch it!
            if (req.readyState == null) {
                req.readyState = 1;
                req.addEventListener("load", function () {
                    req.readyState = 4;
                    if (typeof req.onreadystatechange == "function")
                        req.onreadystatechange();
                }, false);
            }
            
            return req;
        }
        if (window.ActiveXObject) {
            return new ActiveXObject("MSXML2.XmlHttp");
        }
    }
    catch (ex) { }
    // fell through
    //throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() {}

XmlDocument.create = function () {
    try {
        // DOM2
        if (document.implementation && document.implementation.createDocument) {
            var doc = document.implementation.createDocument("", "", null);
            
            // some versions of Moz do not support the readyState property
            // and the onreadystate event so we patch it!
            if (doc.readyState == null) {
                doc.readyState = 1;
                doc.addEventListener("load", function () {
                    doc.readyState = 4;
                    if (typeof doc.onreadystatechange == "function")
                        doc.onreadystatechange();
                }, false);
            }
            return doc;
        }
        if (window.ActiveXObject) {
            return new ActiveXObject("MSXML2.DOMDocument");
        }
    }
    catch (ex) {}
    //throw new Error("Your browser does not support XmlDocument objects");
};

XmlDocument.transform = function ( xmlDoc, xsltDoc, obj ) {
    try
    {
        // DOM2
        if( document.implementation && document.implementation.createDocument ) {
            var xslt = new XSLTProcessor();
            xslt.importStylesheet( xsltDoc );
            var resultDoc = xslt.transformToFragment(xmlDoc,document);
            var xmls = new XMLSerializer();
            obj.innerHTML = '';
            obj.appendChild( resultDoc );
        }
        if( window.ActiveXObject ) {
            obj.innerHTML = xmlDoc.transformNode( xsltDoc );
        }
    }
    catch (ex){
        //throw new Error("Your browser does not support XmlDocument objects");
    }
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
    window.XMLSerializer &&
    window.Node && Node.prototype && Node.prototype.__defineGetter__) {

    // XMLDocument did not extend the Document interface in some versions
    // of Mozilla. Extend both!
    //XMLDocument.prototype.loadXML = 
    Document.prototype.loadXML = function (s) {
        // parse the string to a new doc    
        var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
        
        // remove all initial children
        while (this.hasChildNodes())
            this.removeChild(this.lastChild);
            
        // insert and import nodes
        for (var i = 0; i < doc2.childNodes.length; i++) {
            this.appendChild(this.importNode(doc2.childNodes[i], true));
        }
    };
    
    
    /*
     * xml getter
     *
     * This serializes the DOM tree to an XML String
     *
     * Usage: var sXml = oNode.xml
     *
     */
    // XMLDocument did not extend the Document interface in some versions
    // of Mozilla. Extend both!
    /*
    XMLDocument.prototype.__defineGetter__("xml", function () {
        return (new XMLSerializer()).serializeToString(this);
    });
    */
    Document.prototype.__defineGetter__("xml", function () {
        return (new XMLSerializer()).serializeToString(this);
    });
}

    function insertContent( xmlUrl, xsltUrl, objName, loadingText, time, language ) {
        var xmlDoc;
        var xsltDoc;
        var obj = document.getElementById( objName );   

        //display warning( xml is loading )
        if( obj && loadingText && loadingText != "" )
            obj.innerHTML = loadingText;

        var xmlHttp = XmlHttp.create();
        xmlHttp.open( "GET", xmlUrl, true );
        xmlHttp.onreadystatechange = function () {
            if( xmlHttp.readyState == 4 ) {
                if ( xmlHttp.responseXML == null || xmlHttp.responseXML.xml == "" ) {
                    if ( language == 'de' ) {
                        obj.innerHTML = 'Fehler beim Laden des RSS Feed von diesem Speicherort: `' + xmlHttp.responseText + '`.<br>Bitte versuchen Sie, die Seite neu zu laden.';
                    } else {
                        obj.innerHTML = 'Error loading the rss feed from this location: `' + xmlHttp.responseText + '`.<br>Try to reload the page.';
                    }
                } else {
                    xmlDoc = xmlHttp.responseXML;
                    xsltDoc = XmlDocument.create();
                    xsltDoc.load( xsltUrl );
                    xsltDoc.onreadystatechange = function () {
                        if( xmlHttp.readyState == 4 ) {
                                XmlDocument.transform( xmlDoc, xsltDoc, obj );
                                initMenus( obj );
                                //update the content
                                if( time && time != 0 ) 
                                    window.setTimeout( "insertContent( '"+xmlUrl+"','" + xsltUrl + "','" + objName + "','" + loadingText + "'," + time + ")",time * 1000 ); 
                        }
                    }
                }
            }
        }
        xmlHttp.send( null );
    }

/*
    Expandable Listmenu Script
    Author : Daniel Nolan
    http://www.bleedingego.co.uk/webdev.php
*/

function initMenus( obj ) {
    if (!document.getElementsByTagName) return;
    
    var aMenus = obj.getElementsByTagName("LI");
    for( var i = 0; i < aMenus.length; i++ ) {
        var mclass = aMenus[i].className;
        if( typeof(mclass) != 'undefined' && mclass.indexOf("treenode") > -1 ) {
            var submenu = aMenus[i].childNodes;
            for (var j = 0; j < submenu.length; j++) {
                if (submenu[j].tagName == "A") {
                    
                    submenu[j].onclick = function() {
                        var node = this.nextSibling;
                                            
                        while (1) {
                            if (node != null) {
                                if (node.tagName == "UL") {
                                    var d = (node.style.display == "none")
                                    node.style.display = (d) ? "block" : "none";
                                    this.className = (d) ? "treeopen" : "treeclosed";
                                    return false;
                                }
                                node = node.nextSibling;
                            } else {
                                return false;
                            }
                        }
                        return false;
                    }
                    
                    submenu[j].className = (mclass.indexOf("open") > -1) ? "treeopen" : "treeclosed";
                }
                
                if (submenu[j].tagName == "UL")
                    submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
            }
        }
    }
}

