Rewrote as a jQuery plugin.