
// menu events
$(document).ready(function() {
    $("#navigation > li").addClass("lvlOneItem").children("a:first-child").addClass("lvlOne");
    $("#navigation > li > ul").addClass("lvlTwoList").children("li").addClass("lvlTwoItem").children("a:first-child").addClass("lvlTwo");
    $("#navigation > li > ul > li > ul").addClass("lvlThreeList").children("li").addClass("lvlThreeItem").children("a:first-child").addClass("lvlThree");
    $("#navigation > li > ul > li:first-child > a").addClass("lvlTwoFirst");
    $("#navigation > li > ul > li:last-child > a").addClass("lvlTwoLast");

    var lvl2Children = $("#navigation > li > ul > li > ul");
    jQuery.each(lvl2Children, function(i) {
        var prev = $(this).parent().prev();
        var next = $(this).parent().next();

        if ($(this).children("li").length > 0 && prev.children("ul").children("li").length > 0) {
            if ($(this).parent().children("a:first-child").hasClass("lvlTwoLast")) {
                $(this).parent().children("a:first-child").addClass("lastChildsAfter");
            }
            else {
                $(this).parent().children("a:first-child").addClass("hasChildsAfterChild");
            }
        }
        else if ($(this).children("li").length > 0) {
            if ($(this).parent().children("a:first-child").hasClass("lvlTwoLast")) {
                $(this).parent().children("a:first-child").addClass("lastChilds");
            }
            else {
                $(this).parent().children("a:first-child").addClass("hasChilds");
            }
        }

        if (next.children("a:first-child").hasClass("lvlTwoLast")) {
            next.children("a:first-child").addClass("lastAfter");
        }
        else if ($(this).children("li").length > 0) {
            next.children("a:first-child").addClass("afterChilds");
        }
    });

    $("#navigation li > ul > li:last a:first-child").addClass("lastChild");
    //$("#navigation li ul li:first a").addClass("firstSub");
    $("#navigation li ul li:first a").addClass("subs");
    $("#navigation li ul li ul li:first-child a").addClass("firstSub");

    $("li.lvlOneItem").hover(
            function() {
                $(this).children("ul").stop(true, true).slideDown(500);
                $(this).siblings("li").children("ul").stop(true, true).slideUp(500).children("li").children("ul").stop(true, true).slideUp(500).parent().children("a:first-child").removeClass("lvlTwoOpen");
            },
            function() {
            $(this).children("ul").stop(true, true).animate({ padding: 0 }, 500).slideUp(500);
                //$(this).next("li").children("a:first").removeClass("afterOpen");
            });

    $("li.lvlTwoItem").hover(
                    function() {
                        //$(this).siblings("li").children("ul").stop(true, true);
                        if ($(this).children("ul").children("li").length > 0) {
                            $(this).children("ul").stop(true, true).animate({padding: 0}, 500).slideDown(500);
                            $(this).siblings("li").children("ul").stop(true, true).slideUp(3500).parent().children("a:first-child").removeClass("lvlTwoOpen");
                            $(this).children("ul > li > a").addClass("lvlTwoOpen");
                        }
                    }
                        ,
                        function() {
                            if ($(this).children("ul").children("li").length > 0) {
                            }
                        });

                    });