<?php

use_javascript('prototype.js');

$menu = array();
if ('katao' == $sf_request->getParameter('module') && in_array($sf_request->getParameter('action'), array('suppliers', 'supplierDetails', 'supplierProducts'))) {
    $katao_supplier_url = $sf_request->getParameter('supplier');
    $katao_supplier_ids = array();
	$katao_product_category_active = true;
    foreach (KataoSupplierPeer::getAllVisibleByCategory() as $katao_product_category => $katao_suppliers) {
        $katao_product_category_suppliers = array();
        foreach ($katao_suppliers as $katao_supplier) {
            if (KataoUser::STATUS_ACTIVE == $katao_supplier['status']) {
                $katao_product_category_suppliers[] = array('title' => $katao_supplier['name'], 'url' => url_for('@katao_fournisseurs_details?supplier=' . $katao_supplier['url']), 'active' => $katao_supplier_url == $katao_supplier['url']);
                $katao_supplier_ids[$katao_supplier['id']] = true;
            }
        }
        if (0 < $katao_product_category_suppliers) {
            $active = false;
            foreach ($katao_product_category_suppliers as $katao_supplier_data) {
                $active |= !empty($katao_supplier_data['active']);
            }
        	$menu[] = array('title' => $katao_product_category, 'url' => false, 'active' => $katao_product_category_active && $active, 'suppliers' => $katao_product_category_suppliers);
        	if ($katao_product_category_active && $active) {
        		$katao_product_category_active = false;
        	}
        }
    }
    foreach (KataoSupplierPeer::getAllVisible() as $katao_supplier) {
        if (KataoUser::STATUS_ACTIVE == $katao_supplier['status'] && empty($katao_supplier_ids[$katao_supplier['id']])) {
            $menu[] = array('title' => $katao_supplier['name'], 'url' => url_for('@katao_fournisseurs_details?supplier=' . $katao_supplier['url']), 'active' => $katao_supplier_url == $katao_supplier['url']);
        }
    }
} elseif ('' != $katao_product_category_url = $sf_request->getParameter('category')) {
    $katao_product_family_url = $sf_request->getParameter('family');
    foreach (KataoProductFamilyPeer::getAllByProductCategoryUrlIdentifier($katao_product_category_url) as $katao_product_family) {
        $menu[] = array('title' => $katao_product_family['name'], 'url' => url_for('@katao_boutique_famille?category=' . $katao_product_category_url . '&family=' . $katao_product_family['url']), 'active' => $katao_product_family_url == $katao_product_family['url']);
    }
} else {
    $CurrentNode = $ContentTree->getCurrentNode();
    if ($CurrentNode != $ContentTree->getRoot()) {
        while ($CurrentNode && $CurrentNode->parent && $CurrentNode->parent->parent) {
            $CurrentNode = $CurrentNode->parent;
        }
        if ($CurrentNode) {
            $secondMenuItems = $CurrentNode->getVisibleChildren();
        } else {
            $secondMenuItems = array();
        }
    } else {
        $topMenu = $CurrentNode->getVisibleChildren();
        if (count($topMenu) > 0) {
            $secondMenuItems = $topMenu[0]->getVisibleChildren();
        } else {
            $secondMenuItems = array();
        }
    }

    if (count($secondMenuItems) > 0) {
        foreach ($secondMenuItems as/*(ContentTreeNode)*/ $secondMenuNode) {
            $menu[] = array('title' => $secondMenuNode->getMenuTitle(), 'url' => $secondMenuNode->getUrl(), 'active' => $secondMenuNode->isActive());
        }
    }
}

?>

<?php if (0 < count($menu)): ?>
<div class="left">
	<div class="subnav">
		<div class="top"></div>
		<div class="center">
			<ul>
			<?php foreach ($menu as $item_index => $item): ?>
				<?php if (empty($item['url']) && !empty($item['suppliers'])): ?>
				<li>
					<?php printf('<a href="javascript://" title="%s" onclick="bascule(\'sub-menu-%s\'); return false;">%s</a>', $item['title'], $item_index, $item['title']); ?>
					<ul id="sub-menu-<?php echo $item_index ?>" style="display:<?php echo !empty($item['active'])?'block':'none' ?>;">
					<?php foreach ($item['suppliers'] as $sub_item): ?>
						<li class="<?php echo !empty($sub_item['active'])?'active':'' ?>">
							<a href="<?php echo $sub_item['url'] ?>" title="<?php echo $sub_item['title'] ?>"><?php echo $sub_item['title'] ?></a>
						</li>
					<?php endforeach ?>
					</ul>
				<?php else: ?>
				<li class="<?php echo !empty($item['active'])?'active':'' ?>">
					<a href="<?php echo $item['url'] ?>" title="<?php echo $item['title'] ?>"><?php echo $item['title'] ?></a>
				<?php endif ?>
				</li>
			<?php endforeach ?>
			</ul>
		</div>
		<div class="bottom"></div>
	</div>
</div>

<script language="javascript" type="text/javascript">
function bascule(elem)
   {
   etat=document.getElementById(elem).style.display;
   if(etat=="none"){
   document.getElementById(elem).style.display="block";
   }
   else{
   document.getElementById(elem).style.display="none";
   }
   }
</script>

<?php endif ?>