var HtmlWindow = new Class({

  Implements: [Options, Events],

  options: {
    onSuccess: null
  },

  initialize: function(options)
  {
    this.setOptions(options);

    this.wSize = window.getSize();
    this.wScroll = window.getScroll();
    if($$('.htmlWindow').length > 0)
    {
      this.window = $$('.htmlWindow')[0];
    }
    else
    {
      this.window = new Element('div', { 'class': 'htmlWindow' }).inject(document.body);
    }
    this.mask = new Mask(document.body, { 'class': 'htmlWindowMask' });
    this.spinner = new Element('div', { 'class': 'spinner' });
    this.window.store('htmlWindow', this);
  },

  show: function()
  {
    this.mask.show();
    this.updatePosition();
    this.window.set('html', '').show();
    return this;
  },

  hide: function()
  {
    this.window.hide();
    this.mask.hide();
    return this;
  },

  updatePosition: function()
  {
    this.wScroll = window.getScroll();
    var pSize = this.window.measure(function(){ return this.getSize(); });
    var pos = {
      x: Math.abs((this.wSize.x - pSize.x) / 2),
      y: Math.abs((this.wSize.y - pSize.y) / 2  + this.wScroll.y)
    };
    this.window.setPosition(pos);
    return this;
  },

  loadPageSuccess: function(responseJSON, responseText)
  {
    var obj = JSON.decode(responseText);
    this.fireEvent('success', obj);
  },

  loadPage: function(url, message)
  {
    if(typeof message == 'undefined' || message == '') message = 'Lade ...';
    this.spinner.clone().set('html', message).inject(this.window.empty());
    this.updatePosition();

    new Request.JSON({'url': url, method: 'post', onSuccess: this.loadPageSuccess.bind(this) }).send();
  }

});
