Links (functions)

Links          = {}
Links.Activate = LinksActivate
Links.Complete = LinksComplete

Activate

Links.Activate( links, pathname )

function LinksActivate( links, href, top_level_links )
{
	var n = links.length;

    if ( !top_level_links ) top_level_links = [];
	
	for ( var i=0; i < n; i++ )
	{
		var link = links[i];

        if ( link.href )
        {
            if ( link.href == href )
            {
                link.className += ("" == link.className) ? "active" : " active";
            }

            //
            //  If href       = '/dashboard/some/directory/',
            //  and link.href = '/dashboard/some/',
            //
            //  then it should have the class 'subactive' added,
            //  indicating that the link is a parent directory of the current page
            //  unless it is a 'top_level' link.
            //

            var is_prefix    = LinksActivate.IsPrefix( href, link.href );
            var is_dashboard = LinksActivate.IsDashboard( link.href );
            var is_tll       = LinksActivate.TopLevelLinksContainsPrefixOfCurrentPage( top_level_links, href );

            if ( is_prefix && !(is_dashboard && is_tll) )
            {
                link.className += ("" == link.className) ? "subactive" : " subactive";
            }
        }
	}
}

LinksActivate.IsPrefix
=
function( string, prefix )
{
    return (0 === string.indexOf( prefix ));
}

LinksActivate.IsDashboard
=
function( href )
{
    return (href == location.protocol + "//" + location.hostname + "/dashboard/" );
}

LinksActivate.TopLevelLinksContainsPrefixOfCurrentPage
=
function( top_level_links, current_page )
{
    var r = false;
    var n = top_level_links.length;

    for ( var i=0; i < n; i++ )
    {
        if ( IsPrefix( current_page, top_level_links[i] ) )
        {
            r = true;
            break;
        }
    }

    return r;
}

LinksActivate.IsRootHref
=
function( href )
{
    return ((location.protocol + '//') == href);
}

Complete

Links.Complete( links, tuple )

function LinksComplete( links, tuple )
{
	var n = links.length;
	
	for ( var i=0; i < n; i++ )
	{
		var link = links[i];

        if ( link.href )
        {
    		link.href      = Replace( link.href,      tuple );
        }
		link.innerHTML = Replace( link.innerHTML, tuple );
	}
}