Here is a link to a forum post with my approach to this problem: move-popup-within-bounds-of-client-window