/* 
 * JK
 * Klasa zastępująca funkcjonalność sys.menutree.js
 * + nie wymaga wstawiania listy grup do dokumentu,
 * + obsługuje także menu redaktora i administratora,
 *
 * TODO:
 * - timeout dla ukrywania pozycji przy mouseleave
 *   http://api.jquery.com/delay/
 */
function Menu(){
    
    this.collapseAll = function(){
        var groupList = $('ul.menubt00');
        var el;
        /* pomiń menu administratora i redaktora */
        $(groupList).each(function(){
            el = $(this).find('li');
            $(el).children('ul').hide();
        });

        /* zwiń menu edycji dokumentu i mapy importu */
        groupList = $('ul.ulstylegroupkat_0');
        $(groupList).each(function(){
            el = $(this).find('li');
            $(el).children('ul').hide();
        });
    }

    this.bindAll = function(){
        /* dotyczy menu w edytorze dokumentów i map */
        $('a[class^="menubtresc"]').click(function(){
            $(this).next('ul').toggle();
        });

        $('a[class*="menubt"]').not('[class^="menubtresc"]').mouseenter(function(){
            if ($(this).parent().parent().attr("id") == "path") {
                return; // zignoruj menu wyświetlane na środku
            }

            var elementHovered = this;

            $(this).parent().parent().children().not(this).each(function(){
               $(this).children().each(function(){
                   if(elementHovered == this) return;
                    $(this).next('ul').fadeOut(200);
                });
             });

            var parent = $(this).parents().filter("#VMENUL, #VMENUP, #HMENU");

            var left = 0;
            var top = $(this).position().top;

            if (parent.attr("id") == 'VMENUL'){
                left =  $(this).width();
            } else if (parent.attr("id") == 'VMENUP'){ 
                left = -$(this).width();
            } else if (parent.attr("id") == 'HMENU'){
                if ($(this).parent('li.menubt01').length){
                    left =  $(this).width();
                } else {
                    left = $(this).position().left;
                    top += 24;
                }
            }

            $(this).next('ul').css('position', 'absolute')
                .css("top", top)
                .css("z-index", "10") // nad dokumentem
                .css("left", left).show();

        });

        $('#VMENUL ul, #VMENUP ul, #HMENU ul').mouseleave(function(){
            //var t = setTimeout(menuHide, 1, this);

            if ($(this).hasClass('menubt00')){
                $('ul.menubt01').fadeOut(200); // .not('id^="treeAdm"').not('id^="tree-NIE"')
            }
            else{
                $(this).children().each(function(){
                    $(this).children().each(function(){
                        if ($(this).next('ul').length > 0){
                            $(this).next('ul')
                            .not('[class^="sysedit"]')
                            .not('[class^="ulstylegroupkat"]')
                            .not('[id^="path"]') // nie ukrywaj menu wyświetlanego na środku
                            .is(".class") // tylko jesli posiada klasę (znikały listy w ciałach dokumentów
                            .fadeOut(200); // .not('id^="treeAdm"').not('id^="tree-NIE"')
                        }
                    });
                });
            }
        });

        $('#VMENUL ul').eq(0).mouseenter(function(){

            $('#tip').hide();
        });
        
        $('#VMENUL ul').eq(0).mouseleave(function(){

            $('#tip').fadeIn(500);
        });
    }

}

$(document).ready(function(){
    var menu = new Menu();
    menu.collapseAll();
    menu.bindAll();

    resizeShadows(); // sys.UIActions.js
});
