Geodesics on Lorentzian manifolds: a variational approach