Tidy up media.js in preparation for chrome integration

This is working towards chrome integration and doing a lot of rework to make the structures better and easier for integrating later.