// ======================================
// Script to format the self link and other navigation.
//
// Requires JQuery.
//
debug = function (log_txt) {
    if (window.console != undefined) {
        console.log(log_txt);
    }
}

var FC = new Object();

FC.apply = function(options) {
    var default_args = {
	'self_class' : "selflink",
	'parent_class' : "current",
	'navbar_id' : 'navbar',
	'sidenav_id' : 'side_nav',
	'maxlist' : 20,
    }
    for(var index in default_args) {
	if (!options 
	    || typeof options[index] == "undefined")
	{
	    options[index] = default_args[index];
	}
    }
    FC.self_class = options['self_class'];
    FC.parent_class = options['parent_class'];
    FC.maxlist = options['maxlist'];

    var top_nav = document.getElementById(options['navbar_id']);
    if (top_nav) {
	var li = top_nav.getElementsByTagName("li");
	this.format_self(li);
	this.format_parent(li);
    }
    var side_nav = document.getElementById(options['sidenav_id']);
    if (side_nav) {
	var li = side_nav.getElementsByTagName("li");
	this.toggle_lower(side_nav);
	this.format_self(li);
    }
} // apply

FC.this_url = function() {
    // build the plain URL of this page
    // without any queries or anchors
    var url = window.location.protocol + '//' + window.location.host + window.location.pathname;
    return url;
}

FC.format_self = function(list) {
    var self_ind = -1;
    for(var i=0;i<list.length;i++)
    {
	var links = list[i].getElementsByTagName("a");
	if (links.length && links[0].href == this.this_url()) {
	    var label = $(links[0]).html();
	    $(links[0]).replaceWith("<span class='"+this.self_class+"'>"+label+"</span>");
	    self_ind = i;
	    break;
	}
    }
    return self_ind;
}

FC.find_parent_url = function() {

    var this_url = this.this_url();
    var s = this_url.split("/");
    var plen = s.length - 1;
    if (this_url.match(/\/$/)
	|| this_url.match(/\/index\.s?html?$/))
    {
	plen = s.length - 2;
    }
    var parent_url = this.truncate_url(this_url,plen);
    return parent_url;
} // find_parent_url

FC.format_parent = function(list) {

    var parent_url = this.find_parent_url();

    var parent_ind = -1;
    for(var i=0;i<list.length;i++)
    {
	var links = list[i].getElementsByTagName("a");
	if (links.length && links[0].href == parent_url) {
	    $(list[i]).addClass(this.parent_class);
	    parent_ind = i;
	    break;
	}
    }
    return parent_ind;
}

FC.truncate_url = function(url,len) {
    var s = url.split("/");
    var chop_url = '';
    for (var i=0; i < len; i++)
    {
	chop_url = chop_url + s[i] + '/';
    }
    return chop_url;
} // truncate_url

FC.has_current_link = function(links) {
    var this_path = window.location.pathname;
    var found = 0;
    for (var i=0; i<links.length && !found; i++)
    {
	// current link?
	if (links[i].pathname == this_path)
	{
	    found = 1;
	}
    }
    return found;
} // has_current_link

FC.hide_low_uls = function(lowul) {

    var curr_uls = Array();
    for(var i=0; i<lowul.length; i++)
    {
	var links = $("a", lowul[i]);
	var curr_link_found = this.has_current_link(links);
	// Check if the current UL is the child of the list-item
	// which contains the current link
	if (!curr_link_found)
	{
	    var my_parent = $(lowul[i]).parent();
	    var parentlinks = $(my_parent).children("a");
	    curr_link_found = this.has_current_link(parentlinks);
	}
	
	if (curr_link_found)
	{
	    curr_uls[curr_uls.length] = lowul[i];
	}
	else
	{
	    $(lowul[i]).hide();
	}
    }
    return curr_uls;
} // hide_low_uls

FC.hide_other_lis = function(navul) {

    var top_url = this.truncate_url(this.this_url(),4);
    var re = new RegExp(top_url);

    for(var i=0; i<navul.length; i++)
    {
	// just look at the children of this UL
	var items = $(navul[i]).children("li");
	for (var j=0; j<items.length; j++)
	{
	    var links = $(items[j]).children("a");
	    for (var k=0; k<links.length; k++)
	    {
		if (!links[k].href.match(re))
		{
		    $(items[j]).hide();
		}
	    }
	}
    }
} // hide_other_lis

FC.toggle_lower = function(map) {

    // Hide list items not (under the parent or siblings to it)
    // Find the UL the current link is in

    // Hide all the low-ULs that don't contain the current link
    // Note that there could be multiple ULs that contain it,
    // since ULs can be inside ULs
    var lowul = $("ul ul", map);
    var curr_uls = this.hide_low_uls(lowul);

    // if we don't have a current UL, return
    if (curr_uls.length == 0)
    {
	return;
    }

    // Look in the top UL for things to hide
    var navul = $("div>ul", map);
    this.hide_other_lis(navul);

    // Go through the curr-ULs
    var this_url = this.this_url();
    var trimto = this.maxlist / 2;
    for(var cu=0; cu<curr_uls.length; cu++)
    {
	var listitems = $(curr_uls[cu]).children("li");
	if (listitems.length > this.maxlist)
	{
	    // find the current link in this list, and hide before and after
	    var current = -1;
	    for(var i=0; i < listitems.length; i++)
	    {
		var links = $("a", listitems[i]);
		for (var j=0; j<links.length; j++)
		{
		    if (links[j].href == this_url)
		    {
			current = i;
		    }
		}
	    }
	    var show_before;
	    var hide_before;
	    var show_after;
	    var hide_after;
	    for(var i=0;i<listitems.length;i++)
	    {
		if (i < (current - trimto)) {
		    $(listitems[i]).hide();
		    $(listitems[i]).addClass("before");
		}
		if (i == (current - trimto)) {
		    $(listitems[i]).prepend('<span class="showme">+</span><span class="hideme">--</span> ');
		    show_before = $(listitems[i]).children(".showme");
		    hide_before = $(listitems[i]).children(".hideme");
		    show_before.click(function () {
			$(this).parent().parent().children(".before").show();
			$(this).hide();
			$(this).parent().children(".hideme").show();
		    });
		    hide_before.click(function () {
			$(this).parent().parent().children(".before").hide();
			$(this).hide();
			$(this).parent().children(".showme").show();
		    });
		    $(hide_before).hide();
		}
		if (i > (current + trimto)) {
		    $(listitems[i]).hide();
		    $(listitems[i]).addClass("after");
		}
		if (i == (current + trimto)) {
		    $(listitems[i]).append(' <span class="showme">+</span><span class="hideme">--</span>');
		    show_after = $(listitems[i]).children(".showme");
		    hide_after = $(listitems[i]).children(".hideme");
		    show_after.click(function () {
			$(this).parent().parent().children(".after").show();
			$(this).hide();
			$(this).parent().children(".hideme").show();
		    });
		    hide_after.click(function () {
			$(this).parent().parent().children(".after").hide();
			$(this).hide();
			$(this).parent().children(".showme").show();
		    });
		    $(hide_after).hide();
		}
	    } // for listitems

	}
    }

} // toggle_lower


