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

        /* zwiń menu edycji dokumentu i mapy importu */
        groupList = jQuery('ul.ulstylegroupkat_0');
        jQuery(groupList).each(function(){
            el = jQuery(this).find('li');
            jQuery(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");
            if (parent.attr("id") == undefined){
                parent = $(this).parents().filter("#VMENUP");
            }

            var left = 0;
            if (parent.attr("id") == 'VMENUL'){
                left =  $(this).width();
            } else { // VMENUP
                left = -$(this).width();
            }

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

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

        });

        $('#VMENUL ul, #VMENUP 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);
        });
    }

}

jQuery(document).ready(function(){

    var menu = new Menu();
    menu.collapseAll();
    menu.bindAll();

    resizeShadows(); // sys.UIActions.js

});
