Clean up Javascript

Re-use jQuery variables, use higher scope for variables that need it, eliminate unnecessary variables