explode awful PHP function that generates markup and a script tag

still awful, but a step in the right direction