<!-- category tree -->

{assign var='types' value=','|explode:$types}

{if !$block.Tpl}
	<div class="caption" style="padding-bottom: 5px;">
		<b>
		{if $types|@count > 1}
			{$lang.categories}
		{else}
			{$listing_types[$types.0].name}
		{/if}
		</b>
	</div>
{/if}

{if !$box_categories}
	{assign var='box_categories' value=$categories}
{/if}

{foreach from=$types item='type'}
	{assign var='box_listing_type' value=$listing_types.$type}

	{if $types|@count > 1}
		{include file='blocks'|cat:$smarty.const.RL_DS|cat:'fieldset_header.tpl' name=$box_listing_type.name id='addcatblock'|cat:$box_listing_type.Key}
	{/if}

	{if $box_categories.$type}
		{if $category.ID > 0 && $box_listing_type.Ctree_child_only}
			{math assign='ctree_cbc_count' equation='count-2' count=$bread_crumbs|@count}
			{assign var='ctree_cbc' value=$bread_crumbs[$ctree_cbc_count]}
			<div class="ctree-move-top"><a title="Move top level category" href="{$rlBase}{if $config.mod_rewrite}{$ctree_cbc.path}{if $ctree_cbc.category}{if $box_listing_type.Cat_postfix}.html{else}/{/if}{else}.html{/if}{else}?page={$ctree_cbc.path}{/if}">Move top level category</a></div>
		{/if}
	
		<ul class="ctree-container {if $box_listing_type.Ablock_show_subcats}ctree-allow-sc{/if} {if $smarty.const.RL_LANG_DIR == 'rtl'}ctree-rtl{/if}" id="ctree-container-{$type}">
		{foreach from=$box_categories.$type item='cat' name='fCats'}
			<li id="ctree-catid-{$cat.ID}" class="loaded {if ($category.ID == $cat.ID && $box_listing_type.Ctree_child_only && $cat.sub_categories) || $box_listing_type.Ctree_open_subcat}opened{/if} {if !empty($cat.sub_categories) && $box_listing_type.Ablock_show_subcats}ctree-sc{/if}">
				<img {if !empty($cat.sub_categories) && $box_listing_type.Ablock_show_subcats}title="{$lang.category_tree_show_subcategories}"{/if} class="plus-icon" alt="" src="{$rlTplBase}/img/blank.gif" />
				
				{rlHook name='tplPreCategory'}
				{if $category.ID == $cat.ID && $box_listing_type.Ctree_child_only}
					<span class="current">{$lang[$cat.pName]}</span>
				{else}
					<a {if $category.ID == $cat.ID}class="current"{/if} title="{if $lang[$cat.pTitle]}{$lang[$cat.pTitle]}{else}{$lang[$cat.pName]}{/if}" href="{$rlBase}{if $config.mod_rewrite}{$pages[$box_listing_type.Page_key]}/{$cat.Path}{if $box_listing_type.Cat_postfix}.html{else}/{/if}{else}?page={$pages[$box_listing_type.Page_key]}&amp;category={$cat.ID}{/if}">{$lang[$cat.pName]}</a>
				{/if}
				{if $box_listing_type.Cat_listing_counter}
					<span class="count hlight">{$cat.Count|number_format}</span>
				{/if}
				<span class="tree_loader"></span>
				{rlHook name='tplPostCategory'}
				
				{assign var='direct' value=false}
				{if $cat.ID|in_array:$ctree_bc || $box_listing_type.Ctree_open_subcat || ($category.ID == $cat.ID && $box_listing_type.Ctree_child_only)}
					{assign var='direct' value=true}
				{/if}
				{include file=$smarty.const.RL_PLUGINS|cat:'categories_tree'|cat:$smarty.const.RL_DS|cat:'level.tpl' ctree_subcategories=$cat.sub_categories direct=$direct}
			</li>
		{/foreach}
		</ul>

		<script type="text/javascript">
		var ctree_bc = new Array();
		{if $ctree_bc}
			{foreach from=$ctree_bc item='cb_item'}
				ctree_bc.push({$cb_item});
			{/foreach}
		{/if}
		var ctree_progress = false;
		{literal}
		
		$(document).ready(function(){
			$('#ctree-container-{/literal}{$type}{literal}').parent().attr('style', false);
			$('#ctree-container-{/literal}{$type}{literal}').closest('div.fieldset').parent().attr('style', false);
			
			ctreeOpen{/literal}{$type}{literal}();
			
			if ( ctree_bc.length > 0 ) {
				$('#ctree-catid-'+ctree_bc[0]).addClass('opened');
				if ( ctree_bc[1] ) {
					$('#ctree-catid-'+ctree_bc[1]).find('img.plus-icon:first').trigger('click');
				}
			}
		});
		
		var ctreeOpen{/literal}{$type}{literal} = function(){
			$('#ctree-container-{/literal}{$type}{literal} li.ctree-sc > img.plus-icon').unbind('click').click(function(){
				var self = this;
				if ( $(this).parent().hasClass('opened') ) {
					$(this).parent().find('ul').fadeOut(function(){
						$(self).parent().removeClass('opened');
					});
				}
				else {
					if ( $(this).parent().hasClass('loaded') ) {
						$(this).parent().find('ul').fadeIn(function(){
							$(self).parent().addClass('opened');
						});
					}
					else {
						if ( !ctree_progress ) {
							var id = $(this).parent().attr('id').split('-')[2];
							
							$(this).parent().find('.tree_loader').fadeIn();
							
							ctree_progress = true;
							xajax_ctreeOpen(id, '{/literal}{$type}{literal}', ctree_bc);
						}
					}
				}
			});
		}
		
		{/literal}
		</script>
	{else}
		<div style="padding: 10px 12px;">{$lang.listing_type_no_categories}</div>
	{/if}
	
	{if $types|@count > 1}
		{include file='blocks'|cat:$smarty.const.RL_DS|cat:'fieldset_footer.tpl'}
	{/if}
	
{/foreach}

<!-- category tree end -->