Hamiltonian formulation for surface waves in a layered fluid