This piece of code is pure evil:
// AHAH is not being nice to us and doesn't know the "other" button (that is,
// either "Upload" or "Delete") yet. Which in turn causes it not to attach
// AHAH behaviours after replacing the element. So we need to tell it first.
Suppose you're always adding a
Drupal.settings.foo.bar = 1; setting in PHP, i.e. on the server side.
And on the client side, you're adding a
Drupal.settings.foo.baz = 2; setting.
Then you upload a file using filefield (or an image through imagefield, because imagefield uses filefield). Because you're actually including all settings again, all of them will be overwritten!
Drupal.settings.foo.baz will no longer exist.
You should only output the settings that you actually need to prevent problems in other modules, which is the
Drupal.settings.ahah part. Hierarchical Select suffered from this: http://drupal.org/node/354445.