var SearchCompetitions = new Class({

  Implements: Options,

  initialize: function(el, options) {
    this.el = $(el);
    this.setOptions(options);
    this.init();
  },

  init: function() {
    this.form = this.el.getElement('form');
    this.chooseYear = this.form.getElement('select[name=chooseYear]');
    this.chooseCompetition = this.form.getElement('select[name=chooseCompetition]');
    this.button = this.form.getElement('a.button');
    this.chooseYear.addEvent('change', this.handleYearChange.bindWithEvent(this));
    this.chooseCompetition.addEvent('change', this.handleCompetitionChange.bindWithEvent(this));
    this.button.addEvent('click', this.handleButtonClick.bindWithEvent(this));
  },

  uninit: function() {
    this.chooseYear.removeEvent('change');
    this.chooseCompetition.removeEvent('change');
    this.button.removeEvent('click');
    // TODO
    this.el.setStyle('height', '');
  },

  handleYearChange: function(e) {
    e.stop();
    this.updateParagraph(false);
  },

  handleCompetitionChange: function(e) {
    e.stop();
    this.updateParagraph(false);
  },

  handleButtonClick: function(e) {
    e.stop();
    this.updateParagraph(true);
  },

  updateParagraph: function(search) {
    this.uninit();
    new Request.HTML( {
      url: CONTEXT_PATH + this.options.url,
      data: Hash.toQueryString({
        'async': true,
        'uuid': this.options.uuid,
        'search': search }) + '&' + this.form.toQueryString(),
      evalScripts: false,
      onComplete: function() {
        this.init();
      }.bind(this),
      onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
        this.el.set('html', responseHTML);
        this.init();
      }.bind(this)
    }).get();
  }

});

