removed global variables and added strict mode

Removed some global variables from for loops because they are slow and error prone.
Also renamed "event" variable to "ev" because it breaks in some cases in IE.
At least added strict mode to prevent following mistakes.