Add tests, refactor a bit

Add some basic tests and change the event logic a bit so that a user
doesn't have to click in the body of IE9/IE8 for the visibilitychange
event to occur