refactored some common code under buildPopup (formerly buildPopupContentHTML).