<ul id="topnav">
<?php
    $i = 0;
    $all[0] = array(   'idparent'=> '',
                        'idmenu_items'=> ''
                      );
    if(isset($_SERVER['REDIRECT_URL']))
		$tablica = explode('/', $_SERVER['REDIRECT_URL']);

    foreach($menu_items as $menu_item){
        $current='';
        if($menu_item['active']){
            if(isset($tablica[3])){
                //echo $menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html<br/>';
                if($tablica[3] == $menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html' || $tablica[3] == $menu_item['friendlyurl'].'.html'){
                    $current=array( 'idparent'=>$menu_item['idparent'],
                                    'idmenu_items'=>$menu_item['idmenu_items']
                                  );
                }elseif(isset($subarticle)){
                    if($tablica[3] == $article['friendlyurl'].'-'.$article['idarticles'].'.html' && $subarticle['friendlyurl'].'-'.$subarticle['idarticles'].'.html' == $menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html'){
                        $current=array( 'idparent'=>$menu_item['idparent'],
                                        'idmenu_items'=>$menu_item['idmenu_items']
                                      );
                    }elseif(isset($parent)){
                        if($parent['friendlyurl'].'-'.$parent['idarticles'].'.html' == $menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html'){
                            $current=array( 'idparent'=>$menu_item['idparent'],
                                            'idmenu_items'=>$menu_item['idmenu_items']
                                          );
                        }
                    }
                }
            }elseif(isset($tablica[2])){
                if($tablica[2] == $menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html' || $tablica[2] == $menu_item['friendlyurl']){
                    $current=array( 'idparent'=>$menu_item['idparent'],
                                    'idmenu_items'=>$menu_item['idmenu_items']
                                  );
                }
            }else{
                if('' == $menu_item['friendlyurl']){
                    $current=array( 'idparent'=>$menu_item['idparent'],
                                    'idmenu_items'=>$menu_item['idmenu_items']
                                  );
                }
            }
            if($current!=''){
                $all[$i] = $current;
                break;
            }
        }
    }
    //przygotowanie drzewka menu
    if(isset($all)){
        $i++;
         do{
            $child = false;
            foreach($menu_items as $menu_item){
                if($all[$i-1]['idparent']==$menu_item['idmenu_items']){
                    $all[$i] = array( 'idparent'=>$menu_item['idparent'],
                                    'idmenu_items'=>$menu_item['idmenu_items']
                                  );
                    if($all[$i]['idparent']!=''){
                       $child = true;
                       $i++;
                       break;
                    }
                }
            }
         }while($child);

         foreach($menu_items as $menu_item){
			if($menu_item['active']){
			//$menu_item['friendlyurl']!='panel-klienta.html' jest warunkiem dodanym tylko na potrzeby networds.pl
                if($menu_item['idparent']==null && $menu_item['friendlyurl']!='panel-klienta.html'){
                 /* w przypadku robienia kresek pomiędzy pozycjami
				if($i>=1){
					echo '<div class="dots"></div>';
				}else{
					$i++;
				}
                */
                 
                    if($all[sizeof($all)-1]['idmenu_items']==$menu_item['idmenu_items']){
						echo '<li><div class="menu-poziom-item-left"></div>';
                        echo '<div class="menu-poziom-item-active">';
					}else{
                        echo '<li><div class="menu-poziom-item">';
                    }

                    switch($menu_item['type']){
                        case 'article':
                            echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['artykul'].'/'.$menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html">'.$menu_item['elementname'].'</a>';
                            break;
                        case 'gallery':
                            echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['galeria'].'/'.$menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html">'.$menu_item['elementname'].'</a>';
                            break;
                        case 'news':
                            echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['news'].'/'.$menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html">'.$menu_item['elementname'].'</a>';
                            break;
                        case 'none':
                            if($menu_item['friendlyurl']!=''){
                                echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'">'.$menu_item['elementname'].'</a>';
                            }else{
                                echo '<a href="/">'.$menu_item['elementname'].'</a>';
                            }
                            break;
                        case 'product_category':
                            echo 'a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'.html">'.$menu_item['elementname'].'</a>';
                            break;
                        case 'product':
                            echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['produkt'].'/'.$menu_item['friendlyurl'].'-'.$menu_item['idelement'].'.html">'.$menu_item['elementname'].'</a>';
                            break;
                    }
                    
                    echo '</div>';
                    if($all[sizeof($all)-1]['idmenu_items']==$menu_item['idmenu_items']){
                            echo '<div class="menu-poziom-item-right"></div>';
                    }

                    $first = true;
                    foreach($menu_items as $submenu_item){
                        if($submenu_item['active'] && $submenu_item['idparent']==$menu_item['idmenu_items']){
                            if($first){
                                echo '<div class="sub">';
                                    echo '<ul>';
                                    $first = false;
                            }
                            echo '<li>';
                            switch($submenu_item['type']){
                                case 'article':
                                    echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['artykul'].'/'.$submenu_item['friendlyurl'].'-'.$submenu_item['idelement'].'.html">'.$submenu_item['elementname'].'</a>';
                                    break;
                                case 'gallery':
                                    echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['galeria'].'/'.$submenu_item['friendlyurl'].'-'.$submenu_item['idelement'].'.html">'.$submenu_item['elementname'].'</a>';
                                    break;
                                case 'news':
                                    echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['news'].'/'.$submenu_item['friendlyurl'].'-'.$submenu_item['idelement'].'.html">'.$submenu_item['elementname'].'</a>';
                                    break;
                                case 'none':
                                    if($menu_item['friendlyurl']!=''){
                                        echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$submenu_item['friendlyurl'].'">'.$submenu_item['elementname'].'</a>';
                                    }else{
                                        echo '<a href="/">'.$submenu_item['elementname'].'</a>';
                                    }
                                    break;
                                case 'product_category':
                                    echo 'a href="/'.strtolower($this->site['lang']['skrot']).'/'.$submenu_item['friendlyurl'].'.html">'.$submenu_item['elementname'].'</a>';
                                    break;
                                case 'product':
                                    echo '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$this->site['linki_do_sitemapy']['produkt'].'/'.$submenu_item['friendlyurl'].'-'.$submenu_item['idelement'].'.html">'.$submenu_item['elementname'].'</a>';
                                    break;
                            }
                            echo '</li>';
                        }
                    }
                    if(!$first){
                        echo '</ul>';
                            echo '</div>';
                    }
                    echo '</li>';
                }
            }
         }
    }
 ?>
</ul>

