/**
 * Mit dieser Klasse können Popups geöffnet und geschlossen werden.
 *
 * Es können Fenster mit verschiedenesten Eigenschaften geöffnet werden.
 * Dabei gibt es zwei Modi die im wesentlichen zu beachten sind, den
 * Single und den Multi Modus. Im Single Modus wird immer nur ein Popup
 * Modus angezeigt und im Multi Modus werden so viele Popups wie gewünscht
 * geöffnet.
 *
 * Beispiele:
 * ----------
 * Öffnen eines einfachen Popups
 * new Popup('http://www.medienpark.net');
 * ----------
 * Öffnen eines einfach Popups mit definierter Größe
 * new Popup('http://www.medienpark.net', {width: 400, height: 300});
 * ----------
 * Öffnen eines einfach Popups mit definierter Position
 * new Popup('http://www.medienpark.net', {top: 100, left: 100});
 * ----------
 * Öffnen eines einfach Popups im Single Modus
 * new Popup('http://www.medienpark.net', {type: 'single'});
 * ----------
 * HINWEIS: Bei gesetztem Target Attribute funktioniert das Popup nicht
 * ----------
 *
 * @category Window
 * @author Matthias Göbels <goebels@medienpark.net>
 * @copyright Copyright by medienPARK 2006
 * @version 01.08.06
 * @access public
 **/
Popup = Jim.Class.create();
Jim.Class.extend(Popup, new Object());
Jim.Version.check(0, 4, 0, 'Popup');

/**
 * Statische Registervariablen !!! NICHT ZU VERÄNDERN !!!
 *
 * @access private
 **/
__Jim_Window_Popup_singlePopup     = null;
__Jim_Window_Popup_multiPopups     = new Array();
__Jim_Window_Popup_single          = null;
__Jim_Window_Popup_singleDependent = false;

/**
 * Öffnet das Popup und erstellt das Popup Objekt
 *
 * @param string URI Hier wird die zu ladende Adresse übergeben.
 * @param array config Hier werden die Eigenschaften des Popups übergeben.
 *                     Erklärungen hierzu im Manual.
 * @return Popup-Objekt 
 * @access public
 **/
Popup.prototype.__construct = function(URI, config)
{
  // Default Eigenschaften setzen
  this.URI = URI;
  this.width     = 400; // Popuphöhe
  this.height    = 300; // Popupbreite
  this.top       = -1; // Popupposition von Oben
  this.left      = -1; // Popupposition von links
  this.scrollbars = 'no'; // Scrollleisten
  this.resizable  = 'no'; // Größe veränderbar
  this.status     = 'no'; // Statusleiste
  // ### ACHTUNG ### Das this.title Attribut darf KEINE Minuszeichen beinhalten
  this.title      = 'popupfenster'+this.ID; // Fenstername
  this.dependent  = 'yes'; // Fenster schließen, wenn Elternfenster geschlossen wird
  this.hotkeys    = 'yes'; // Tastaturbefehle zum Steuern des Browsers in dem Fenster
  this.location   = 'no'; // Adressleiste
  this.menubar    = 'no'; // Menüleiste
  this.toolbar    = 'no'; // Werkzeugleiste
  
  // Eigenschaften aus der Konfiguration setzen
  if(config !== null)
  {
    for(key in config)
    {
      this[key] = config[key];
    }
  }
  
  // Bei nicht gesetztem Abstand von Oben - Vertikal zentrieren
  if(this.top == -1)
  {
    if(screen.height)
    {
      this.top = ((screen.height - this.height) / 2);
    }
    else
    {
      this.top = 100;
    }
  }

  // Bei nicht gesetztem Abstand von Links - Horizontal zentrieren  
  if(this.left == -1)
  {
    if(screen.width)
    {
      this.left = ((screen.width - this.width) / 2);
    }
    else
    {
      this.left = 100;
    }
  }
  
  // this.type aus der Config setzen
  if(config)
  {
    this.type = (config['type'] == 'single') ? 'single' : ((config['type'] == 'multi') ? 'multi' : 'multi');
  }
  else
  {
    this.type = 'multi';
  }

  // Initialmodus laden / speichern
  if(__Jim_Window_Popup_single == null)
  {
    __Jim_Window_Popup_single = this.type;
  }
  else
  {
    this.type = __Jim_Window_Popup_single;
  }
  
  // Popup ID setzen
  if(this.type == 'single')
  {
    this.ID = 'Single';
  }
  else if(this.type == 'multi')
  {
    this.ID = __Jim_Window_Popup_multiPopups.length;
  }
  // Bugfix: Gleiche Fenstertitle vermeiden und so multipopups ermöglichen
  this.title      = 'popupfenster'+this.ID; // Fenstername
  
  // Erstellen des neuen Popups
  if(this.type == 'single')
  {
    this.close();
    __Jim_Window_Popup_singlePopup = window.open(this.URI, this.title, this.getConfigstring());
    __Jim_Window_Popup_singlePopup.focus();
  }
  else
  {
    __Jim_Window_Popup_multiPopups[this.ID] = window.open(this.URI, this.title, this.getConfigstring());
    __Jim_Window_Popup_multiPopups[this.ID].focus();
  }
  
  if(this.dependent == 'yes' || this.dependent != 'no')
  {
    if(this.type == 'single')
    {
      if(!__Jim_Window_Popup_singleDependent)
      {
        Jim.Events.addOnUnload((function(){ if(Jim.typeOfWindow(__Jim_Window_Popup_singlePopup)) { __Jim_Window_Popup_singlePopup.close(); }}));
        __Jim_Window_Popup_singleDependent = true;
      }
    }
    else
    {
      func = eval("func = (function(){ if(Jim.typeOfWindow(__Jim_Window_Popup_multiPopups['" + this.ID + "'])){ __Jim_Window_Popup_multiPopups['" + this.ID +  "'].close(); }})");
      Jim.Events.addOnUnload(func);
    }
  }
}

/**
 * Wandelt die Eigenschaften eines Popups zu einem Konfigurationsstring
 *
 * @return string Konfigurationsstring
 * @access public
 **/
Popup.prototype.getConfigstring = function()
{
  return 'width=' + this.width + 
    ', height=' + this.height + 
    ', top=' + this.top + 
    ', left=' + this.left +
    ', scrollbars=\'' + this.scrollbars + '\'' +
    ', resizable=\'' + this.resizable + '\'' +
    ', status=\'' + this.status + '\'' +
    ', dependent=\'' + this.dependent + '\'' +
    ', hotkeys=\'' + this.hotkeys + '\'' +
    ', location=\'' + this.location + '\'' +
    ', menubar=\'' + this.menubar + '\'' +
    ', toolbar=\'' + this.toolbar + '\''
  ;
}

/**
 * Schließt das Popup, welches vom Objekt erstellt wurde
 *
 * @access public
 **/
Popup.prototype.close = function()
{
  if(this.type == 'single')
  {
    if(Jim.typeOfWindow(__Jim_Window_Popup_singlePopup))
    {
      if(__Jim_Window_Popup_singlePopup.closed !== true)
      {
        __Jim_Window_Popup_singlePopup.close();
      }
    }
  }
  else
  {
    if(Jim.typeOfWindow(__Jim_Window_Popup_multiPopups[this.ID]))
    {
      if(__Jim_Window_Popup_multiPopups[this.ID].closed !== true)
      {
        __Jim_Window_Popup_multiPopups[this.ID].close();
      }
    }
  }
}
