/**
 * Register-Array zur ID-Erstellung bei Multi-Popups
 */
var __mPopup_Multipop_Registry = Array();

/**
 * Register-Variable zur Zwischenspeicherung des Singlepopups
 */
var __mPopup_Singlepop_Registry = null;

/**
 * mPopup dient zum Öffnen von Popup Fenstern
 *
 * Abhängig von MooTools v1.11
 *
 * @encoding  utf-8
 *
 * @author    Matthias Göbels <goebels@medienpark.net>
 * @version   18.10.2007 RC 1 Beta
 * @copyright 2007 medienPARK GmbH & Co.KG.
 */
var mPopup = new Class(
{
  /**
   * Initialisiert das mPopup Objekt
   *
   * Verfügbare Optionen:
   * - multiple  boolean   True wenn mehrere Selektiert(Voreinstellung) werden sollen, ansonsten false
   * - reorder   boolean   True wenn die Select-element sortiert werden sollen(Voreinstellung), ansonsten false
   * - dependent string    'yes' wenn die Popups beim schließen des Fensters mit geschlossen werden sollen
   *                       default mässig ist 'no' eingestellt
   * - height    int       Höhe des Fensters in Pixeln
   * - hotkeys   string    Wenn nein (no), werden Tastaturbefehle zum Steuern des Browsers in dem Fenster
   *                       deaktiviert. Wenn ja (yes = Voreinstellung), bleiben Tastaturbefehle des
   *                       Browsers in dem Fenster gültig.   'yes',
   * - innerHeight int     Höhe des Anzeigebereichs des neuen Fensters in Pixeln
   * - innerWidth  int     Breite des Anzeigebereichs des neuen Fensters in Pixeln
   * - left        int     Horizontalwert der linken oberen Ecke des neuen Fensters in Pixeln
   * - location    string  Wenn ja (yes), erhält das Fenster eine eigene Adresszeile. Wenn nein (no),
   *                       erhält es keine Adresszeile. Voreinstellung ist no, beim Internet Explorer jedoch
   *                       nur, wenn die Optionenzeichenkette mindestens eine Option enthält. Netscape 6.1
   *                       interpretiert diese Angabe nicht.
   * - menubar:    string  Wenn ja (yes), erhält das Fenster eine eigene Menüleiste mit Browser-Befehlen.
   *                       Wenn nein (no), erhält es keine Menüleiste. Voreinstellung ist no, beim Internet
   *                       Explorer jedoch nur, wenn die Optionenzeichenkette mindestens eine Option enthält.
   *                       Beachten Sie: Bei Safari ist die Menüleiste wie auf dem Macintosh üblich nicht
   *                       Bestandteil des Fensters, kann also nicht ausgeblendet werden.
   * - multipopup  boolean Wenn true können mehrere Fenster geöffnet werde, wenn false wir immer nur ein Popup
   *                       geöffnet.
   * - resizable   string  Wenn ja (yes), kann der Anwender das Fenster in der Größe verändern. Wenn nein
   *                       (no), kann er die Fenstergröße nicht ändern. Voreinstellung ist no, beim Internet
   *                       Explorer jedoch nur, wenn die Optionenzeichenkette mindestens eine Option enthält.
   * - screenX     int     Horizontalwert der linken oberen Ecke des neuen Fensters in Pixeln
   * - screenY     int     Vertikalwert der linken oberen Ecke des neuen Fensters in Pixeln
   * - scrollbars  string  Wenn ja (yes), erhält das Fenster Scroll-Leisten. Wenn nein (no), kann der
   *                       Anwender in dem Fenster nicht scrollen. Voreinstellung ist no, beim Internet
   *                       Explorer jedoch nur, wenn die Optionenzeichenkette mindestens eine Option enthält.
   * - status      string  Wenn ja (yes), erhält das Fenster eine eigene Statuszeile. Wenn nein (no), erhält
   *                       es keine Statuszeile. Voreinstellung ist no, beim Internet Explorer jedoch nur,
   *                       wenn die Optionenzeichenkette mindestens eine Option enthält.
   * - toolbar     string  Wenn ja (yes), erhält das Fenster eine eigene Werkzeugleiste. Wenn nein (no),
   *                       erhält es keine Werkzeugleiste. Voreinstellung ist no, beim Internet Explorer
   *                       jedoch nur, wenn die Optionenzeichenkette mindestens eine Option enthält.
   * - top         int     Vertikalwert der linken oberen Ecke des neuen Fensters in Pixeln
   * - width       int     Breite des neuen Fensters in Pixeln
   * 
   * @param string url Adresse zu der Seite die in dem neuen Fenster geladen werden soll.
   * @param array  options   enthält die Optionen die für diese Instanz
   *                         angewandt werden sollen.
   */
  initialize: function(url, options)
  {
    this.setOptions(
    {
        dependent:   'no',
        height:      null,
        hotkeys:     'yes',
        innerHeight: null,
        innerWidth:  null,
        left:        null,
        location:    'no',
        menubar:     'no',
        multipopup:  true,
        resizable:   'no',
        screenX:     null,
        screenY:     null,
        scrollbars:  'no',
        status:      'no',
        toolbar:     'no',
        top:         null,
        width:       null
    }, options);
    
    /* Popup ID generieren */
    if(this.options.multipopup == true)
    {
      this.id = __mPopup_Multipop_Registry.length;
      __mPopup_Multipop_Registry.push(null);
    }
    else
    {
      this.id = 'Single';
      __mPopup_Singlepop_Registry = this;
    }
    
    this.url = url;
    /* ### ACHTUNG ### Das this.title Attribut darf KEINE Minuszeichen beinhalten */
    this.title = 'mPopup' + this.id;
    this.window = null;
    
    /* Dimensionen checken */
    if(this.options.width == null)
    {
      this.options.width = 400;
    }
    if(this.options.height == null)
    {
      this.options.height = 300;
    }
    
    /* Positionierung setzen */
    this.setPosition();
    
    /* Events zuweisen */
    this.addEvents();
  },
  
  /**
   * Erstellt die benötigten Events
   *
   * Weißt die benötigten Events den Elementen zu und erstellt diese.
   */
  addEvents: function()
  {
    /* event zum schließen der popups beim schließen der seite */
    if(this.options.dependent == 'yes')
    {
      window.addEvent(
        'unload',
        function(e)
        {
          this.close();
        }.bind(this)
      );
    }
  },
  
  /**
   * Setzt die Position des Fensters
   *
   * Übernimmt die vorgegebenen Positionierung für das neue Fenster
   * oder berechnet diese anhand der Bildschirmmaße und positioniert das
   * Fenster mittig
   */
  setPosition: function()
  {
    /* Positionierung bei Bedarf setzen */
    if(this.options.top == null && this.options.screenY == null)
    {
      if(screen.height)
      {
        this.options.top = ((screen.height - this.options.height) / 2);
      }
      else
      {
        this.options.top = 100;
      }
      this.options.screenY = this.options.top;
    }

    if(this.options.left == null && this.options.screenX == null)
    {
      if(screen.width)
      {
        this.options.left = ((screen.width - this.options.width) / 2);
      }
      else
      {
        this.options.left = 100;
      }
      this.options.screenX = this.options.left;
    }
    
    if(this.options.top != null && this.options.screenY == null)
    {
      this.options.screenY = this.options.top;
    }
    
    if(this.options.left != null && this.options.screenX == null)
    {
      this.options.screenX = this.options.left;
    }
    
    if(this.options.top == null && this.options.screenY != null)
    {
      this.options.top = this.options.screenY;
    }
    
    if(this.options.left == null && this.options.screenX != null)
    {
      this.options.left = this.options.screenX;
    }
  },
  
  getConfigString: function()
  {
    var configArray = new Array();
    
    if(window.gecko){ configArray.push('dependent=' + this.options.dependent); }
    if(this.options.height != null){ configArray.push('height=' + this.options.height); }
    if(window.gecko){ configArray.push('hotkeys=' + this.options.hotkeys); }
    if(window.gecko && this.options.innerHeight != null){ configArray.push('innerHeight=' + this.options.innerHeight); }
    if(window.gecko && this.options.innerWidth != null){ configArray.push('innerWidth=' + this.options.innerWidth); }
    if(this.options.left != null){ configArray.push('left=' + this.options.left); }
    configArray.push('location=' + this.options.location);
    if(window.gecko || window.ie){ configArray.push('menubar=' + this.options.menubar); }
    if(window.gecko || window.ie || window.webkit || window.webkit419 || window.webkit420){ configArray.push('resizable=' + this.options.resizable); }
    if((window.gecko || window.webkit || window.webkit419 || window.webkit420) && this.options.screenX != null){ configArray.push('screenX=' + this.options.screenX); }
    if((window.gecko || window.webkit || window.webkit419 || window.webkit420) && this.options.screenY != null){ configArray.push('screenY=' + this.options.screenY); }
    configArray.push('scrollbars=' + this.options.scrollbars);
    if(!window.opera){ configArray.push('status=' + this.options.status); }
    if(!window.opera){ configArray.push('toolbar=' + this.options.toolbar); }
    if(this.options.top != null){ configArray.push('top=' + this.options.top); }
    if(this.options.width != null){ configArray.push('width=' + this.options.width); }
    
    return configArray.join(', ');
  },
    
  /**
   * Öffnet das neue Fenster
   *
   * Erstell das neue Fenster Objekt und zeigt das neue Fenster an!
   */
  open: function()
  {
    if(this.window != null)
    {
      this.window.close();
    }
    
    this.window = window.open(this.url, this.title, this.getConfigString());
    if(!this.options.multipopup)
    {
      __mPopup_Singlepop_Registry = this.window;
    }
    
    if(this.window != null)
    {
      this.window.focus();
    }
  },
  
  /**
   * Schließt das zuvor geöffnete Fenster
   */
  close: function()
  {
    if(this.options.multipopup)
    {
      if(this.window != null)
      {
        this.window.close();
      }
    }
    else
    {
      if(__mPopup_Singlepop_Registry != null)
      {
        var tmp = __mPopup_Singlepop_Registry;
        __mPopup_Singlepop_Registry = null;
        tmp.close();
        tmp = null;
      }
    }
  }
});

// Das Popup Objekt um das Options Objekt erweitern
mPopup.implement(new Options);
