// ---------------------------------------------------------------------------
// (c) 2006 KystAtlas, Hans Martin Mohn
// ---------------------------------------------------------------------------
// Utility class to aid in connecting events.
//
// The gtEventHandler class helps in the startup phase of an application where
// the target event handler class hasn't loaded yet when an event is fired.
//
// gtEventHandler will store the most recent set of parameters to the event and
// forward them to the target class when it connects to the event.
//
// The gtEventHandler class also supports more than one event sink by holding a
// collection of event handler pointers.
//
// Dependencies:
//

function gtEventHandler()
{
  this.m_sinks = [];
  this.m_args  = null;
  this.m_open  = false;
  this.m_me    = [];
//  alert("Created new gtEventHandler!");
} // end gtEventHandler()


gtEventHandler.prototype.getHandlerCount = function _getHandlerCount()
{
  return this.m_sinks.length;
}; // end getHandlerCount()


gtEventHandler.prototype.isOpen = function _isOpen()
{
  return this.m_open;
}; // end isOpen()


gtEventHandler.prototype.raise = function _raise()
{
  var i;
  if (!this.m_open)
  {
    // Save all parameters in the m_args array.
    // TODO: Handle more than one event! (store events with parameters in a two-dimensional array)
    this.m_args = new Array();
    for (i = 0; i < arguments.length; i++)
    {
      this.m_args.push(arguments[i]);
    }
  }
  else
  {
    // Call all event handlers with the given parameters (regrettably, maximum 10!!!)
    if (arguments.length > 0)
    {
      for (i = 0; i < this.m_sinks.length; i++)
      {
        if (this.m_me[i])
        {
          // NOTE: Broke all old code by calling the method correctly by using the "call()" method!!
          this.m_sinks[i].call(this.m_me[i], arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9]);
        }
        else
        {
          this.m_sinks[i](arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9]);
        }
      }
    }
    else
    {
      for (i = 0; i < this.m_sinks.length; i++)
      {
        if (this.m_me[i])
          this.m_sinks[i].call(this.m_me[i]);
        else
          this.m_sinks[i]();
      }
    }
  }
}; // end raise();



gtEventHandler.prototype.findHandler = function _findHandler(handler)
{
  for (var i = 0; i < this.m_sinks.length; i++)
  {
    if (this.m_sinks[i] == handler)
      return true;
  }
  return false;
}; // end findHandler()



gtEventHandler.prototype.addHandler = function _addHandler(handler, me, open_now)
{
  if (!handler)
    return; // Don't know why this should happen???

  if (typeof(open_now) == "undefined")
    open_now = true;

  if (this.findHandler(handler))
    return;

  this.m_sinks.push(handler);
  this.m_me.push(me);
  if (open_now == true)
    this.open();
}; // end addHandler()



gtEventHandler.prototype.removeHandler = function _removeHandler(handler)
{
  for (var i = 0; i < this.m_sinks.length; i++)
  {
    if (this.m_sinks[i] == handler)
    {
      this.m_sinks.splice(i, 1);
      this.m_me.splice   (i, 1);
      return true;
    }
  }
  return false;
}; // end removeHandler()



gtEventHandler.prototype.removeAllHandlers = function _removeAllHandlers()
{
  this.m_sinks.length = 0;
}; // end removeAllHandlers()



gtEventHandler.prototype.open = function _open()
{
  if (!this.m_open && this.m_args !== null)
  {
    // Call all event handlers with the given parameters (regrettably, maximum 10!!!)
    if (this.m_args)
    {
      for (i = 0; i < this.m_sinks.length; i++)
      {
        if (this.m_me[i])
        {
          // NOTE: Broke all old code by calling the method correctly by using the "call()" method!!
          this.m_sinks[i].call(this.m_me[i], this.m_args[0], this.m_args[1], this.m_args[2], this.m_args[3], this.m_args[4], this.m_args[5], this.m_args[6], this.m_args[7], this.m_args[8], this.m_args[9]);
        }
        else
        {
          this.m_sinks[i](this.m_args[0], this.m_args[1], this.m_args[2], this.m_args[3], this.m_args[4], this.m_args[5], this.m_args[6], this.m_args[7], this.m_args[8], this.m_args[9]);
        }
      }
    }
    else
    {
      for (i = 0; i < this.m_sinks.length; i++)
      {
        if (this.m_me[i])
          this.m_sinks[i].call(this.m_me[i]);
        else
          this.m_sinks[i]();
      }
    }
  }
  this.m_open = true;
  delete this.m_args;
  this.m_args = null;
}; // end open();
