var maxHeight = 150;

jQuery(function(){

    jQuery(".dropdown > li").hover(function() {
    
         var jQuerycontainer = jQuery(this),
             jQuerylist = jQuerycontainer.find("ul"),
             jQueryanchor = jQuerycontainer.find("a"),
             height = jQuerylist.height() * 1.1,       // make sure there is enough room at the bottom
             multiplier = height / maxHeight;     // needs to move faster if list is taller
        
        // need to save height here so it can revert on mouseout            
        jQuerycontainer.data("origHeight", jQuerycontainer.height());
        
        // so it can retain it's rollover color all the while the dropdown is open
        jQueryanchor.addClass("hover");
        
        // make sure dropdown appears directly below parent list item    
        jQuerylist
            .show()
            .css({
                paddingTop: jQuerycontainer.data("origHeight")
            });
        
        // don't do any animation if list shorter than max
        if (multiplier > 1) {
            jQuerycontainer
                .css({
                    height: maxHeight,
                    overflow: "hidden"
                })
                .mousemove(function(e) {
                    var offset = jQuerycontainer.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - (jQuerycontainer.data("origHeight") * multiplier);
                    if (relativeY > jQuerycontainer.data("origHeight")) {
                        jQuerylist.css("top", -relativeY + jQuerycontainer.data("origHeight"));
                    };
                });
        }
        
    }, function() {
    
        var jQueryel = jQuery(this);
        
        // put things back to normal
        jQueryel
            .height(jQuery(this).data("origHeight"))
            .find("ul")
            .css({ top: 0 })
            .hide()
            .end()
            .find("a")
            .removeClass("hover");
    
    });
    
    // Add down arrow only to menu items with submenus
    jQuery(".dropdown > li:has('ul')").each(function() {
        jQuery(this).find("a:first").append("<img src='http://www.doubleeint.com/cms/images/icons/arrow-down.gif' />");
    });
    
    
});




