Detect edge of page

I have this netflix like hover menu that pops up when you hover over a movie. Unfortunately, the popup is unaware of its positioning and when you scroll too far up or down it just cuts off the pop up. How can I make it "sense" the horizontal and vertical edges of the screen so it pops the menu up somewhere that is always visible?

An example of what I am talking about can be found here:

Btw sorry if this is not a CSS issue, I...

Detect edge of page

Poster :