/**
 * Filename    : navigation.js
 * Author      : Robert Cerny
 * Created     : 2007-03-17
 * Last Change : 2007-03-17
 *
 * This file supports the navigation throughout CERNY-ONLINE.  It does
 * so by marking the currently displayed entries in the tocs in the
 * margin.  The top navigation is handled on the Server, and is
 * therefore not part of this script.
 */

CERNY.require("CO.navigation", "CERNY.js.String", "addClass");

CO = {};

CERNY.namespace("navigation", CO);
CO.navigation.logger = CERNY.Logger("CO.navigation");

/**
 * Mark all links in the margin as selected, which match the current
 * URL. There might be more than one link selected.
 *
 * Do not mark more than one link per table of content container
 * (e.g. max one package should be selected).
 *
 * url - the URL to use; defaults to document.URL
 * return - the number of links selected
 */
CO.navigation.markLinksAsSelected = function(url) {
    var logger = CERNY.Logger("CO.navigation.markLinksAsSelected");
    if (!isNonEmptyString(url)) {
        url = document.URL;
    }
    var count = 0;

    var marginE = document.getElementById("margin");
    if (!marginE) {
        logger.error("Element with id 'margin' not present.");
    }
    var links = marginE.getElementsByTagName("a");

    function getTocContainer(node) {
        return node.parentNode.parentNode;
    }

    var tocContainer = marginE;
    for (var i = links.length - 1; i >= 0; i--) {
        var link = links[i];
        if (url.indexOf(link.href) == 0 && getTocContainer(link) != tocContainer) {
            CO.navigation.markLinkAsSelected(link);
            tocContainer = getTocContainer(link);
            count += 1;
        }
    }
    return count;
}
CERNY.signature(CO.navigation.markLinksAsSelected, "number", ["undefined", "string"]);

CO.navigation.markLinkAsSelected = function(link) {
    addClass(link, "selected");
}
// CERNY.signature(CO.navigation.markLinkAsSelected, "undefined", Element);

// CERNY.intercept(CO.navigation);
