Issue #1685140 by frega, seutje, ethanw, nod_: Refactor collapse.js.

PR for https://drupal.org/node/1685140