/* anchor-menu-item-selected.js */

$(document).ready(function () {

    var menuItems = $('li[id*=menu]').find('a');
    for (var i = 0; i < menuItems.length; i++) {
        var $menuItemI = $(menuItems[i]);

        $menuItemI.click(function (event) {
            var $item = $(this);

            for (var j = 0; j < menuItems.length; j++){
                var $menuItemJ = $(menuItems[j]);
                var classes = $menuItemJ.attr('class').split(" ");
                for (var k = 0; k < classes.length; k++) {
                    if (classes[k].indexOf('_selected') !== -1) {
                        $menuItemJ.removeClass(classes[k]);
                    }
                }
            }

            var $a = $(event.target).closest('a');
            if ($a.is($item)) {
                var parentId = $a.closest('div').attr('id');
                $a.addClass(parentId + "_selected");
            }
        });
    }
});