Handle relative elements correctly, as well as elements at the edges

of a page. Also fixes a few jshint issues, like redeclaring variables
and bad line breaking.

The `position: relative` handling is built into jQuery in its calculation of `.offset()`. This does the same checking as jQuery for the positioning of the parent elem.