<?php
    $i = 0;

             $print = '';
    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);
         //ojciec jest nullem
         if($all[0]['idparent']==''){
             $first = true;
             foreach($menu_items as $key => $menu_item){
                 if($menu_item['idparent']==$all[0]['idmenu_items']){
                        if($first){
                            $print .= '<ul>';
                            $first=false;
                        }
                        $print .= '<li class="box-link"><img src="/files/css/img/frontside/small-arrow.png" alt=""/>';
                        switch($menu_item['type']){
                            case 'article':
                                $print .= '<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':
                                $print .= '<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':
                                $print .= '<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']!=''){
                                    $print .= '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'">'.$menu_item['elementname'].'</a>';
                                }else{
                                    $print .= '<a href="/">'.$menu_item['elementname'].'</a>';
                                }
                                break;
                            case 'product_category':
                                $print .= 'a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'.html">'.$menu_item['elementname'].'</a>';
                                break;
                            case 'product':
                                $print .= '<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;
                        }
                        $print .= '</li>';
                 }
                 if(!isset($menu_items[$key+1]) && !$first){
                            $print .= '</ul>';
                        }
             }
         //ojciec nie jest nullem
         }else{
             $level = sizeof($all);
             $print = '';
             for($lvl=0; $lvl<$level; $lvl++){
                 $rownolegle = '';
                 foreach($menu_items as $menu_item){
                     $bufor = '';
                     if($all[$lvl]['idparent']==$menu_item['idparent']){
                         if($all[$lvl]['idparent']!=''){
                             if($all[$lvl]['idmenu_items']==$menu_item['idmenu_items']){
                                 $bufor .= '<li class="box-link-active"><img src="/files/css/img/frontside/small-arrow.png" alt=""/>';
                             }else{
                                 $bufor .= '<li class="box-link"><img src="/files/css/img/frontside/small-arrow.png" alt=""/>';
                             }
                            switch($menu_item['type']){
                                case 'article':
                                    $bufor .= '<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':
                                    $bufor .= '<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':
                                    $bufor .= '<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']!=''){
                                        $bufor .= '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'">'.$menu_item['elementname'].'</a>';
                                    }else{
                                        $bufor .= '<a href="/">'.$menu_item['elementname'].'</a>';
                                    }
                                    break;
                                case 'product_category':
                                    $bufor .= 'a href="/'.strtolower($this->site['lang']['skrot']).'/'.$menu_item['friendlyurl'].'.html">'.$menu_item['elementname'].'</a>';
                                    break;
                                case 'product':
                                    $bufor .= '<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;
                            }
                            $bufor .= '</li>';
                            if($all[$lvl]['idmenu_items']==$menu_item['idmenu_items']){
                                if($print == ''){
                                    $first = true;
                                     foreach($menu_items as $key => $submenu_item){
                                         if($submenu_item['idparent']==$menu_item['idmenu_items']){
                                            if($first){
                                                $print .= '<ul>';
                                                $first=false;
                                            }
                                            $print .= '<li class="box-link"><img src="/files/css/img/frontside/small-arrow.png" alt=""/>';
                                            switch($submenu_item['type']){
                                                case 'article':
                                                    $print .= '<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':
                                                    $print .= '<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':
                                                    $print .= '<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']!=''){
                                                        $print .= '<a href="/'.strtolower($this->site['lang']['skrot']).'/'.$submenu_item['friendlyurl'].'">'.$submenu_item['elementname'].'</a>';
                                                    }else{
                                                        $print .= '<a href="/">'.$submenu_item['elementname'].'</a>';
                                                    }
                                                    break;
                                                case 'product_category':
                                                    $print .= 'a href="/'.strtolower($this->site['lang']['skrot']).'/'.$submenu_item['friendlyurl'].'.html">'.$submenu_item['elementname'].'</a>';
                                                    break;
                                                case 'product':
                                                    $print .= '<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;
                                            }
                                            $print .= '</li>';
                                         }
                                         if(!isset($menu_items[$key+1]) && !$first){
                                                $print .= '</ul>';
                                            }
                                     }
                                }
                                $bufor .= '<li>'.$print.'</li>';
                            }
                        }
                     }
                     $rownolegle .= $bufor;
                 }
                 if($rownolegle != ''){
                     $print = '<ul>'.$rownolegle.'</ul>';
                 }

             }
         }
    }
    echo $print;
    

?>