Factored out Javascript to separate file

Also use dimensions as read from the HTML instead of hardcoded.