A Variational Approach to Nonsmooth Dynamics