ns('Carib.Order');

/**
 * Common info controller
 * 
 * @version $Id: common_info_controller.js 865 2010-03-19 17:01:03Z  $
 */
Carib.Order.CommonInfoController = function() {
  this.table = null;
  this.panel = null;
  this.win = null;
  this.additional_info_win = null;
  this.additional_info_panel = null;
  this.record = new Carib.Order.Data.CommonInfo();
}

Carib.Order.CommonInfoController.prototype = {
  doStart: function() {
    this.getTable().init();
    this.getWin().init();
    this.getAdditionalInfoWin().init();
    this.getTable().getRecordset().add(this.record);
    this.getPanel().setRecord(this.record);
    this.getAdditionalInfoButton().click(this.onEditAdditionalInfo.createDelegate(this));
  },
  
  doEditCommonInfo: function(record) {
    this.doShowCommonInfoWin();
  },
  
  doShowCommonInfoWin: function() {
    this.getWin().show();
  },
  
  doShowAdditionalInfoWin: function() {
    this.getAdditionalInfoWin().show();
  },
  
  setAdditionalInfo: function(content) {
    this.record.set('additional_info', content);
    this.getAdditionalInfoPreview().text(content);
  },
  
  getRecord: function() {
    return this.record;
  },
  
  getTable: function() {
    if(!this.table) {
      this.table = new Carib.Order.Widgets.CommonInfoTable({
        selector: '#common_info_table',
        recordset: new Carib.Order.Data.Recordset(),
        listeners: {
          edit: this.onEditCommonInfo,
          scope: this
        }
      });
    }
    return this.table;
  },
  
  getPanel: function() {
    if(!this.panel) {
      this.panel = new Carib.Order.Widgets.CommonInfoPanel({
        selector: '#common_info_panel',
        auto_init: true
      });
    }
    return this.panel;
  },
  
  getWin: function() {
    if(!this.win) {
      this.win = new Carib.Order.Widgets.CommonInfoWin({
        auto_init: false,
        selector: '#common_info_win',
        form: this.getPanel(),
        listeners: {
          ok: this.onOk,
          scope: this
        }
      });
    }
    return this.win;
  },
  
  getAdditionalInfoPanel: function() {
    if(!this.additional_info_panel) {
      this.additional_info_panel = new Carib.Order.Widgets.AdditionalInfoPanel({
        selector: '#additional_info_panel',
        record: this.record,
        auto_init: true
      });
    }
    return this.additional_info_panel;
  },
  
  getAdditionalInfoWin: function() {
    if(!this.additional_info_win) {
      this.additional_info_win = new Carib.Order.Widgets.CommonInfoWin({
        auto_init: false,
        selector: '#additional_info_win',
        form: this.getAdditionalInfoPanel(),
        listeners: {
          ok: this.onAdditionalInfoOk,
          scope: this
        }
      });
    }
    return this.additional_info_win;
  },
  
  validate: function(errors) {
    return this.getPanel().getErrors(errors);
  },
  
  getAdditionalInfoPreview: function() {
    return $('#additional_info_preview');
  },
  
  getAdditionalInfoButton: function() {
    return $('#additional_info_button');
  },
  
  onEditCommonInfo: function(table, record) {
    this.doEditCommonInfo(record);
  },
  
  onOk: function() {
    this.record.commit();
  },
  
  onEditAdditionalInfo: function() {
    this.getAdditionalInfoWin().show();
  },
  
  onAdditionalInfoOk: function() {
    this.setAdditionalInfo(this.record.get('additional_info'));
  }
}

