////// Define the functions common to the entire site.



//// Master function that loads all other functions for a page.
function addToPage(newFunction)
    {
    // Check whether functions are already set to run when the page loads.
    if (typeof window.onload != 'function')
        {
        // No other functions are set to run.
        // Set this function to run when the page loads.
        window.onload = newFunction;
        }
    else
        {
        // Other functions are already set to run.
        // Store the other functions.
        var existingFunctions = window.onload;

        // Overwrite the existing page load.
        window.onload = function()
            {
            // When the page loads,
            // run the stored functions first,
            // then the new function.
            existingFunctions();
            newFunction();
            }
        }
    }



//// Expand or colapse the Section links as needed.
function ExpandColapseSections()
    {
    // Check the viewing device's JavaScript capabilities.
    if (document.getElementById)
        {
        // Find the Contents link in the site's navigation bar.
        var navContentsLink = document.getElementById('navigation_contents_link');

        // Find the Section links in the site's navigation bar.
        var navSections     = document.getElementById('navigation_sections');

        // Check the viewing device's JavaScript capabilities again.
        if ( (navContentsLink.getAttribute) && (navContentsLink.setAttribute) && (navSections.setAttribute) )
            {
            // Check the state of the Contents link.
            var navContentsClass = navContentsLink.getAttribute('class');

            // Compensate when 'className' is used instead of 'class'.
            var navContentsClassName = navContentsLink.getAttribute('className');
            if (navContentsClass == null) { navContentsClass = navContentsClassName; }

            // Expand or Colapse the Section links
            // and change the state of the Contents link.
            if (navContentsClass == 'up')
                {
                navContentsLink.setAttribute('title', 'Contents - Expand');
                navContentsLink.setAttribute('class', 'down');
                navContentsLink.setAttribute('className', 'down');
                navSections.setAttribute('class', 'colapsed');
                navSections.setAttribute('className', 'colapsed');
                }
            else // Expanded by default (navContentsClass == 'up').
                {
                navContentsLink.setAttribute('title', 'Contents - Colapse');
                navContentsLink.setAttribute('class', 'up');
                navContentsLink.setAttribute('className', 'up');
                navSections.setAttribute('class', 'expanded');
                navSections.setAttribute('className', 'expanded');
                }
            }
        }
    }



//// Create a new Contents navigation link
//// to expand and colapse the Sections links,
//// then colapse the Sections links.
function setLinksNavigation()
    {
    // Check the viewing device's JavaScript capabilities.
    if ( (document.getElementById) && (document.createElement) && (document.createTextNode) )
        {
        // Find the Contents static text element in the site's navigation bar.
        var navContents = document.getElementById('navigation_replace');

        // Find the Section links in the site's navigation bar.
        var navSections = document.getElementById('navigation_sections');

        // Create a new Contents link element.
        var newLink     = document.createElement('a');

        // Check the viewing device's JavaScript capabilities again.
        if ( (newLink.setAttribute) && (newLink.appendChild) && (navContents.parentNode.replaceChild) && (navSections.setAttribute) )
            {
            // Assign attributes to the new Contents link element.
            newLink.setAttribute('id', 'navigation_contents_link');
            newLink.setAttribute('href', '#');
            newLink.setAttribute('title', 'Contents - Expand');
            newLink.setAttribute('class', 'down');
            newLink.setAttribute('className', 'down');
            newLink.appendChild(document.createTextNode('Contents'));

            // Replace the Contents static text element
            // with the new Contents link element.
            navContents.parentNode.replaceChild(newLink, navContents);

            newLink.onclick = function() { ExpandColapseSections(); return false; }

            // Colapse the Section links.
            navSections.setAttribute('class', 'colapsed');
            navSections.setAttribute('className', 'colapsed');
            }
        }
    }



//// Given a URL, open a new browser window.
function externalWindow(theURL)
    {
    if (window.open)
        {
        var theWindow = window.open(theURL);
        if (window.focus)
            {
            // Bring the new window forward.
            theWindow.focus();
            }
        // Supress the browser's default behavior.
        return false;
        }
    }



//// Assign behaviors to certain types of links.
function setLinks()
    {
    if ((document.getElementsByTagName) && (document.getElementById))
        {

        var theLinks = document.getElementsByTagName('a');

        for (var i = 0; i < theLinks.length; i++)
            {
            if ((theLinks[i].getAttribute) && (theLinks[i].setAttribute))
                {
                // Modify links for external sites and arbitrarily defined new windows.
                var theHREF  = theLinks[i].getAttribute('href');
                var theClass = theLinks[i].getAttribute('class');

                // Compensate for when 'className' is used instead of 'class'.
                var theClassName = theLinks[i].getAttribute('className');
                if (theClass == null) { theClass = theClassName; }
                
                if (theClass == 'external')
                    {
                    // Open the link in a new window.
                    theLinks[i].onclick = function() { externalWindow(this.href); return false; }

                    var theTitle = theLinks[i].getAttribute('title');
                    var newTitle = theTitle + ' (opens a new window)';
                    theLinks[i].setAttribute('title', newTitle);
                    }
                }
            }

        }
    else
        {
        return true;
        }
    }



////// Call all the functions common to every page.
addToPage(setLinksNavigation);
addToPage(setLinks);
