Fix  a lot of undefined, unused or shared vars

I was curious about your comment about the memory usage, since it ran against my expirience. So I changed to lot of vars to be only declared when used (and subsequently recycled when the function has run).

I did a small benchmark using the Chrome Devtool. It seems that my version is faster (average 17fps vs 19fps). Memory usage doesn't change significantly (55.4m vs 55.1m) 

branch master:
![version theirs screenshot 2014-05-20 12 03 46](https://cloud.githubusercontent.com/assets/183302/3025643/239df170-e008-11e3-86f5-d02b407279bb.png)
![version theirs screenshot 2014-05-20 12 03 49](https://cloud.githubusercontent.com/assets/183302/3025661/49c272ea-e008-11e3-9352-e2714e177228.png)

my branch:
![version mine screenshot 2014-05-20 12 04 46](https://cloud.githubusercontent.com/assets/183302/3025663/530c2be8-e008-11e3-93ce-37d17a7d300a.png)
![version mine screenshot 2014-05-20 12 05 01](https://cloud.githubusercontent.com/assets/183302/3025665/550a1360-e008-11e3-9f96-dc6965c1a3ea.png)


The project I used is here: https://www.dropbox.com/s/8cbitgxl81iej6n/project.zip