ns('Carib.Order.Widgets');


Carib.Order.Widgets.FxPanel = function(config) {
  Carib.Order.Widgets.FxPanel.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.FxPanel, Carib.Order.Widgets.FormPanel, {
  show: function() {
    this.getEl().fadeIn('fast');
    this.onShow();
  },
  
  hide: function() {
    this.getEl().fadeOut('fast');
    this.onHide();
  }  
});



/**
 * Residence panel
 * 
 * @version $Id: order_panels.js 553 2009-09-17 15:20:32Z  $
 */
Carib.Order.Widgets.ResidencePanel = function(config) {
  this.first_show = true;
  
  var validator = new Carib.Order.Validation.Validator({
    validators: [
      new Carib.Order.Validation.Rules.Required({
        name: 'nights',
        msg: 'должно быть указано количество ночей' 
      }),
      new Carib.Order.Validation.Rules.Greater({
        name: 'nights',
        msg: 'количество ночей должно быть больше нуля' 
      })
    ]
  });

  config = $.extend({
    title: 'Проживание, экскурсии и трансферы',
    validator: validator,
    fields: [
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=country_id]',
        name: 'country_id',
        recordset: Carib.Order.App.getCountries()
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=city_id]',
        name: 'city_id',
        empty_text: 'города отсутвуют',
        loader: new Carib.Order.Data.Loader({
          url: '/module/country_lists/cities/',
          reader: new Carib.Order.Data.XmlReader({
            record_class: Carib.Order.Data.City,
            id_field: 'id'
          })
        }),
        recordset: new Carib.Order.Data.Recordset()
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=tour_id]',
        name: 'tour_id',
        empty_text: 'туры отсутвуют',
        loader: new Carib.Order.Data.Loader({
          url: '/module/country_lists/tours/',
          reader: new Carib.Order.Data.XmlReader({
            record_class: Carib.Order.Data.Tour,
            id_field: 'id'
          })
        }),
        recordset: new Carib.Order.Data.Recordset()
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=hotel_id]',
        name: 'hotel_id',
        empty_text: 'отели отсутствуют',
        loader: new Carib.Order.Data.Loader({
          url: '/module/country_lists/hotels/',
          reader: new Carib.Order.Data.XmlReader({
            record_class: Carib.Order.Data.Hotel,
            id_field: 'id'
          })
        }),
        recordset: new Carib.Order.Data.Recordset()
      }),
      new Carib.Order.Widgets.NightsField({ 
        selector: 'input[name=nights],select[name=nights]',
        name: 'nights'
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=placing_category_id]',
        name: 'placing_category_id',
        recordset: Carib.Order.App.getPlacingCategories()
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=number_category_id]',
        name: 'number_category_id',
        recordset: Carib.Order.App.getNumberCategories()
      }),
      new Carib.Order.Widgets.Select({ 
        selector: 'select[name=food_type_id]',
        name: 'food_type_id',
        recordset: Carib.Order.App.getFoodTypes()
      }),
      new Carib.Order.Widgets.ExcursionsSelect({
        selector: '.excursions_list',
        name: 'excursions',
        select: new Carib.Order.Widgets.Select({
          selector: 'select[name=excursion_id]',
          empty_text: 'экскурсии отсутствуют',
          loader: new Carib.Order.Data.Loader({
            url: '/module/country_lists/excursions/',
            reader: new Carib.Order.Data.XmlReader({
              record_class: Carib.Order.Data.Excursion,
              id_field: 'id'
            })
          }),
          recordset: new Carib.Order.Data.Recordset()
        })
      }),
      new Carib.Order.Widgets.SimpleField({ 
        selector: 'select[name=transfer]',
        name: 'transfer'
      })
    ]
  }, config);
  
  Carib.Order.Widgets.ResidencePanel.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.ResidencePanel, Carib.Order.Widgets.FxPanel, {
  init: function() {
    Carib.Order.Widgets.ResidencePanel.superclass.init.call(this);
    
    this.getInfoTourButton().click(this.onClickInfoTour.createDelegate(this));
  },
  
  _initFields: function() {
    Carib.Order.Widgets.ResidencePanel.superclass._initFields.call(this);
    
    this.getCountryField().on('change', this.onChangeCountry, this);
    this.getCityField().on('change', this.onChangeCity, this);
    this.getTourField().on('change', this.onChangeTour, this);
    this.getTourField().on('load', this.onLoadTours, this);
    
    //this._loadLists();
  },
  
  _loadLists: function() {
    this.loadCities(true);
    this.loadHotels(true);
    this.loadExcursions(true);
    this.loadTours(true);
  },
  
  loadCities: function(no_reset) {
    this.getCityField().load({
      params: { country_id: this.getCountryField().getValue() }
    }, no_reset && this.getCityField().getValue());
  },
  
  loadHotels: function(no_reset) {
    if(this.getCityField().getValue()) {
      this.getHotelField().load({
        params: {
          city_id: this.getCityField().getValue()
        }
      }, no_reset);
    }
  },
  
  loadExcursions: function(no_reset) {
    if(!no_reset)
      this.getExcursionsField().getRecordset().removeAll();
      
    if(this.getCityField().getValue()) {
      this.getExcursionsField().getSelect().load({
        params: { city_id: this.getCityField().getValue() }
      });
    }
  },
  
  loadTours: function(no_reset) {
    if (this.getCityField().getValue()) {
      this.getTourField().load({
        params: {
          city_id: this.getCityField().getValue()
        }
      }, no_reset);
    }
  },
  
  _updateFields: function() {
    Carib.Order.Widgets.ResidencePanel.superclass._updateFields.call(this);
    
    this._loadLists();        
  },
  
  updateRecord: function() {
    if(!this.record)
      return;
    Carib.Order.Widgets.ResidencePanel.superclass.updateRecord.call(this);
      
    this._syncSelectedName('country_name', this.getCountryField());
    this._syncSelectedName('city_name', this.getCityField());
    this._syncSelectedName('tour_name', this.getTourField());
    this._syncSelectedName('hotel_name', this.getHotelField());
    this._syncSelectedName('placing_category_name', this.getPlacingCategoryField());
    this._syncSelectedName('number_category_name', this.getNumberCategoryField());
    this._syncSelectedName('food_type_name', this.getFoodTypeField());
  },
  
  _syncSelectedName: function(record_field, field) {
    var selected = field.getSelected();
    if(selected) {
      this.record.set(record_field, selected.get(field.display_value));
    } else
      this.record.set(record_field, '');
  },
  
  getExcursionsField: function() {
    return this.getField('excursions');
  },
  
  getTourField: function() {
    return this.getField('tour_id');
  },
  
  getCountryField: function() {
    return this.getField('country_id');
  },
  
  getCityField: function() {
    return this.getField('city_id');
  },
  
  getHotelField: function() {
    return this.getField('hotel_id');
  },
  
  getNumberCategoryField: function() {
    return this.getField('number_category_id');
  },
  
  getPlacingCategoryField: function() {
    return this.getField('placing_category_id');
  },
  
  getFoodTypeField: function() {
    return this.getField('food_type_id');
  },
  
  getNightsField: function() {
    return this.getField('nights');
  },
  
  syncFieldsWithTour: function() {
    if(this.getTourField().getValue() != 0) {
      $('tr.hide_for_tour', this.getEl()).hide();
    } else {
      $('tr.hide_for_tour', this.getEl()).show();
    }
    this.syncTourDuration();
    this.syncTourFile();
  },
  
  syncTourDuration: function() {
    if(this.getTourField().getValue() != 0) {
      var selected = this.getTourField().getSelected();
      if(selected) {
        var duration = this.getTourField().getSelected().get('duration');
        this.getNightsField().transformToSelect(duration);
        if(!this.getNightsField().hasOption(this.getNightsField().getValue()))
          this.getNightsField().setFirstValue();
      }
    } else {
      this.getNightsField().transformToInput();
    }
  },
  
  getInfoTourButton: function() {
    return $('.tour_info_button', this.getEl());
  },
  
  activeTourDescription: function(file) {
    this.getInfoTourButton()
      .css('cursor', 'pointer')
      .attr('href', file)
      //.attr('target', '_blank')
      .addClass('ui-state-hover');
  },
  
  unactiveTourDescription: function() {
    this.getInfoTourButton()
      .css('cursor', 'default')
      .attr('href', 'javascript:void(0)')
      //.removeAttr('target')
      .removeClass('ui-state-hover');
  },
  
  syncTourFile: function() {
    if(this.getTourField().getValue() != 0) {
      var selected = this.getTourField().getSelected();
      if(selected && selected.data.description_file) {
        this.activeTourDescription(selected.data.description_file);
        return;
      }
    }
    this.unactiveTourDescription();
  },
  
  /*onShow: function() {
    Carib.Order.Widgets.ResidencePanel.superclass.onShow.call(this);
    
    if(this.first_show) {
      this.first_show = false;
      this._firstLoad();
    }
  },*/
  
  onChangeCountry: function() {
    if(this.updating)
      return;
    
    this.getHotelField().getRecordset().removeAll();
    this.loadCities();
  },
  
  onChangeCity: function() {
    if(this.updating)
      return;
    
    this.loadHotels();
    this.loadExcursions();
    this.loadTours();
  },
  
  onChangeTour: function() {
    this.syncFieldsWithTour();
  },
  
  onLoadTours: function() {
    this.syncTourDuration();
    this.syncTourFile();
  },
  
  onClickInfoTour: function(e) {
    if(!this.getInfoTourButton().hasClass('ui-state-hover'))
      e.preventDefault();
  }
});


