<?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>

<?if (!empty($arResult)):?>
    <ul id="side_menu_multilevel">

    <?
    $previousLevel = 0;
    foreach($arResult as $arItem):?>

        <?if ($previousLevel && $arItem["DEPTH_LEVEL"] < $previousLevel):?>
            <?=str_repeat("</ul></li>", ($previousLevel - $arItem["DEPTH_LEVEL"]));?>
        <?endif?>

        <?if ($arItem["IS_PARENT"]):?>

            <?if ($arItem["DEPTH_LEVEL"] == 1):?>
                <li class="parent <?if ($arItem["SELECTED"]):?>root-item-selected opened<?else:?>root-item<?endif?>">
                                <a rel="nofollow"  href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?><span class="roll-out"> </span></a>
                    <ul class="root-item">
            <?else:?>
                <li class="parent<?if ($arItem["SELECTED"]):?> item-selected<?endif?>"><a rel="nofollow"  href="<?=$arItem["LINK"]?>" ><?=$arItem["TEXT"]?></a>
                    <ul>
            <?endif?>

        <?else:?>

            <?if ($arItem["PERMISSION"] > "D"):?>

                <?if ($arItem["DEPTH_LEVEL"] == 1):?>
                    <li class="<?if ($arItem["SELECTED"]):?>root-item-selected opened<?else:?>root-item<?endif?>"><a rel="nofollow"  href="<?=$arItem["LINK"]?>" ><?=$arItem["TEXT"]?></a></li>
                <?else:?>
                    <li <?if ($arItem["SELECTED"]):?> class="item-selected"<?endif?>><a rel="nofollow"  href="<?=$arItem["LINK"]?>" ><?=$arItem["TEXT"]?></a></li>
                <?endif?>

            <?else:?>

                <?if ($arItem["DEPTH_LEVEL"] == 1):?>
                    <li class="<?if ($arItem["SELECTED"]):?>root-item-selected opened<?else:?>root-item<?endif?>"><a rel="nofollow"  href=""  title="<?=GetMessage("MENU_ITEM_ACCESS_DENIED")?>"><?=$arItem["TEXT"]?></a></li>
                <?else:?>
                    <li><a rel="nofollow"  href="" class="denied" title="<?=GetMessage("MENU_ITEM_ACCESS_DENIED")?>"><?=$arItem["TEXT"]?></a></li>
                <?endif?>

            <?endif?>

        <?endif?>

        <?$previousLevel = $arItem["DEPTH_LEVEL"];?>

    <?endforeach?>

    <?if ($previousLevel > 1)://close last item tags?>
        <?=str_repeat("</ul></li>", ($previousLevel-1) );?>
    <?endif?>

    </ul>
<?endif?>
<script>
$(document).ready(function() {
    const $menu = $("#side_menu_multilevel");
    if ($("li", $menu).is(".item-selected"))  {$("li.root-item-selected", $menu).addClass("hide_selection")}

    const clickHandler = function(){
        const $a = $(this).parent('a');
        if($a.next('ul').is('.root-item'))
        {
            $a.parent().toggleClass("opened");
            $a.next("ul.root-item").toggle(150);
            return false;
        }
    };

    $('li.root-item a span.roll-out', $menu).click( clickHandler );
    $('li.root-item-selected a span.roll-out', $menu).click( clickHandler );
});
</script>
