<?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() {
    if ($("#side_menu_multilevel li").is(".item-selected"))  {$("#side_menu_multilevel li.root-item-selected").addClass("hide_selection")}  
    //$("#side_menu_multilevel li.item-selected").parent('#side_menu_multilevel').find("li.root-item-selected").addClass("hide_selection");
    //console.log("!!!!!!!!!!111234!!!!!!!!!!!!!"+$("#side_menu_multilevel li.item-selected").parent('#side_menu_multilevel'));
        
    $('#side_menu_multilevel li.root-item a span.roll-out').click(
           function(){ 
               if($(this).parent('a').next('ul').is('.root-item'))
               {
                $(this).parent('a').parent().toggleClass("opened");
                $(this).parent('a').next("ul.root-item").toggle(150);
                return false;
               }
           }
           );    
    $('#side_menu_multilevel li.root-item-selected a span.roll-out').click(
           function(){ 
               if($(this).parent('a').next('ul').is('.root-item'))
               {
                $(this).parent('a').parent().toggleClass("opened");
                $(this).parent('a').next("ul.root-item").toggle(150);
                return false;
               }
           }
           );    
    });                                
</script>
