Fix problem with visibilitychange API

Not having desired effect, may revert
Small optimisations on events.js
fix bug which would cause functions not to be added to event listeners
Emit events

newgame, victory, failure, pause, unpause, loaded, error