/**
 * Activates the two separate javascript functions for drop down buttons on page load.
 **/
$(document).ready(function(){
    selectFirstElement();
    deselectFirstElement();
});

/**
 * When a dropdown menu button is opened this function will autoselect the first element in the list.
 */
function selectFirstElement() {
    $(document).on('shown.bs.dropdown', function(event) {
            var dropdown = $(event.target);

            // Set aria-expanded to true
            dropdown.find('.dropdown-menu').attr('aria-expanded', true);

            // Set focus on the first link in the dropdown
            setTimeout(function() {
                dropdown.find('.dropdown-menu li:first-child a').focus();
            }, 10);
    });
}

/**
 * When a dropdown menu is closed this function will deselect the selected element in the list.
 */
function deselectFirstElement() {
    $(document).on('hidden.bs.dropdown', function(event) {
        var dropdown = $(event.target);

        // Set aria-expanded to false
        dropdown.find('.dropdown-menu').attr('aria-expanded', false);

        // Set focus back to dropdown toggle
        dropdown.find('.dropdown-toggle').focus();
    });
}