// Forwarding callback utility, for callbacks that are in evaled script.
//
// Usage:
// var cookie = forward.register(function() { alert('eval done!'); });
// document.write('forward.oneshot(' + cookie + ');');
//
// oneshot can be called once only with a given cookie.
// notify can can be called repeatedly with the same cookie.
// remove can be used to explicitly remove a cookie.

var forward = {
  map: {},
  counter: 0,

  register: function register(f) {
    forward.map[forward.counter] = f;
    return forward.counter++;
  },

  oneshot: function oneshot(n) {
    var f = forward.map[n];
    if (f) {
      delete forward.map[n];
      if (arguments.length == 2) {
        f(arguments[1]);
      } else {
        f();
      }
    }
  },

  notify: function notify(n) {
    var f = forward.map[n];
    if (f) {
      if (arguments.length == 2) {
        f(arguments[1]);
      } else {
        f();
      }
    }
  },

  remove: function remove(n) {
    delete forward.map[n];
  }
}

