

// deal with navigation directives in dynamic content
var ShellNavigation = {
        
    
    // namespace
    NAMESPACE_URI: "http://www.civicscience.com/ns/2009/navigation",
    
    
    doRedirect: function(element) {
        var url = element.getAttribute("url");
        if (url !== null) {
            window.location.replace(url);
        }
    }, // doRedirect
    
    
    getLinkUrl: function(element) {
        var matches = DOMUtil.matchClass(element, /^navigation:linkURL\[(.+)\]$/);
        if (matches != null) {
            return matches[1];
        } else {
            return null;
        }
    }, // getLinkUrl
        
    
    activateLink: function(link) {
        var init = false;
        var loader = null;
        var url = null;
        var onclick = function() {
            if (!init) {
                // get the link URL
                url = ShellNavigation.getLinkUrl(link);
                if (url != null) {
                    // check for an explicitly identifier target loader
                    matches = DOMUtil.matchClass(link, /^navigation:targetID\[(.+)\]$/);
                    if (matches != null) {
                        var target = document.getElementById(matches[1]);
                        if (target != null) {
                            loader = target.contentLoader;
                        }
                    }
                    if (loader == null) {
                        // try nearest ancestor content target
                        loader = ContentLoader.getElementContentLoader(link);
                    }
                }
                // finished lazy initialization
                init = true;
            }
            if (loader != null && url != null) {
                // load the URL into the content target
                loader.loadURL(url);
            }
        };
        EventHandling.addHandler(link, "click", onclick);
    }, // activateLinks
    
        
    loadedContent: function(loader) {
        var i;
        // activate link elements
        var links = DOMUtil.getElementsByClassName(loader.element, "link");
        for (i = 0; i < links.length; i++) {
            this.activateLink(links[i]);
        }
        if (loader.parentLoader == null) {
            // check root loader for redirect instructions
            for (i = 0; i < loader.element.childNodes.length; i++) {
                var child = loader.element.childNodes[i];
                if (child.nodeType == 1) {
                    var url = DOMUtil.getAttributeNS(child, this.NAMESPACE_URI, "redirectURL");
                    if (url) {
                        window.location = url;
                    }
                }
            }
        }
    }, // loadedContent
        
        
    addedContentLoader: function(loader) {
        this.loadedContent(loader);
        loader.addContentListener(this);
    }, // addedContentLoader
        
        
    initialize: function() {
        var i;
        // register with content loaders
        for (i = 0; i < ContentLoader.rootLoaders.length; i++) {
            this.addedContentLoader(ContentLoader.rootLoaders[i]);
            ContentLoader.rootLoaders[i].addLoaderListener(this);
        }
        // activate links outside of any content target
        var links = DOMUtil.getElementsByClassName(document.body, "link");
        for (i = 0; i < links.length; i++) {
            if (ContentLoader.getElementContentLoader(links[i]) == null) {
                this.activateLink(links[i]);
            }
        }
    } // initialize
        
        
}; // ShellNavigation


EventHandling.executeOnLoad(function() {
    ShellNavigation.initialize();
});

