Converted javascript into an AMD module.

Tested and ready for merging.