$(document).ready(function() {

	function check_location() {
    $.getJSON("/destinations/exists", 
							{ name: $('input#location_unique_name').val() },
							function(json) {
								if (json.result == 'true') {
									$('div#new_location_parent').hide();
									$('div#new_location_kind').hide();
								}	else { 
									$('#hotel_location_id').val('');
									$('div#new_location_parent').show();
									$('div#new_location_kind').show();
									$('input#parent_location').focus();
								}
							} );
  }

  $('input#location_unique_name').autocomplete("/locations/auto_complete_for_location_name", 
																				{ matchContains:1, 
																					delay:400,
																					minChars:2, 
																					matchSubset:false, 
																					cacheLength:1, 
																					selectFirst:true,
																					max:10 });
	
	$('input#location_unique_name').result(function(event, data, formatted, oldValue) {
		$('#hotel_location_id').val(data[1]);
		check_location();
	});
	
  $('input#location_unique_name').blur(check_location);
	
  $('input#parent_location').autocomplete("/locations/auto_complete_for_location_name_with_states_and_countries", 
																					{ matchContains:1, 
																						delay:400,
																						minChars:2, 
																						matchSubset:false, 
																						cacheLength:1, 
																						selectFirst:true,
																						max:10 });
																						
	$('input#parent_location').result(function(event, data, formatted) {
		$('#location_parent_id').val(data[1]);
	});
	
	$('input#parent_location').blur(function() {
		if ($(this).val() == '') {
			$('#location_parent_id').val('');
		}
	});

	// Show either the URL field or the File upload for the Hotel photo
	function togglePhotoSources() {
		selected = $("#photo_source option:selected").val();
		if (selected == 'URL') {
			$('#photo_file_upload').hide();
			$('#photo_url_upload').show();
		} else {
			$('#photo_file_upload').show();
			$('#photo_url_upload').hide();
		}
	}
	
	$('#photo_source').change( togglePhotoSources );
	
	// Get the photo sources into the proper initially hidden/shown states 
	togglePhotoSources();
	
	// Setup the IAN hotel link auto-complete and handling
	$('input#ian_hotel_unique_name').autocomplete("/hotels/auto_complete_for_ian_hotel", 
																							  { matchContains:1, 
																							    minChars:2, 
																							    matchSubset:false, 
																							    cacheLength:1, 
																							    selectFirst:true,
																							    max:10 });

	$('input#ian_hotel_unique_name').result(function(event, data, formatted) {
		var id_and_rate = data[1].split(",");
		$('#hotel_ian_hotel_id').val(id_and_rate[0]).change();
		$('#ian_rate_per_night_value').html('$' + id_and_rate[1]).change();
	});
	
	function unlink_rate() {
		if (confirm("Are you sure you want to unlink this hotel's rate from the hotels.com hotel?")) {
			$('input#hotel_ian_hotel_id').val('').change();
			$('input#ian_hotel_unique_name').val('').change();
			$('#ian_rate_per_night_value').html('no hotel selected yet').change();
			return true;
		} else {
			return false;
		}
	}

	$('#unlink_rate_link').click( function() {
		unlink_rate();
	});
		
	// Show/hide the linked rate icon and text if we are linked to a hotel
	$('input#hotel_ian_hotel_id').change( 
		function() {
			if ($(this).val() && ($(this).val() > 0)) {
				$('#linked_rate_indicator').show();
				//$('#linked_rate_message').html('Rate will be linked to Hotels.com property ' + $('input#ian_hotel_unique_name').val());
			} else {
				$('#linked_rate_indicator').hide();
				//$('#linked_rate_message').html('Rate is not linked to a Hotels.com rate');
			}
		}
	).change();
	
	$('#manual_hotel_rate_switch').click(function() {
		if ($('#hotel_ian_hotel_id').val() == '' ? true : unlink_rate()) {
			$('div#ian_hotel_fields').hide();
			$('div#hotel_source_fields').show();
			$('input#ian_hotel_unique_name').val('').change();
		}
	});
	$('#link_to_ian_hotel_switch').click(function() {
		$('div#hotel_source_fields').hide();
		$('div#ian_hotel_fields').show();
		$('input#hotel_rate_source').val('').change();
	});
	
	// Reload the reCaptcha, so that it doesn't inadvertantly say "incorrect" even though we've not yet tried
	// if (typeof(Recaptcha) != 'undefined') {
	// 	Recaptcha.reload();
	// }
	
});
