Moved the script variables and helper function into a closure

I saw that you were looking for some JavaScript feedback (did you really *just* start learning JS?), and I needed some git/GitHub practice, so I figured I'd try a few commits.

Most of my changes are JavaScript best practices related, with the removal of globals in favor of a closure. Actually, with these changes and a few other tweaks, you could make this into a nicely configurable library (which I might do on my own branch).

If you want any specific reasons I made these changes, feel free to ask here. Likewise, if I broke git etiquette or did anything wrong, please tell me!