
/*
 * This Module is added to the Game state to handle the interruption events.
 * Events are added to an area specific queue. If a player travels to a
 * location in an area where there's a waiting interrupt, the interrupt event
 * is executed instead of the location being loaded.
 */
var InterruptQueue = {

  /*
   * The isInterrupted() function returns true if an event is waiting in the
   * interrupt queue for the given area, or if there is an event waiting in
   * the global event queue.
   */
  isInterrupted: function(area) {
    var q = this.interruptQueue;

    return (q && q['global'] && q['global'].length > 0) ||
           (q && q[area] && q[area].length > 0)
  },


  /*
   * The addInterrupt() function adds an event code to the interrupt queue for
   * the given area.
   */
  addInterrupt: function(area, event) {
    if (this.interruptQueue == null) {
      this.interruptQueue = {};
    }
    if (this.interruptQueue[area] == null) {
      this.interruptQueue[area] = [];
    }

    Data.getEvent(event)
    if (area != 'global') {
      Data.getArea(area)
    }

    if (this.interruptQueue[area].indexOf(event) < 0) {
      this.interruptQueue[area].push(event);
    }
  },


  /*
   * The removeInterrupt() function removes and returns the head event from
   * the area queue, or the global queue. Global events take precedence over
   * area events.
   */
  removeInterrupt: function(area) {
    if (this.isInterrupted(area)) {
      if (this.interruptQueue['global'].length > 0) {
        return this.interruptQueue['global'].shift();
      }
      return this.interruptQueue[area].shift();
    }
    return null;
  }

};
