You shouldn't be writing code what is essentially library code in a document.ready function.