<?
if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
if (empty($arResult))return;
$menuList = array();
$lev = 0;
$lastInd = 0;
$parents = array();
$blockLevel = false;
foreach ($arResult as $arItem) {
	if (is_int($blockLevel) && $blockLevel<= $arItem['DEPTH_LEVEL']) 
		continue;
	else
		$blockLevel = false;

    $lev = $arItem['DEPTH_LEVEL'];
    if ($arItem['IS_PARENT']) {
        $arItem['CHILDREN'] = array();
    }
    
    if ($lev == 1) {
        $menuList[] = $arItem;
        $lastInd = count($menuList)-1;
        $parents[$lev] = &$menuList[$lastInd];
    } else {
		if ($arItem['TEXT']==$parents[$lev-1]['TEXT'] && $arItem['LINK']==$parents[$lev-1]['LINK']) {
			$blockLevel = $arItem['DEPTH_LEVEL'];
			unset ($parents[$lev-1]['CHILDREN']);
			continue;
		}
        $parents[$lev-1]['CHILDREN'][] = $arItem;
        $lastInd = count($parents[$lev-1]['CHILDREN'])-1;
        $parents[$lev] = &$parents[$lev-1]['CHILDREN'][$lastInd];
    }
}
if (isset ($menuList[0]['CHILDREN']))
	array_unshift($menuList,
	array(
		'LINK' => SITE_DIR,
		'TEXT' => GetMessage("MENU_MAIN_PAGE"),
	));
function dbReqPrintMenuBySect(&$arStr, $arList, $num = 0, $strTitle = '', $strTitleParentFx = '', $strID = 0, $strDepthLevel = 1){
	$str = '';
	if($num > 0){
	$str .='
		<div id="menu-'.$strDepthLevel.'-'.$num.'" class="nav-block-fx '.($num > 0 ? '': '').'">
			<div class="header-menu">
				<div class="btn-group">
					<a href="#menu-'.($strDepthLevel-1).'-'.$strID.'" data-cur="menu-'.$strDepthLevel.'-'.$num.'" data-link="menu-'.($strDepthLevel-1).'-'.$strID.'" rel="menu" class="btn fx-in"><i class="icon-chevron-left"></i>&nbsp;'.$strTitle.'</a>
				</div>
			</div>
		';
		$strTitleParent = $strTitleParentFx;
	}else{
		$str .='
		<div id="menu-'.$strDepthLevel.'-'.$num.'" class="nav-block-fx db_open">
		';
		$strTitleParent = GetMessage("MENU_TOP_LEVEL");
	}
	$str .='
		<ul class="nav nav-tabs nav-stacked " >
	';
	
	foreach($arList as $index => $arItem){
		if (isset($arItem["CHILDREN"])){
			dbReqPrintMenuBySect(&$arStr, $arItem["CHILDREN"], $index, $strTitleParent, $arItem["TEXT"], $num, ($arItem["DEPTH_LEVEL"]+1));
			$str .='
				<li class="'.($arItem["SELECTED"] ? 'active' : '').' nav-rt"><a rel="menu" data-cur="menu-'.$arItem["DEPTH_LEVEL"].'-'.$num.'" data-link="menu-'.($arItem["DEPTH_LEVEL"]+1).'-'.$index.'" href="'.$arItem["LINK"].'"><i class="icon-chevron-right"></i>'.$arItem["TEXT"].'</a></li>
			';
		}else{
			$str .='
				<li class="'.($arItem["SELECTED"] ? 'active' : '').'"><a rel="nav-menu" href="'.$arItem["LINK"].'">'.$arItem["TEXT"].'</a></li>
			';
		}
	}
	$str .='
		</ul>
	';
	if($num > 0){
		$str .='
			</div>
		';
	}else{
		$str .='
			</div>
		';
	}
	$arStr[] = $str;
}
$arStr = array();
dbReqPrintMenuBySect(&$arStr, $menuList);
echo implode('', $arStr);
?>