Fix race condition in comet startup.

The comet subscribe() function does not wait until a subscription
is acutally formed. To deal with this, the logic to launch the scan
is now done as an event attached to the subscription succeeding.

Also cleaned up the javascript into a proper object.