GLAMER - I. A code for gravitational lensing simulations with adaptive mesh refinement