Don't use window and document

With this change, our JavaScript can also be executed by
non-brower engines.
Use no browser globals at all
Merge pull request #529 from ahocevar/global

Don't use window and document