/**
 * Applet behind the navigation menu in the master page.
 * It generates a navigation menu from the page's URL and puts it into the "navigation" tag.
 *
 * Note that this code relies on the fact that each page has a URL of the form:
 *
 *          http://host/dvt/Some-Section/Some-Sub/My-Page
 *
 * where 'dvt' is fixed and the number of sections/subsections may vary; in all cases,
 * words are separated by an '-'.  The above URL would result in a navigation menu like
 * the following:
 *
 *   > Home | Some Section | Some Sub | My Page
 *
 * with Home linking to 'http://host/', Some Section to 'http://host/dvt/Some-Section',
 * and so on.
 *
 *
 * INTERFACE WITH MARKUP
 * ---------------------
 * The page is expected to contain a tag with id of "navigation"; the generated menu will
 * be output in that tag.
 *
 *
 * DEPENDENCIES: jQuery 1.4.2, fun.lists.js
 *
 *
 * USAGE
 * -----
 * <script src="[path]/scripts/lib/jquery.js" type="text/javascript"></script>
 * <script src="[path]/scripts/fun.lists.js" type="text/javascript"></script>
 * <script src="[path]/scripts/master.nav-menu.js" type="text/javascript"></script>
 * <script type="text/javascript">
 *     $(document).ready(function () { startNavMenuApplet(); });
 * </script>
 *
 */
 
function startNavMenuApplet()
{
    var model = new NavMenuModel();
    var navMenu = new NavMenuView(model);
    navMenu.update();
}


function NavMenuModel()
{
    this.baseUrl = '';
    this.pathElements = location.pathname.split("/").slice(1);
    this.relUrls = scan(function (prevPath, curPathElement) 
                         { 
                            return prevPath + "/" + curPathElement; 
                         }, 
                         "", 
                         this.pathElements);
}
NavMenuModel.prototype.makeLink = function (relUrl, name)
{
    name = name.replace(/-/g, " ");
    
    /*if (name == "dvt")
    {
        name = "Home";
        relUrl = "/";
    }*/
   
    return concat(['<a href="', this.baseUrl + relUrl, 
                    '" title="', name,
                    '">', name,
                    '</a>']);
}
NavMenuModel.prototype.makeNavLine = function ()
{
    var _self = this;
    var links = zipWith(function (rel, name) 
                        { 
                            return _self.makeLink(rel, name); 
                        }, 
                        this.relUrls,
                        this.pathElements);
                                                
    return concat(intersperse(" | ", links));
}


function NavMenuView(model)
{
    this.model = model;
    this.navTag = $("#navigation");
}
NavMenuView.prototype.update = function ()
{
    var navLine = "&gt; " + this.model.makeNavLine();
    this.navTag.html(navLine );
}

