refactor to tidy JS in head

removed multiple wrappers and references to document + window, now all encapsulated in one function.

All file loading now uses custom appendFile method to remove duplication