refactored js to function as a jquery plugin