/**
 * Aero panel
 * 
 * @version $Id: order_panels.js 553 2009-09-17 15:20:32Z  $
 */
Carib.Order.Widgets.AeroPanel = function(config) {
  config = $.extend({
    title: 'Авиаперелет, гид/переводчик',
    fields: [
      new Carib.Order.Widgets.SimpleField({ 
        selector: 'select[name=airfare]',
        name: 'airfare'
      }),
      new Carib.Order.Widgets.SimpleField({ 
        selector: 'select[name=guide]',
        name: 'guide'
      }),
      new Carib.Order.Widgets.SimpleField({ 
        selector: 'select[name=language]',
        name: 'language'
      }),
      new Carib.Order.Widgets.SimpleField({ 
        selector: 'textarea[name=comment]',
        name: 'comment'
      })
    ]
  }, config);
  
  Carib.Order.Widgets.AeroPanel.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.AeroPanel, Carib.Order.Widgets.FxPanel, {
  init: function() {
    Carib.Order.Widgets.AeroPanel.superclass.init.call(this);
    
    this.syncLanguage();
    this.getGuideField().on('change', this.onChangeGuide, this);
  },
  
  syncLanguage: function() {
    if(this.getGuideField().getValue() != '-')
      this.getLanguageField().show();
    else
      this.getLanguageField().hide();
  },
  
  getGuideField: function() {
    return this.getField('guide');
  },
  
  getLanguageField: function() {
    return this.getField('language');
  },
  
  onChangeGuide: function() {
    this.syncLanguage();
  }
});
