ns('Carib.Order.Widgets');

/**
 * Order wizard panel
 * 
 * @version $Id: order_wizard.js 546 2009-09-11 15:35:29Z  $
 */
Carib.Order.Widgets.OrderWizard = function(config) {
  this.order = null;
  
  config = $.extend({
    only_button_events: true
  }, config);
  
  Carib.Order.Widgets.OrderWizard.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.OrderWizard, Carib.Order.Widgets.Wizard, {
  setOrder: function(order) {
    this.order = order;
    this.find('residence_panel').setRecord(order.get('residence'));
    this.find('aero_panel').setRecord(order);
  },
  
  getOrder: function() {
    return this.order;
  }
});


/**
 * Order wizard window
 * 
 * @version $Id: order_wizard.js 546 2009-09-11 15:35:29Z  $
 */
Carib.Order.Widgets.OrderWizardWin = function(config) {
  config = $.extend({
    auto_init: false,
    window_config: {
      autoOpen: false,
      title: 'Онлайн-заявка',
      modal: true,
      dialogClass: 'order_wizard',
      resizable: false,
      closeOnEscape: false,
      position: ['right', 'top'],
      buttons: {
        'Отмена': this.onCancel.createDelegate(this),
        'OK': this.onOk.createDelegate(this)
      }
    },
    wizard: null // required      
  }, config);
  
  var wizard_size = config.wizard.getSize();
  config.window_config.width = wizard_size.width + 30;
  config.window_config.height = wizard_size.height + 130;
  
  this.button_processing = false;
  
  this.addEvents({
    ok: true,
    cancel: true
  });
  
  Carib.Order.Widgets.OrderWizardWin.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.OrderWizardWin, Carib.Order.Widgets.Window, {
  init: function() {
    Carib.Order.Widgets.OrderWizardWin.superclass.init.call(this);
    
    this.wizard.on('resize', this.onWizardResize, this);
    this.wizard.on('showbutton', this.onShowWizardButton, this);
    this.wizard.on('hidebutton', this.onHideWizardButton, this);
  },
  
  getWizard: function() {
    return this.wizard;
  },
  
  updatePanelsFields: function() {
    this.wizard.each(function(num, panel) {
      panel.updateFields();
    });
  },
  
  updatePanelsRecords: function() {
    this.wizard.each(function(num, panel) {
      panel.updateRecord();
    });
  },
  
  syncTitle: function() {
    this.setTitle('Проживание, экскурсии, трансферы');
  },
  
  _updateButtons: function() {
    this.getEl().dialog('option', 'buttons', this.window_config.buttons);
  },
  
  showNextButton: function() {
    this.window_config.buttons['Далее'] = this.onNext.createDelegate(this);
    this._updateButtons();
  },
  
  showPrevButton: function() {
    this.window_config.buttons['Назад'] = this.onPrev.createDelegate(this);
    this._updateButtons();
  },
  
  hideNextButton: function() {
    if(this.window_config.buttons['Далее'])
      delete this.window_config.buttons['Далее'];
      
    this._updateButtons();
  },
  
  hidePrevButton: function() {
    if(this.window_config.buttons['Назад'])
      delete this.window_config.buttons['Назад'];
      
    this._updateButtons();
  },
  
  onShowWizardButton: function(wizard, button, num) {
    if(num == 0)
      this.showPrevButton();
    else
      this.showNextButton();
  },
  
  onHideWizardButton: function(wizard, button, num) {
    if(num == 0)
      this.hidePrevButton();
    else
      this.hideNextButton();
  },
  
  onNext: function() {
    this.wizard.goNext();
  },
  
  onPrev: function() {
    this.wizard.goPrev();
  },
  
  onWizardResize: function() {
    this.setSize(this.wizard.getSize());
  },
  
  onShow: function() {
    Carib.Order.Widgets.OrderWizardWin.superclass.onShow.call(this);
    
    this.updatePanelsFields();
    this.wizard.activate(this.wizard.getAt(0).getId());
    this.syncTitle();
  },
  
  onHide: function() {
    Carib.Order.Widgets.OrderWizardWin.superclass.onHide.call(this);
    
    if(!this.button_processing)
      this.fireEvent('cancel', [this]);
  },
  
  onOk: function() {
    if(
      !this.wizard.find('residence_panel').validate()
      || !this.wizard.find('aero_panel').validate()
    )
      return;

    this.button_processing = true;
    this.hide();
    this.updatePanelsRecords();
    this.fireEvent('ok', [this]);
    this.button_processing = false;
  },
  
  onCancel: function() {
    this.button_processing = true;
    this.hide();
    this.fireEvent('cancel', [this]);
    this.button_processing = false;    
  }
});

