ns('Carib.Order');

/**
 * Order controller
 * 
 * @version $Id: order_controller.js 865 2010-03-19 17:01:03Z  $
 */
Carib.Order.OrderController = function(config) {
  // configurable {
  this.default_order_record = null; // required
  // }
  this.preview_table = null;
  this.wizard = null; 
  this.wizard_win = null;
  this.order = new Carib.Order.Data.Recordset();
  this.add_mode = false;
  
  $.extend(this, config);
}

Carib.Order.OrderController.prototype = {
  doStart: function() {
    this.preloadOrderImage();
    this.getPreviewTable().init();
    this.getWizardWin().init();
    //this.order.add(this.createOrderRecord());
    this.getPreviewTable().select(0);
    this.getAddOrderButton().click(this.onAddOrder.createDelegate(this));
    //this.doAddOrder();
  },
  
  doShowWizard: function() {
    var selected = this.getPreviewTable().getSelected();
    if(!selected)
      return;
      
    this.getWizard().setOrder(selected);
    this.getWizardWin().show();
  },
  
  doEdit: function(record) {
    var num = this.order.findRecord(record.getId())
    this.getPreviewTable().select(num);
    
    this.doShowWizard();
  },
  
  doAddOrder: function() {
    this.add_mode = true;
    this.order.add(this.createOrderRecord());
    this.getPreviewTable().select(this.order.getCount() - 1);
    
    this.doShowWizard()
  },
  
  preloadOrderImage: function() {
    var img = new Image();
    img.src = '/i/order-loader.gif';
  },
  
  createOrderRecord: function() {
    var record = new Carib.Order.Data.Order();
    record.importValues(this.default_order_record.data);
    if(this.order.getCount() == 0) {
      var residence = record.data.residence;
      var tour_data = Carib.Order.App.getTourData();
      if (tour_data.id) {
        residence.set('country_id', tour_data.country_id);
        residence.set('country_name', tour_data.country_name);
        residence.set('city_id', tour_data.city_id);
        residence.set('city_name', tour_data.city_name);
        residence.set('tour_id', tour_data.id);
        residence.set('tour_name', tour_data.name);
        residence.set('nights', tour_data.duration);
      }
    }
      
    return record;
  },
  
  getPreviewTable: function() {
    if(!this.preview_table) {
      this.preview_table = new Carib.Order.Widgets.PreviewTable({
        selector: '#preview_table',
        recordset: this.order,
        listeners: {
          edit: this.onEditOrder,
          scope: this
        }
      });
    }
    return this.preview_table;
  },
  
  getWizard: function() {
    if(!this.wizard) {
      this.wizard = new Carib.Order.Widgets.OrderWizard({
        panels: [
          this._createResidencePanel(),
          this._createAeroPanel()
        ],
        active: 'residence_panel',
        selector: '#order_wizard',
        auto_init: true
      });
    }
    return this.wizard;
  },
  
  getWizardWin: function() {
    if(!this.wizard_win) {
      this.wizard_win = new Carib.Order.Widgets.OrderWizardWin({
        selector: '#order_wizard_win',
        wizard: this.getWizard(),
        listeners: {
          ok: this.onOk,
          cancel: this.onCancel,
          scope: this
        }
      });
    }
    return this.wizard_win;
  },
  
  getOrder: function() {
    return this.order;
  },
  
  _createCommonInfoPanel: function() {
    
    return new Carib.Order.Widgets.CommonInfoPanel({
      selector: '#common_info_panel',
      auto_init: true
    });
  },
  
  _createResidencePanel: function() {
    return new Carib.Order.Widgets.ResidencePanel({
      selector: '#residence_panel',
      auto_init: true
    });
  },
  
  _createAeroPanel: function() {
    return new Carib.Order.Widgets.AeroPanel({
      selector: '#aero_panel',
      auto_init: true
    });
  },
  
  getAddOrderButton: function() {
    return $('#add_order_button');
  },
  
  onOk: function() {
    var order = this.getPreviewTable().getSelected();
    if(order)
      order.commit();
      
    this.add_mode = false;
  },
  
  onCancel: function() {
    if(this.add_mode) 
      this.order.remove(this.getPreviewTable().getSelected().getId());
      
    this.add_mode = false;
  },
  
  onEditOrder: function(table, record) {
    this.doEdit(record);
  },
  
  onAddOrder: function() {
    this.doAddOrder();
  }
}

