﻿/*
** menu.js
*/
$(function() {
	// Current url offset.  Note, we need to rely on the directory name
	// to identify the active menu group, so we strip off the page name
	// from the url.
	var root = (this.location.protocol + "//" + this.location.host + "/").toLowerCase(); 
	var path = this.location.href.toLowerCase();
	path = path.substring(0, path.lastIndexOf("/") + 1);

	// If 'root === path' then we are on the Home page and therefore
	// we don't want to expand any menus.  If this should ever change
	// then we will need to modify the following if statement to allow
	// for both situations.	Note, the issue her is that the root folder
	// will always match all the sub-folders, therefore we must manage
	// it directly.
	if (root !== path) {
		// Find all the top level item links
		$("ul.menu > li > a").each(function() {
			var href = this.href.toLowerCase();
			if (href.indexOf(path) >= 0) {
				$(this).parent().children("ul").show();
				$(this).parent().children("ul").addClass("active");
			}
		});
	}


    // Check each link to see if they match the current location
    var currentUrl = Jes.Uri.getBaseUrl().toLowerCase();
    $("ul.menu a").each(function() {
        var href = Jes.Uri.getBaseUrl(this.href).toLowerCase();
        if (currentUrl === href) {
            var $a = $(this).addClass("current");
            $a.parent().parents("li").each(function() {
                $(this).children("a").eq(0).addClass("current");
            });
        }
    });

});
