var maxHeight = 400;

jQuery(function($){

    jQuery(".dropdown > li").hover(function() {
    
         var $container = $(this),
             $list = $container.find("ul"),
             $parent = $(this).parent();
             $anchor = $container.find("a"),
             height = $list.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            
        $container.data("origHeight", $container.height());
      
        // so it can retain it's rollover color all the while the dropdown is open
        $anchor.addClass("hover");
        $container.parent().addClass("hover");
        
        //don't change opacity if there is no drop down
        if ($list.val() != null) {
			jQuery('#content').css('opacity', 0.3);
			
			};

        // make sure dropdown appears directly below parent list item    
        $list
            .show()
            .css({
                paddingTop: $container.data("origHeight") -20
            });
        
        // don't do any animation if list shorter than max
        if (multiplier > 1) {
            $container
                .css({
                    height: maxHeight,
                    overflow: "hidden"
                })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
        }
        
    }, function() {
    
        var $el = $(this);
        
        // put things back to normal
        $el
            .height(jQuery(this).data("origHeight"))
            .find("ul")
            .css({ top: 0 })
      	    .hide()
            .end()
            .find("a")
            .removeClass("hover");

	       jQuery('#content').css('opacity', 1);
	    
    
    });
    
    // Add down arrow only to menu items with submenus
    jQuery(".dropdown > li:has('ul')").each(function() {
       // jQuery(this).find("a:first").append("<img src='images/down-arrow.png' />");
    });
    
    
});



