Added 2 new event:onBeforeClose and onBeforeOpen

...thanks to paul for suggesting this a while ago
Minor updates to keep JSHint happy