Edited to pass JSHint/Lint (and added directives for JShint/Lint)

- most visible change: tabs->spaces
- no *functional* changes
quite a few minor changes like moving variable definitions around to pass JSHint/Lint