Complete rewrite as a jQuery plugin.