All you ever need to know about scope and functions in JavaScript is in this paragraph:
“So the rule is you can put function myfunc(){} definitions anywhere but var myfunc=function(){} have to be placed before the function is used.”
http://www.i-programmer.info/programming/javascript/3722-private-functions-in-javascript.html