Clean UI.js

The ui.js now is in a messy state: 
1. Some classes are defined in the UI but some classes are out side.
2. Mass of global functions

This patch does not touch 2. but just move all classes out of ui.js and the next step is to figure out what does every global functions in ui.js do and construct a more semantic way to boostrap.