jQuery(document).ready(function() {
  
  $('input.#phrase').focus();
  var search_default = $('input.search-box').val();
  var search_action = $('input.search-box').parents('form').attr('action').replace('index', '') + "/suggest";

  $('input.search-box').autoComplete({
    ajax: search_action,
    minChars: 3,
    postVar: 'string',
    preventEnterSubmit: false,
    onFocus: function() {
      if (this.value==search_default) this.value='';
    },
    onBlur: function() {
      if (this.value=='') this.value=search_default;
    }
  });

  $(window).load(function () {
    if ($("#advancedsearch_sort_attr option:selected").val() == "r") {
      $('#advancedsearch_sort_order').attr("disabled","disabled");
    }
  });

  $('.dropdown_export_1').change(function() {
    return dropdown_export_change1();
  });
  $('.dropdown_export_2').click(function() {
    return dropdown_export_change2();
  });

  function dropdown_export_change1() {
    $('.dropdown_export_2').val($('.dropdown_export_1').val())
  }

  function dropdown_export_change2() {
    $('.dropdown_export_1').val($('.dropdown_export_2').val())
  }

  $('#advancedsearch_sort_attr').change(function() {
   
    if ($("#advancedsearch_sort_attr option:selected").val() != "r") {
      $('#advancedsearch_sort_order').removeAttr("disabled");
    } else {
      $('#advancedsearch_sort_order').attr("disabled","disabled");
    }
    if ($("#advancedsearch_sort_attr option:selected").val() == "y") {
      $('#advancedsearch_sort_order').val("d");
    } else {
      $('#advancedsearch_sort_order').val("a");
    }
  });

  $('#advancedsearch_add_condition').click(function() {

    // Clone last condition row
    var last_row = $('.search_condition:last');
    var row = last_row.clone(true);

    // Remove the last add_condition button since it'll be included in the new last row
    last_row.find('#advancedsearch_add_condition').remove();

    // Get the new id
    var condition_count = $('#advancedsearch_condition_count');
    var new_id = eval(condition_count.val()) + 1;

    // Update name and id parameters thoughout row to contain new_id
    var re = /(\w*)\d+(\w*)/;

    // Depending on the structure of row (e.g. a div), it may be necessary to update the row attrs
    if (row.attr('id'))   { row.attr('id', row.attr('id').replace(re, "$1"+ new_id +"$2")); }
    if (row.attr('name')) { row.attr('name', row.attr('name').replace(re, "$1"+ new_id +"$2")); }

    row.find('select,input').map(function () {
      this.id = this.id.replace(re, "$1"+ new_id +"$2");
      this.name = this.name.replace(re, "$1"+ new_id +"$2");
    });

    // Reset the text of the cloned phrase input field
    row.find('.phrase_input').val("");

    // Ensure the rem link is displayed (only applicable if we clone the first row)
    row.find('.remove_condition').show();

    // Hide the remove button/link from last_row
    last_row.find('.remove_condition').hide();

    // Update the new condition count
    condition_count.val(new_id);

    // Add the new row after the last row
    row.insertAfter(last_row);

    return false;
  });

  // Remove the last row
  $('.remove_condition').click(function() {
    var last_row = $(this).parents('.search_condition:last');
    var prev_row = last_row.prev('.search_condition');

    // Show the remove button/link for the second last row (unless it's the 1st row),
    prev_row.not('#1').find('.remove_condition').show();

    // .. and add the add_condition button to the new last row
    var add = last_row.find('#advancedsearch_add_condition');
    prev_row.find('.remove_condition').after(add);

    // ...before removing the last row
    last_row.remove();
    
    // Decrement the condition count
    var condition_count = $('#advancedsearch_condition_count');
    condition_count.val(eval(condition_count.val()) -1);

    return false;
  });

  // Expand the filters form area when it's label is clicked
  $('.filters-title').click(function() {

    var filters_id = $(this).attr('for');
    var filters = $('#' + filters_id);    
    filters.slideToggle();
    
    // Also toggle it's search button
    $('#bottom_button_div').toggle();

    return false;
  });

  // Toggle the advanced search form 
  $('.show-advanced-search').click(function() {
    $('#advanced_search').slideToggle();
    $('.hide-advanced-search').show();
    $(this).hide();
    return false;
  });

  $('.hide-advanced-search').click(function() {
    $('#advanced_search').slideToggle();
    $('.show-advanced-search').show();
    $(this).hide();
    return false;
  });

  // Show / hide preview window
  $('.btn').click(function() {

    var teaser_div = $(this).parents('.result-right').find('.result-teaser');
    var preview_div = $(this).parents('.result-right').find('.result-preview');
    var container_top_div = $(this).parents('.result-data').parents('.result-container').find('.container-top');
    var container_top_preview_div;
    var result_data_div = $(this).parents('.result-data');
    var result_data_preview_div = $(this).parents('.result-data-preview');
    var container_bot_div = $(this).parents('.result-data').parents('.result-container').find('.container-bot');
    var container_bot_preview_div;

    if ($(this).hasClass('expand-btn')) {
      $(this).attr('class', 'btn collapse-btn');
      $(this).find('img').attr('src', '/images/off.png');

      result_data_div.removeClass('result-data');
      result_data_div.addClass('result-data-preview');
      container_top_div.removeClass('container-top');
      container_top_div.addClass('container-top-preview');
      container_bot_div.removeClass('container-bot');
      container_bot_div.addClass('container-bot-preview');
      teaser_div.hide();
      preview_div.show();

      return false;
    }
    else if ($(this).hasClass('collapse-btn')) {
      $(this).attr('class', 'btn expand-btn');
      $(this).find('img').attr('src', '/images/on.png');

      result_data_preview_div.removeClass('result-data-preview');
      result_data_preview_div.addClass('result-data');
      container_bot_preview_div = $(this).parents('.result-data').parents('.result-container').find('.container-bot-preview');
      container_top_preview_div = $(this).parents('.result-data').parents('.result-container').find('.container-top-preview');
      container_top_preview_div.removeClass('container-top-preview');
      container_top_preview_div.addClass('container-top');
      container_bot_preview_div.removeClass('container-bot-preview');
      container_bot_preview_div.addClass('container-bot');
      preview_div.hide();
      teaser_div.show();

      return false;
    }
  });

});


