$(document).ready(function() {
  function resort_deals() {
  	params = '?' + jQuery.param({ sort: $('#sort_select').val() }) + "&range=" + $.url.param('range');
  	
    // If we aren't on the first page, then do a regular non-AJAX request
  	var matches = document.location.href.match(/(.+)\/p\d+$/);
  	if (matches) {
  		document.location.href = matches[1] + params;
  	} else {
  		$("#filtering_status").click();
  	  $('#deal_cards').load(document.location.pathname + params, function() { tb_remove(); });
  	}
  }

  $('#sort_select').livequery('change', resort_deals);
  
  // prevent enter key from submitting form
  $('input#new_location_name').keypress(function(e) {
    if (e.which == 13) {
      return false;
    }
  });
  
  // Disable submit button once pressed, so we don't get double submits
  $('form').submit(function() {
    $('input[type=submit]', this).attr('disabled', 'disabled');
  });
  
  $('input#new_location_name').example('start typing a destination name');
  
	$('input#new_location_name').autocomplete("/locations/auto_complete_for_location_search", 
																					{ matchContains:1, 
																						delay:400,
																						minChars:3, 
																						matchSubset:false, 
																						cacheLength:1, 
																						selectFirst:true,
																						max:10 });

	$('input#new_location_name').result(function(event, data, formatted) {
		if (data[1] !== '0') {
      var pure_location_name = data[0].replace(/ \(.*\)$/, '');
      checkbox_id = "alert_locations_' + data[1] + '_id_' + data[1] + '";
      $('<div class="location"><label for="' + checkbox_id + '"><input id="' + checkbox_id + '" name="alert[locations][' + data[1] + '][id]" type="checkbox" checked />' + pure_location_name + '</div>').insertBefore($(this).parent('p'));
		}
		$(this).val('');
	});
	
	var price_range_min = 10;
	var price_step = 10;
	var price_range_max = 500 + price_step;
			
  function update_price_slider_handle(e, ui) {
    // Need to use ui.value here if we have it, because otherwise asking the handle
    // for its value returns its previous value.  But, if no ui (manually called method)
    // then fetch from the slider
    var value;
    if (typeof(ui) == 'undefined') {
      value = $('#price_range_slider').slider('value');
	  } else {
		  value = ui.value;
		} 
	 	if (value == price_range_max) {
  		value = '$' + (price_range_max - price_step) + '+/night';
		} else {
  	  value = '$' + value + '/night';
		}
	  $('span#price_range_label_end').html(value);
	}
	
	function set_price_slider_value(e, ui) {
	  $('input#alert_max_price').val(ui.value);
	}
	
	if ($('#price_range_slider').get(0)) {
  	$('#price_range_slider').slider({
    	step: price_step,
    	min: price_range_min, 
    	max: price_range_max,
    	slide: update_price_slider_handle,
    	change: set_price_slider_value,
    	value: $('input#alert_max_price').val() == 0 ? price_range_max : $('input#alert_max_price').val()
    });
    
    update_price_slider_handle();
  }
    
  function set_hotel_ratings() {
    if (hotel_rating_filter_in_use()) {
			var hotel_ratings = "";
			$("input[name=filter_rating]:checked").each(function() {
				hotel_ratings += $(this).val() + ",";
			});
  	  $('input#alert_hotel_ratings').val(hotel_ratings);
		} 
		else {
  	  $('input#alert_hotel_ratings').val('');
		}		
  }
  
	function hotel_all_rating_checked() {
		return $('input[name=filter_rating_all]')[0].checked;
	}
  
  function hotel_rating_filter_in_use() {
		if ($('input[name=filter_rating_all]').length > 0) {
			return !hotel_all_rating_checked();
		}
	}		
	
	function set_hotel_all_rating_checked(check) {
		$('input[name=filter_rating_all]')[0].checked = check;
		$('input[name=filter_rating_all]')[0].disabled = check;
		if (check) {
			$('input[name=filter_rating]').each(function() {
				$(this).removeAttr('checked');
			});
		}
	}
	
	$('input[name=filter_rating]').click(function() {
		var no_individual_ratings_checked = ($("input[name=filter_rating]:checked").length === 0);
		set_hotel_all_rating_checked(no_individual_ratings_checked);
		set_hotel_ratings();
	});

	$('input[name=filter_rating_all]').click(function(event) {
		set_hotel_all_rating_checked(event.target.checked);
		set_hotel_ratings();
	});
	
  $('.sample_alert_modal').modal();
});
