Moved javascript logic into a jQuery plugin