here is the code for menu.

Code:
 (document).ready(function(){

    $("#main-nav li a.main-link").hover(function(){
    $("#main-nav li a.main-link").removeClass("active");
    $(this).addClass("active");
    $("#sub-link-bar").animate({
    height: "40px"
    });
    $(".sub-links").hide();
    $(this).siblings(".sub-links").fadeIn();

    });


    $("#main-nav").mouseleave(function(){

    $("#main-nav li a.main-link").removeClass("active");
    $(".sub-links").fadeOut();
    $("#sub-link-bar").animate({
    height: "10px"
    });
    });
    });
I want to add functionality so that

1) sub menu should be closed if mouse moved down

2) sub menu should be closed if page is scrolled down

3) sub menu should be active on hover

Please reply. Thanks.