$(document).ready(function () {
    // enable tooltips
    $('[data-toggle="tooltip"]').tooltip();

    if (!isTouchDevice) {
        $('#mara-navigation').mouseover(expandNavigation);
        $('#mara-navigation').mouseleave(collapseNavigation);
    }

    // highlight navigation entry for current uri
    highlightNavigationEntry(window.location.pathname + window.location.search + window.location.hash);
});

// the nav entry that matches the current page uri best
var currentNavigationEntry;

// expands and highlights the navigation entry that matches an uri best
function highlightNavigationEntry(uri) {

    // find all navigation entries that match the request uri, take the longest (most specific) match
    var matchingUris = $('#mara-navigation .mara-nav-entry > a')
        .map(function () {
            return $(this).attr('href');
        })
        .filter(function (x) {
            return uri.startsWith(this);
        });

    if (matchingUris) {
        var longestUri = matchingUris.sort(function (a, b) {
            return b.length - a.length
        })[0];

        currentNavigationEntry = $('#mara-navigation .mara-nav-entry > a[href="' + longestUri + '"]').parent();

        currentNavigationEntry.siblings().removeClass('highlighted');
        currentNavigationEntry.addClass('highlighted');
        currentNavigationEntry.addClass('visible');
        currentNavigationEntry.addClass('expanded');
        currentNavigationEntry.parentsUntil('#mara-navigation').addClass('expanded');
        currentNavigationEntry.parentsUntil('#mara-navigation').addClass('highlighted');
        currentNavigationEntry.parentsUntil('#mara-navigation').addClass('visible');
        currentNavigationEntry.parentsUntil('#mara-navigation').children('div').addClass('visible');
    }
}

// expands or collapses a navigation entry
function toggleNavigationEntry(a) {
    if ($(a).parent().hasClass('expanded')) {
        $(a).parent().removeClass('expanded');
        $(a).siblings().removeClass('visible')
            .css('height', '') // reset height if it was left over from a previous slide action
            .slideUp({queue: true});
    } else {
        $(a).parent().addClass('expanded');
        $(a).siblings().addClass('visible')
            .css('height', '') // reset height if it was left over from a previous slide action
            .slideDown({queue: true});
    }
}


// expands the navigation sidebar
function expandNavigation() {
    $('body').removeClass('navigation-collapsed');
    $('#mara-navigation .mara-nav-entry.visible:not(.level-1)')
        .css('height', '') // reset height if it was left over from a previous slide action
        .show(0);
    if (currentNavigationEntry) {
        currentNavigationEntry[0].scrollIntoView();
        currentNavigationEntry = null;
    }
}

// collapses the navigation side bar
function collapseNavigation() {
    $('body').addClass('navigation-collapsed');
    $('#mara-navigation .mara-nav-entry.level-2.visible').slideUp({queue: true});
}

// expands or collapses the navigation side bar
function toggleNavigation() {
    if ($('body').hasClass('navigation-collapsed')) {
        expandNavigation();
    } else {
        collapseNavigation();
    }
}


// adds an alert to the bottom of the page
function showAlert(message, category) {
    alert = $('<div style="display:none" class="alert alert-' + category + ' alert-dismissible" role="alert">' +
        '<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
        '<span aria-hidden="true">&times;</span>' +
        '</button>' + message + '</div>');
    $('#alerts').append(alert);
    alert.fadeIn(2000);
}


@@ -2444,32 +2444,90 @@
 dNavigation() %7B%0A
+    if ($('body').hasClass('navigation-collapsed')) %7B%0A    
     $('body').re
@@ -2553,32 +2553,36 @@
 on-collapsed');%0A
+    
     $('#mara-nav
@@ -2629,32 +2629,36 @@
 el-1)')%0A        
+    
 .css('height', '
@@ -2733,16 +2733,20 @@
         
+   
+ 
 .show(0)
@@ -2747,16 +2747,20 @@
 how(0);%0A
+    
     if (
@@ -2781,24 +2781,28 @@
 ionEntry) %7B%0A
+    
         curr
@@ -2853,38 +2853,9 @@
     
-currentNavigationEntry = null;
+%7D
 %0A   

fix highlighting of current navigation entry when nav bar is expanded multiple times