ns('Carib.Order');

/**
 * Tourists controller
 * 
 * @version $Id: tourists_controller.js 418 2009-07-10 13:08:59Z  $
 */
Carib.Order.TouristsController = function(config) {
  this.tourists_table = null;
  this.tourist_form = null; 
  this.tourist_win = null;
  this.tourists = new Carib.Order.Data.Recordset();
  this.add_mode = false;
  
  $.extend(this, config);
}

Carib.Order.TouristsController.prototype = {
  doStart: function() {
    this.getTouristsTable().init();
    this.getTouristWin().init();
    this.getAddTouristButton().click(this.onAddTourist.createDelegate(this));
    
    /*this.tourists.add(new Carib.Order.Data.Tourist());
    this.getTouristsTable().select(0);*/
    //this.getAddOrderButton().click(this.onAddOrder.createDelegate(this));
  },
  
  doAddTourist: function() {
    this.add_mode = true;
    this.tourists.add(new Carib.Order.Data.Tourist());
    this.getTouristsTable().select(this.tourists.getCount() - 1);
    
    this.doShowTouristWindow()
  },
  
  doEdit: function(record) {
    var num = this.tourists.findRecord(record.getId())
    this.getTouristsTable().select(num);
    
    this.doShowTouristWindow();
  },
  
  doShowTouristWindow: function() {
    var selected = this.getTouristsTable().getSelected();
    if(!selected)
      return;
      
    this.getTouristForm().setRecord(selected);
    this.getTouristWin().show();
  },
  
  getTouristsTable: function() {
    if(!this.tourists_table) {
      this.tourists_table = new Carib.Order.Widgets.TouristsTable({
        selector: '#tourists_table',
        recordset: this.tourists,
        listeners: {
          edit: this.onEditTourist,
          scope: this
        }
      });
    }
    return this.tourists_table;
  },
  
  getTouristForm: function() {
    if(!this.tourist_form) {
      this.tourist_form = new Carib.Order.Widgets.TouristPanel({
        selector: '#tourist_panel',
        auto_init: true
      });
    }
    return this.tourist_form;
  },
  
  getTouristWin: function() {
    if(!this.tourist_win) {
      this.tourist_win = new Carib.Order.Widgets.TouristWin({
        auto_init: false,
        selector: '#tourist_win',
        form: this.getTouristForm(),
        listeners: {
          ok: this.onOk,
          cancel: this.onCancel,
          scope: this
        }
      });
    }
    return this.tourist_win;
  },
  
  getTourists: function() {
    return this.tourists;
  },
  
  getAddTouristButton: function() {
    return $('#add_tourist_button');
  },
  
  onOk: function() {
    var order = this.getTouristsTable().getSelected();
    if(order)
      order.commit();
      
    this.add_mode = false;
  },
  
  onCancel: function() {
    if(this.add_mode)
      this.tourists.remove(this.getTouristsTable().getSelected().getId());
    
    this.add_mode = false;
  },
  
  onAddTourist: function() {
    this.doAddTourist();
  },
  
  onEditTourist: function(table, record) {
    this.doEdit(record);
  }
}

