$(document).ready(function() {
    
    var status = '0';
    var dropDownCloseTimer = false;
    var dropDownOpenTimer = false;
    var dropDownTimer = false;
    
    $('#vpnavigation > ul > li').mouseenter(function() {    
        newDropDown = $(' > ul', this);
        
        dropDownTimer = setTimeout(function() {
            elemCnt = newDropDown.context.childElementCount;
        
            if (elemCnt > 1) {
                if (status == 1) {          
                    dropDownOpenTimer = setTimeout(function() {
                        $(newDropDown).slideDown(300);
                        status = 1;
                    }, 400);
                }
        
                if (status == 0) {
                    $(newDropDown).slideDown(300);
                    status = 1; 
                }
            }
        
            if (status == 2) { 
                if (leavedDropDown.context.innerHTML == newDropDown.context.innerHTML && elemCnt > 1) {
                    clearTimeout(dropDownCloseTimer);
                    clearTimeout(dropDownOpenTimer);
                } else {       
                    dropDownOpenTimer = setTimeout(function() {
                        $(leavedDropDown).slideUp(300);
                        if (elemCnt > 1) {
                            $(newDropDown).slideDown(300);
                        }
                        status = 1;
                    }, 400);
                }
            }
        }, 200);
    }).mouseleave(function() {
        clearTimeout(dropDownTimer);
        if (status != 2) {
            leavedDropDown = $(' > ul', this);
            
            status = 2;
            
            dropDownCloseTimer = setTimeout(function() {
                $(leavedDropDown).slideUp(300);
                status = 0;
            }, 1000);
        }
    });
});
