_Dialogs = new Hash();

IxDialog = Class.create( {
  initialize : function(options) {
    this._id = options.id;
    this._isOpen = false;

    this._dialogNode = $(document.createElement("div"));
    this._dialogNode.addClassName('IxDialog');
    this._dialogNode.writeAttribute('id',this._id);

    this._titleBar = $(document.createElement("div"));
    this._titleBar.addClassName('IxDialogTitleBar');

    this._title = $(document.createElement("div"));
    if (options.title)
      this.setTitle(options.title);

    this._title.addClassName('IxDialogTitle');

    this._buttonsContainer = $(document.createElement("div"));
    this._buttonsContainer.addClassName('IxDialogTitleBarButtons');

    this._titleBar.appendChild(this._buttonsContainer);
    this._titleBar.appendChild(this._title);

    this._dialogNode.appendChild(this._titleBar);
    this._dialogContent = $(document.createElement("div"));
    this._dialogContent.addClassName('IxDialogContent');
    this._dialogNode.appendChild(this._dialogContent);

    this._parentNode = document.getElementsByTagName('body')[0];
    this._centered = options.centered;

    this._windowResizedListener = this._onWindowResized.bindAsEventListener(this);
    this._listeners = new Array();

    Event.observe(window, "resize", this._windowResizedListener);

    _Dialogs.set(this._id, this);
  },
  open : function() {
    this._parentNode.appendChild(this._dialogNode);
    this._isOpen = true;
    this._recalculateSize();
  },
  close : function() {
    this._parentNode.removeChild(this._dialogNode);
    this._isOpen = false;
    this._dialogNode = undefined;
    Event.stopObserving(window, "resize", this._windowResizedListener);
    _Dialogs.unset(this._id);

    while (this._listeners.length > 0)
      this._listeners.pop();
  },
  center : function() {
    var dialogDims = this._dialogNode.getDimensions();
    var viewportDims = document.viewport.getDimensions();
    var scrollOffs = document.viewport.getScrollOffsets();
    var left = (viewportDims.width / 2) - (dialogDims.width / 2);
    var top = (viewportDims.height / 2) - (dialogDims.height / 2) + scrollOffs.top;

    this._dialogNode.setStyle( {
      top :top + 'px',
      left :left + 'px'
    });
  },
  getContentElement : function() {
    return this._dialogContent;
  },
  loadContent : function(url, options) {
    var _this = this;

    new Ajax.Request(url, {
      method :options.method ? options.method : 'get',
      parameters :options.parameters,
      onCreate : function(transport) {
        if (options.onCreate)
          options.onCreate(transport);
      },
      onFailure : function(transport) {
        if (options.onFailure)
          options.onFailure(transport);
      },
      onSuccess : function(transport) {
        _this._dialogContent.innerHTML = transport.responseText;
         _this._recalculateSize();
        if (options.onSuccess)
          options.onSuccess(transport);
          
      }
    });
  },
  setSize : function(width, height) {
    this._dialogNode.setStyle( {
      width :width,
      height :height
    });

    this._recalculateSize();
  },
  addDialogListener : function(listener) {
    this._listeners.push(listener);
  },
  setTitle : function(title) {
    this._title.innerHTML = title;
    this._recalculateSize();
  },
  _onWindowResized : function(event) {
    this._recalculateSize();
  },
  _fire : function(eventName, extraInfo) {
    for ( var i = 0; i < this._listeners.length; i++) {
      this._listeners[i].call(this, Object.extend( {
        name :eventName
      }, extraInfo));
    }
  },
  _recalculateSize : function() {
    if (this._isOpen == true) {
      if (this._centered == true) {
        this.center();
      }

      var contentElement = $(this.getContentElement());

      if (contentElement) {
        var maxHeight = Element.getMaxHeight(contentElement);
        contentElement.setStyle({
          height: maxHeight + 'px'
        });
      }
      
      this._fire("resized", {});
    }
  }
});

IxAlert = Class.create(IxDialog, {
  initialize : function($super, message, title) {
    $super({id: 'alertDialog', centered :true, title: title});

    this._dialogNode.addClassName('IxAlert');

    this._messageContent = $(document.createElement("div"));
    this._messageContent.innerHTML = message;
    this._messageContent.addClassName('IxAlertMessageContent');

    this._buttonsContainer = $(document.createElement("div"));
    this._buttonsContainer.addClassName('IxAlertButtons');
    this._okButton = $(document.createElement("div"));
    this._okButton.addClassName('IxAlertOkButton');
    this._okButton.innerHTML = 'Ok';
    this._buttonsContainer.appendChild(this._okButton);

    this.getContentElement().appendChild(this._messageContent);
    this.getContentElement().appendChild(this._buttonsContainer);

    this._okButtonClickedListener = this._onOkButtonClicked.bindAsEventListener(this);
    Event.observe(this._okButton, "click", this._okButtonClickedListener);

    this.open();
  },
  close : function($super) {
    Event.stopObserving(this._okButton, "click", this._okButtonClickedListener);

    $super();
  },
  _onOkButtonClicked : function(event) {
    this.close();
    this._fire("closed", {});
  }
});

function getDialog(id) {
  return _Dialogs.get(id);
}
