Context menu - conditional style applied for large submenu lists
===

I have a Context menu with Menu - Submenu items. The no of sub menu items is quite dynamic and sometimes more than 30. This causes the sub menu to be too long and hence user is unable to choose the desired sub menu item as its cut.

This context menu is being wired up on right click with different grids on the page. The context menu at the grid1 (top of page) has few sub menu items not appearing.
The context menu which is attached to grid2 just below grid1, which is positioned 50% of the window height, has more problems as very fewer sub menu items appearing.

ideally what I want is to determine the position of X,Y coordinates of the mouse and then take into account no. of sub menu items * 20px (height of each sub menu item) and determine realistically how many sub menu items can be show and hence set a height and overflow style to the context menu's ul tag. In case of grid 2, as its below grid one set a height lower than grid1 as less space is available to show the sub items and apply overflow.
How can this achieved using jQuery?  