reorganize javascript files
factor out js and head, fix a few js bugs