Refactored js in to a jquery plugin

Refactored js in to a jquery plugin.