declare vars at the top of the function always.  This is due to JS hoisting, see http://cssdeck.com/labs/javascript-variable-hoisting-explained for more info