Javascript into separate files

Extract javascript from templates into files and do some magick.