var preparePage = 
{
    initialized: false
    , oldOnload: window.onload //remember previous onload handler

    //onload handler
    , onload: function()
    {
        if (preparePage.oldOnload)    //call previous onload handler if any existed
            preparePage.oldOnload();
        preparePage.init();           //initialize lwNavigation
    }
       
    //initialize some stuff in page
    , init: function()
    {
        if (preparePage.initialized)  //only if not yet initialized
            return;
            
        if (! document.getElementById) //keep out old browsers like Netscape 4.x/IE 4.0/... 
            return;                    //users of those browsers will see the full menu always

        var theURLspan = document.getElementById("url");
        
        if (theURLspan)
        {   
            var theText = document.createTextNode(self.location.href);
           
            while (theURLspan.firstChild)
                theURLspan.removeChild(theURLspan.firstChild);
            
            theURLspan.appendChild(theText);
        }

        if (document.all && ! window.opera) 
        //adjust table for IE 6 
        //if content is small, IE makes top rows too big, thus
        // set content to a height high enough to avoid the effect but not causing a scrollbar
        {
            var thetab = document.getElementById("layout");
            var thebalken = document.getElementById("layouttrenner");
            var themain = document.getElementById("layoutmain");
            if (thetab && thebalken && themain)
            {
                 themain.style.height = (thetab.offsetHeight - 115 - thebalken.firstChild.offsetHeight) + "px";
            }
        }
    }    
}

window.onload = preparePage.onload;

var lwNavigation = 
{
    theNav: null                //navigation element holding the ul tree
    , theCrumbs: null           //navigation element holding the breadcrumb navigation
    , initialized: false
    , oldOnload: window.onload //remember previous onload handler

    //onload handler
    , onload: function()
    {
        if (lwNavigation.oldOnload)    //call previous onload handler if any existed
            lwNavigation.oldOnload();
        lwNavigation.init();           //initialize lwNavigation
    }
       
    //initialize lwNavitagion    
    , init: function()
    {
        if (lwNavigation.initialized)  //only if not yet initialized
            return;
        
        if (! document.getElementById) //keep out old browsers like Netscape 4.x/IE 4.0/... 
            return;                    //users of those browsers will see the full menu always

        if (! (document.createElement        //check for existence of other functions to be used
                && document.createTextNode
           ))
            return;                          //if not all exist, don't start
            
        lwNavigation.theNav = document.getElementById("lwTreeNavigation"); //find lwTreeNavigation element
        
        if (! lwNavigation.theNav)           //terminate if no lwNavigation was found
            return;

        lwNavigation.theCrumbs = document.getElementById("lwBreadCrumbNavigation"); //find lwBreadCrumbNavigation
        
        var str = self.location.href;       //get url of document (to find the corresponding item in navigation tree

        var navilinks = lwNavigation.theNav.getElementsByTagName("a"); //get all links within lwNavigation

        var subnavs = lwNavigation.theNav.getElementsByTagName("ul"); //get all lists in lwNavigation
        for (var ii = 1; ii < subnavs.length; ii++)                   //hide them all except the top level
        {
            subnavs[ii].style.display = "none";
        }

        for (var ix = 0; ix < navilinks.length; ix++)               //search through links for current page
        {
            if (navilinks[ix].href == str)  //found the current document?
            {

                var elem = navilinks[ix];                           //display all parent ul lists
                var newElem; 
                var newElem2;
                while ((elem = elem.parentNode) != lwNavigation.theNav)
                {
                    if (elem.nodeName.toLowerCase() == "ul")
                    {
                        elem.style.display = "block";
                    }
                }
                
                elem = navilinks[ix];                              //find innermost li around current link
                while ((elem = elem.parentNode).nodeName.toLowerCase() != "li")
                {
                    ; //nothing in loop body 
                }
                elem.className="current";                          //set its class to current
                
                subnavs = elem.getElementsByTagName("ul");         //search for first ul within current li
                if (subnavs.length > 0)
                {
                    subnavs[0].style.display = "block";            //if found, display it
                }

                if (lwNavigation.theCrumbs)                        //if breadcrumb-Navigation element exists, fill it
                {
                    while (lwNavigation.theCrumbs.firstChild)      //first, remove all existing content from element
                    {
                        lwNavigation.theCrumbs.removeChild(lwNavigation.theCrumbs.firstChild);
                    }

                    elem = navilinks[ix];                          
                    
                    var crumblist = document.createElement("ul");  //create the list of crumbs
                    lwNavigation.theCrumbs.appendChild(crumblist);
                    
                    newElem = document.createElement("li");        //current page will be last item 
                    newElem.className = "current";
                    newElem2 = document.createElement("span");
                    
                    var subelem = elem.firstChild;                     //clone all children of current link into span for crumb trail
                    while (subelem)
                    {
                        newElem2.appendChild(subelem.cloneNode(true));
                        subelem = subelem.nextSibling;
                    }
                    
                    newElem.appendChild(newElem2);                  //put span into li and li into crumb list
                    crumblist.appendChild(newElem);
                    
                    while ((elem = elem.parentNode).nodeName.toLowerCase() != "li") //find nearest ancester li
                    {
                        ; //nothing to do in loop
                    }
                    
                    while ((elem = elem.parentNode) != lwNavigation.theNav)  //work the way up the navigation tree
                    {
                        if (elem.nodeName.toLowerCase() == "li")             //when a li is found
                        {
                            var myanchors = elem.getElementsByTagName("a");  //get its first a element
                            if (myanchors.length > 0)
                            {
                                newElem = document.createElement("li");      //create a li element in breadcrumb trail
                                newElem2 = myanchors[0].cloneNode(true);
                                
                                newElem.appendChild(newElem2);
                                crumblist.insertBefore(newElem, crumblist.firstChild); //enter new li element into crumb list
                            }
                        }
                    }
                    
                    if (ix != 0) //if currentLink is not the "Home" link (the first link)
                    {
                        newElem = document.createElement("li");                //create another bread crumb entry "Home"
                        newElem2 = navilinks[0].cloneNode(true);
                        newElem.appendChild(newElem2);
                        crumblist.insertBefore(newElem, crumblist.firstChild);
                    }
                    lwNavigation.theCrumbs.style.display = "block";            //now display the bread crumb trail
                        
                }

                newElem = document.createElement("span");      //replace current link with a span (a link to the current page doesn't make much sense
                elem = navilinks[ix];
                while (elem.firstChild)
                {
                    newElem.appendChild(elem.firstChild);      //appending the elem.firstChild to the span automatically removes it from the anchor element
                }
                elem.parentNode.replaceChild(newElem, elem);
            }
        }
        
		// display navigation DIV
		elem = document.getElementById("lwTreeNavigationDiv");
		if (elem) elem.style.display = "";

		
        lwNavigation.initialized = true;
    }
}
window.onload = lwNavigation.onload;

