var QuestionsAnswers = {
  display_answer_form: function(){
    jQuery('#answer').addClass('active');
    jQuery('#answer_form').show();
    jQuery("#answer_body").focus();
  },
  toggle_selection: function(recommendation, newState){
    OTHER = {after: 'before', before: 'after'};
    // TODO crap out unless recommendation is in (merchant, rate)
    jQuery('#recommend .temp_' + recommendation).
      find('.' + newState + '_selection').
        show().
      end().find('.' + OTHER[newState] + '_selection').
        hide();
  },
  recommend_rate: function (rate_id, title){
    jQuery('#answer_recommended_rate_id').val(rate_id);
    jQuery('#recommended_rate').text(title);
    QuestionsAnswers.toggle_selection('rate', 'after');
    QuestionsAnswers.RatesSearchForm.dialog('close');
    return false;
  },
  unselect_radio_buttons: function () {
    jQuery("#recommendation_type_none").click();
  },
  hide_all_recommendations: function () {
    jQuery('#recommend li').removeClass('active');
  },

  activate_answer_this_question: function() {
    jQuery("#answer a.open").click(function () {
      QuestionsAnswers.display_answer_form();
      return false;
    });

    jQuery("#answer a.close").click(function () {
      jQuery('#answer').removeClass('active');
      jQuery('#answer_form').hide();
      return false;
    });
  },

  activate_focus_answer_textarea: function() {
    if (jQuery("#answer").hasClass("active")) {
      jQuery("#answer_body").focus();
    }
  },

  activate_answer_form_radio_buttons: function() {
    QuestionsAnswers.hide_all_recommendations();
    jQuery("#recommend input[type=radio]").click(function(){
      QuestionsAnswers.hide_all_recommendations();
      jQuery(this).parents('li').addClass('active');
    });
  },

  activate_erase_recommended_merchant: function(){
    jQuery("#recommend .temp_merchant .erase").click(function(){
      QuestionsAnswers.unselect_radio_buttons();
      QuestionsAnswers.hide_all_recommendations();
      jQuery("#institution").val('');
      return false;
    });
  },

  activate_erase_recommended_rate: function(){
    // FIXME remove this temp_* crap. Don't forget the hidden one in toggle_selection()
    jQuery("#recommend .temp_rate .erase").click(function(){
      QuestionsAnswers.unselect_radio_buttons();
      QuestionsAnswers.hide_all_recommendations();
      jQuery('#answer_recommended_rate_id, #recommended_rate').val('');
      QuestionsAnswers.toggle_selection('rate', 'before');
      return false;
    });
  },
  activate_configure_search_form: function(){
    //FIXME Hack to make sure we mess with the dialog only when in q/show
    if (jQuery('.conversation').length > 0) {
      QuestionsAnswers.RatesSearchForm = 
        jQuery("#comparerates").dialog({
          autoOpen:false,
          modal:true, 
          resizable:false, 
          title: t('searches._widget.title'),
          width: 500
        });

      jQuery("#compare_rates_overlay_results_container").hide();
    }
  },
  activate_display_search_form: function(){
    jQuery("#display_search_form").click(function () {
      QuestionsAnswers.RatesSearchForm.dialog('open');
      return false;
    });
  },

  activate_choose_best_answer: function(){
    jQuery("a.best-answer-button").click(function () {
      jQuery(this).siblings('div.confirmation').show();
      return false;
    });
    jQuery('.confirmation form a').click(function(){
      jQuery(this).parents('.confirmation').hide();
      return false;
    });
  },

  // Rounded Corners
  activate_rounded_corners: function() {
    var cornerSettings = {
        tl: { radius: 4 },
        tr: { radius: 4 },
        bl: { radius: 4 },
        br: { radius: 4 },
        antiAlias: false,
        autoPad: true,
        validTags: ["div"] };

    jQuery(".focus_question .details").corner();
    jQuery(".other-questions-ask").corner(cornerSettings);
  },

  searchStarted: function(formData, jqForm, options) {
    jQuery('#compare_rates_overlay_form .spinner').css('visibility', 'visible');
    jQuery('#compare_rates_overlay_results_container').hide();
  },

  searchSuccessful: function(formData, jqForm, options) {
    jQuery('#compare_rates_overlay_form .spinner').css('visibility', 'hidden');
    jQuery('#compare_rates_overlay_results_container').show();
  },

  activate_compare_rates_form: function() {
    var options = { 
      target: '#compare_rates_overlay_results',
      beforeSubmit: QuestionsAnswers.searchStarted,
      success: QuestionsAnswers.searchSuccessful
    };
    jQuery('#compare_rates_overlay_form').ajaxForm(options);

    jQuery(".recommend_rate_link").click(function() {
      jQuery('#answer_recommended_rate_id').val(this.name);
      jQuery('#recommended_rate').text(
        jQuery(this).parents('tr.recommend_rate_row').
                     children('.answer_form_display').text()
      );
      QuestionsAnswers.toggle_selection('rate', 'after');
      QuestionsAnswers.RatesSearchForm.dialog('close');
      return false;
    });
  }
}


activator_class(QuestionsAnswers);
