Changes to make jslint happy

Made some changes to make jslint happy. Unfortunately, jslint made made me chage tabs to spaces, so it looks like I completely changed the file. Which I didn't.

Mainly I prevented variable hoisting, placed function declaration outside of loop and added document as parameter instead of having it being declared at the top.

Great, great little piece of kit, this. Cheers!