var trip_finder_values = new Array();

function tripMatch(value_set) {
  trip_type_id = $('search_type_id').getValue();
  return (trip_type_id == '' || value_set[0] == trip_type_id);
}

function originMatch(value_set) {
  origin_id = $('search_origin_id').getValue();
  return (origin_id == '' || value_set[2] == origin_id);
}

function destMatch(value_set) {
  dest_id = $('search_destination_id').getValue();
  return (dest_id == '' || value_set[4] == dest_id);
}

function add_origin_option(options, value_set) {
  add_option(options, value_set[3], value_set[2]);
}

function add_dest_option(options, value_set) {
  add_option(options, value_set[5], value_set[4]);
}

function add_date_option(options, value_set) {
  add_option(options, value_set[7], value_set[6]);
}

function add_option(options, text, value) {
  if (text == '') {
    return;
  }

  for (var i = 0; i < options.length; i++) {
    if (options[i][1] == value) {
      return;
    }
  }

  options[options.length] = [text, value];
}

function set_options(element, new_options, value_sort) {
  options = element.options;
  options.length = 1;
  if (value_sort == true) {
    new_options.sort(function(a,b) {
      if (a[1] > b[1]) {
        return 1;
      }
      else if (a[1] < b[1]) {
        return -1;
      }
      else {
        return 0;
      }
    });
  }
  else {
    new_options.sort();
  }
  for (var i = 0; i < new_options.length; i++) {
    options[options.length] = new Option(new_options[i][0], new_options[i][1], false, false);
  }
}

function tripTypeSelected() {
  origin_options = [];
  dest_options = [];
  date_options = [];

  trip_finder_values.each(function(value_set) {
    if (tripMatch(value_set)) {
      add_origin_option(origin_options, value_set);
      add_dest_option(dest_options, value_set);
      add_date_option(date_options, value_set);
    }
  });
  set_options($('search_origin_id'), origin_options);
  set_options($('search_destination_id'), dest_options);
  set_options($('search_date'), date_options, true);
}

function originSelected() {
  dest_options = [];
  date_options = [];

  trip_finder_values.each(function(value_set) {
    if (tripMatch(value_set) && originMatch(value_set)) {
      add_dest_option(dest_options, value_set);
      add_date_option(date_options, value_set);
    }
  });
  set_options($('search_destination_id'), dest_options);
  set_options($('search_date'), date_options, true);
}

function destinationSelected() {
  date_options = [];

  trip_finder_values.each(function(value_set) {
    if (tripMatch(value_set) && originMatch(value_set) && destMatch(value_set)) {
      add_date_option(date_options, value_set);
    }
  });
  set_options($('search_date'), date_options, true);
}

function selectTripType(value) {
  $$('select#search_type_id option').each(function(o){
    if (o.value == value) { o.selected = true; }
  });
  tripTypeSelected();
}

function selectOrigin(value) {
  $$('select#search_origin_id option').each(function(o){
    if (o.value == value) { o.selected = true; }
  });
  originSelected();
}

function selectDestination(value) {
  $$('select#search_destination_id option').each(function(o){
    if (o.value == value) { o.selected = true; }
  });
  destinationSelected();
}

function selectDepartureDate(value) {
  $$('select#search_date option').each(function(o){
    if (o.value == value) { o.selected = true; }
  });
}

document.observe('dom:loaded', function() {
  tripTypeSelected();
  $('search_type_id').observe('change', tripTypeSelected);
  $('search_origin_id').observe('change', originSelected);
  $('search_destination_id').observe('change', destinationSelected);
});

