ns('Carib.Order.Widgets');

/**
 * Send window
 * 
 * @version $Id: send_win.js 400 2009-07-03 15:18:46Z  $
 */
Carib.Order.Widgets.SendWin = function(config) {
  // configurable {
  this.preview_url = '/order/preview/';
  this.send_url = '/order/send/';
  // }
  
  this.last_data = {};
  this.last_url = '';
  
  config = $.extend({
    auto_init: true,
    window_config: {
      autoOpen: false,
      title: 'Отправка заказа',
      modal: true,
      dialogClass: 'order_wizard',
      resizable: true,
      width: 700,
      height: 500,
      closeOnEscape: false
    }
  }, config);
  
  this.addEvents({
    sent: true,
    cancel: true
  });
  
  Carib.Order.Widgets.SendWin.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.SendWin, Carib.Order.Widgets.Window, {
  loadPreview: function(data) {
    this.last_data = data;
    this.last_url = this.preview_url;
    this._load(this.preview_url, data, this.onPreviewLoaded.createDelegate(this));
  },
  
  send: function(data) {
    this.last_data = data;
    this.last_url = this.send_url;
    this._load(this.send_url, data, this.onSent.createDelegate(this));
  },
  
  _load: function(url, data, callback) {
    this.getEl().dialog('option', 'buttons', {});
    this.setLoader();
    this.getEl().load(url, data, callback);
  },
  
  reload: function() {
    this._load(this.last_url, this.last_data, this.onPreviewLoaded.createDelegate(this));
  },
  
  setLoader: function() {
    this.getEl().html('<img src="/i/order-loader.gif" alt="Идет загрузка..." title="Идет загрузка..."/>');
  },
  
  setButtonsToSend: function() {
    this.getEl().dialog('option', 'buttons', {
      'Обновить': this.onReload.createDelegate(this),
      'Отмена': this.onCancel.createDelegate(this),
      'Отправить': this.onSend.createDelegate(this)
    });
  },
  
  setButtonsToExit: function() {
    this.getEl().dialog('option', 'buttons', {
      'OK': this.onOk.createDelegate(this)
    });
  },
  
  onReload: function() {
    this.reload();
  },
  
  onSend: function() {
    this.send(this.last_data);
  },
  
  onCancel: function() {
    this.hide();
    this.fireEvent('cancel', [this]);
  },
  
  onPreviewLoaded: function() {
    this.setButtonsToSend();
  },
  
  onSent: function() {
    this.setButtonsToExit();
  },
  
  onOk: function() {
    this.hide();
  }
});

