{$category_id = {category attr="id"}}

{hook name="category.sidebar-top" category=$category_id}
{ifhook rel="category.sidebar-body"}
   {hook name="category.sidebar-body" category=$category_id}
{/ifhook}
{elsehook rel="category.sidebar-body"}
<section id="categories" class="block block-nav" role="navigation" aria-labelledby="categories-label">
    <div class="block-heading">
        <h3 class="block-title" id="categories-label">{intl l="Categories"}</h3>
    </div>
    <div class="block-content">
        <nav class="nav-categories">

            {assign var="parent_cat" value={category attr="parent"}}

            {assign var="parent_cat_ids" value=[]}

            {* While it's not the top level category *}
            {while $parent_cat != 0}
                {assign var="current_loop_cat" value=$parent_cat}

                {loop name="set_parent_category" type="category" id=$parent_cat}
                    {assign var="parent_cat" value=$PARENT}
                    {append var="parent_cat_ids" value=$ID}
                {/loop}
            {/while}

            {* define the function *}
            {function menu level=0}
            {if $level == 0}
                <ul id="category" class="accordion">
            {else}
                {if in_array($parent, $parent_cat_ids)}
                    <ul id="collapse{$parent}" class="in">
                {else}
                    <ul id="collapse{$parent}" class="collapse">
                {/if}
            {/if}

            {loop name="cat-parent-$level" type="category" parent=$parent need_count_child=1 not_empty="1"}
                {if $CHILD_COUNT > 0}
                    {if in_array($ID, $parent_cat_ids)}
                        {assign var="collapsed" value=""}
                    {else}
                        {assign var="collapsed" value="collapsed"}
                    {/if}

                    <li>
                        <a href="{$URL nofilter}">
                            {$TITLE} ({count type="product" category=$ID})
                        </a>

                        <a href="#collapse{$ID}" class="accordion-toggle {$collapsed}" data-toggle="collapse" data-parent="#collapse{$ID}"></a>

                        {menu parent=$ID level=$level+1}
                    </li>
                {else}
                    <li>
                        <a href="{$URL nofilter}">
                            {$TITLE} ({count type="product" category=$ID})
                        </a>
                    </li>
                {/if}
            {/loop}

            </ul>
            {/function}

            {menu parent='0'}

        </nav>
    </div>
</section>
{/elsehook}
{hook name="category.sidebar-bottom" category=$category_id}
