Moved JS to its own file.

This included refactoring so its moving towards a more reusable class
and cleaning up some of the code.