Refactored JavaScript

* put code into closure to avoid potential intermixing with other scripts
* stored reused jQuery calls into variables to speed things up

* tested locally, but could not verify for sure if everything still works