A variational problem related to self-trapping of an electromagnetic field