BLG.menuHandler = function(menu) {               
    
    //OBIEKTY
    var middle = menu.find('.menu_middle_tab');
    var bottom = menu.find('.menu_bottom_tab');
    var bottom_wrapper =  menu.find('.menu_bottom_wrapper');
    var bottom_menu = bottom.find('.menu_bottom_content');
    var current_submenu = $('.menu_bottom_wrapper[name=0]');    
    var current_bottom = 0;
    var white_dim = $('.main_area .white_dim');
    var first_time = 0;
    var link = $('.link');
    var logo = $('.logo');
      
    //MENU MIDDLE  
    //slide_down_menu(middle.find('h1'));
    
    middle.find('td').click(function(){
        
        var id = $(this).find('h1').attr('name');
         slideMenuCore(id);
    });
    
    slide_down_menu($('.question_mark img'));
    slide_down_menu($('.labels_el'));
    link.click(function(){        
        slideMenuCore($(this).attr('name'));
    });
    logo.click(function(){ 
        middle.find('td').removeClass('menu_middle_selected_active'); 
        bottom_menu.find('td').removeClass('menu_bottom_selected_active'); 
        $('.menu_bottom_tab').find('h1[name=0]').parent().addClass('menu_bottom_selected_active');      
        slideMenuCore(0,'no_slide');
    });
    
    
    function slide_down_menu(elementt){       
        elementt.click(function(){        
          var id = $(this).attr('name');
          if(id!=='6'){
          slideMenuCore(id);
          }        
        });                        
    }
    
    function slideMenuCore(id, action){                              
        /* Rozwijanie i zwijanie dolnego menu*/                                                       
        /* Jeśli drugie kliknięcie w to samo menu */                                                                                       
        if(current_submenu.attr('name')==id) {                        
            if(first_time==0) { current_submenu.find('.text[name=0]').css('display','block'); first_time=1};
            $('#slider1').anythingSlider(1);
            current_submenu.slideToggle();             
        /* Jeśli kliknięcie w inne menu */
        } else {                        
            if(first_time==0) { current_submenu.find('.text[name=0]').css('display','block'); first_time=1};        
            current_submenu.slideUp(700,'swing',function(){
                if(id>=1000 || id==1){                    
                    var height = BLG.getResolution();                    
                    var height = height.H - 116 - 24;                    
                    bottom_wrapper.find('.content').css('height',height+'px');
                     
                } else {                    
                    bottom_wrapper.find('.content').css('height','246px');
                }                 
                /* Ukrycie obecnej warstwy z FLASHem */               
                //$('.flash_ob').css('z-index','-1');
                /* Ukazanie warstwy FLASHowej dla obecnego menu */
                //$('.flash_ob[name='+id+']').css('z-index','1');
                
                //ukrywanie obecnego tekstu  
                //alert('Teraz current_bottom to:'+current_bottom);
                //alert(current_submenu.find('.text[name='+current_bottom+']').html());    
                current_submenu.find('.text[name='+current_bottom+']').css('display','none');
                
                //odznaczenie klasy active                
                bottom_menu.find('td').removeClass('menu_bottom_selected_active'); 
                $('.menu_bottom_tab').find('h1[name=0]').parent().addClass('menu_bottom_selected_active');
                 
                
                //alert('Zmieniam current_bottom na 0');
                current_bottom=0;                                                                
                
                /* Rozwinięcie obecnego menu */
                current_submenu = $('.menu_bottom_wrapper[name='+id+']');                 
                current_submenu.find('.text[name=0]').css('display','block');
                
                                                                                                                   
                if(action!='no_slide') current_submenu.slideDown(1400,'swing',function(){                
                    if(id==1){
                    $('.miniatures_container').css('overflow','auto')
                    $('.miniatures_container').jScrollPane(); 
                    }
                }
                
                );    
                //white_dim.fadeOut(600);                         
            });            
        }                                 
                                            
        BLG.setResolution($('.main_wrapper'), $('.main_area .background_image'),id);                     
    }
      
    middle.find('td').hover(
         function () {
            $(this).addClass('menu_middle_selected');
          }, 
          function () {
            $(this).removeClass('menu_middle_selected');
          }
    ).click(function(){
        //alert($(this).children('h1').attr('name'));
        middle.find('td').removeClass('menu_middle_selected_active');
        if($(this).children('h1').attr('name')!=='6') $(this).addClass('menu_middle_selected_active');
    });
    
    //MENU BOTTOM  
    bottom.find('td')
    .hover(
         function () {
            $(this).addClass('menu_bottom_selected');                                                   
          }, 
          function () {
            $(this).removeClass('menu_bottom_selected');
          }
    )
    .click(function(){
        //alert('Dolne menu click. Current bottom przed: '+current_bottom+ 'A cuurent_submenu to:' +current_submenu);              
        
        //zabarwieni
        bottom.find('td').removeClass('menu_bottom_selected_active');
        $(this).addClass('menu_bottom_selected_active');   
        
        //warunek ze nie podmenu w zakladce realizacje 
         //if($(this).attr('name')=='realization'){alert('tak'); }
        if($(this).attr('name')!=='realization'){
            current_submenu.find('.text[name='+current_bottom+']').css('display','none');    
            var id = $(this).find('h1').attr('name');
            current_bottom = id;
            //alert('Dolne menu click. Current bottom po: '+current_bottom);
            current_submenu.find('.text[name='+current_bottom+']').css('display','block');                
            //Debbug.object_iterate(current_submenu);
        } else {
            //Zakladka realizacje
            var id = $(this).find('h1').attr('name');
            switch (id) {
            case "0":
            idd = 0;            
            //$('#opis_container').load('php/ajax/get_realization_list.php?id='+id+'&type=miniatures');
            $('#opis_container').load('php/ajax/get_kategorie_list.php');
            $('.karuzela').load('php/ajax/get_realization_miniatures_kategorie.php?id=0',function(){
                $('.miniatures_container').jScrollPane();
            });
            break;
            case "1":
            idd = 0;            
            $('#opis_container').load('php/ajax/get_realization_list.php?type=klienci');
            $('.karuzela').load('php/ajax/get_realization_miniatures.php?id='+idd,function(){
                $('.miniatures_container').jScrollPane();
            });
            break;            
            case "2":            
            idd=0;
            $('#opis_container').load('php/ajax/get_realization_list.php?id='+id+'&type=sort');
            $('.karuzela').load('php/ajax/get_realization_miniatures.php?id='+idd+'&sort=DESC',function(){
                $('.miniatures_container').jScrollPane();                                                                                                  
            });
            
            break;
            default:
            idd=0;
            }
            
            
        
        }
    });
    
    
    //Wcisniety przycisk Labela
    $('.labels_el').click(function(){
        $(this).find('.pushed').addClass('active');    
    });
    
    $('.labels_el').hover(
         function () {
             $(this).find('.pushed').addClass('pushed_active');    
          }, 
          function () {
             $(this).find('.pushed').removeClass('pushed_active');    
          }
    );
    
    
    //Zamykanie menu
    bottom_wrapper.find('.close, .close_left').click(function(){
        $(this).parents('.menu_bottom_wrapper').slideToggle();
    }); 
}
