
$(document).ready(function(){

	init_city_select();

	$('#submit-desc').keydown(function(){
		chars			= $('#max-chars').val();
		count_chars("submit-desc", chars);
	});

	$('#submit-desc').keyup(function(){
		chars			= $('#max-chars').val();
		count_chars("submit-desc", chars);
	});
	
	$('#local-city').change(function(){
		
		type			= $('#type').val();
		continent		= $('#city-0 select.continent-drop:first').val();
		region			= $('#city-0 select.region-drop:first').val();
		id				= $('#city-0 select.city-drop:first').val();
		label			= $('#city-0 select.city-drop option[@value="' + id + '"]').text();
		window.location = '/resources/show/' + type + '/' + id + '/' + region + '/' + label;
	});


	$('#educator-city').change(function(){
		
		continent		= $('#city-0 select.continent-drop:first').val();
		region			= $('#city-0 select.region-drop:first').val();
		label			= $('#city-0 select.city-drop option[@value="' + $(this).val() + '"]').text();
		window.location = '/resources/educators/' + continent + '/' + region + '/' + $(this).val() + '/' + label;
	});

	$('#restaurant-city').change(function(){
		
		continent		= $('#city-0 select.continent-drop:first').val();
		region			= $('#city-0 select.region-drop:first').val();
		label			= $('#city-0 select.city-drop option[@value="' + $(this).val() + '"]').text();
		window.location = '/resources/restaurants/' + $(this).val() + '/' + 'BYOB-restaurants-in-' + label;
	});

	$('#link-city').change(function(){
		
		continent		= $('#city-0 select.continent-drop:first').val();
		region			= $('#city-0 select.region-drop:first').val();
		label			= $('#city-0 select.city-drop option[@value="' + $(this).val() + '"]').text();
		window.location = '/links/for_city/' + $(this).val() + '/' + 'links-for-' + label;
	});

	$('#festival-city').change(function(){
		
		continent		= $('#city-0 select.continent-drop:first').val();
		region			= $('#city-0 select.region-drop:first').val();
		window.location = '/festivals/near/' + $(this).val() + '/';
	});

	if (curr_continent != ''){
		
		update_continent();
		update_region(curr_continent, 'city-0');
	}
});

function init_city_select(){
	
	$('#city-0 select.continent-drop').change(function(){ update_region($(this).val(), 'city-0'); });
	$('#city-0 select.region-drop').change(function(){ update_city($(this).val(), 'city-0'); });
	
	$('#city-alt select.continent-drop').change(function(){ update_region($(this).val(), 'city-alt'); });
	$('#city-alt select.region-drop').change(function(){ update_city($(this).val(), 'city-alt'); });

}

function update_city(ref, par){

	$.getJSON('/cities/find_all_for_region/' + ref + '/' + $('#' + par + ' select.continent-drop').val() + '/', null, function(j){
	
		var options = '<option>Choose A City</option>';

		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].id + '">' + j[i].name + '</option>';
		}

		$("#" + par + " select.city-drop").html(options);
		$("#" + par + " select.city-drop").attr('disabled', false);

		if (curr_city != ''){
			
			$('#' + par + 'select.city-drop option[@value="' + curr_city + '"]').attr('selected', 'selected');
			curr_city = '';
		
		} else {
			
			$("#" + par + " select.city-drop option:first").attr('selected', 'selected');
		}
	})
}

function update_region(ref, par){

	$.getJSON('/cities/find_parents_for_continent/' + ref + '/', null, function(j){

		var options = '<option>Choose A Region</option>';

		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i] + '">' + j[i] + '</option>';
		}

		$("#" + par + " select.region-drop").html(options);
		$("#" + par + " select.region-drop").attr('disabled', false);
		$("#" + par + " select.city-drop").attr('disabled', true);

		if (curr_region != ''){
			
			$('#' + par + ' select.region-drop option[@value="' + curr_region + '"]').attr('selected', 'selected');
			update_city(curr_region, 'city-0');

			curr_region = '';
		
		} else {
			
			$("#" + par + " select.region-drop option:first").attr('selected', 'selected');
		}
	})
}

function update_continent(){
	
	$('#city-0 select.continent-drop option[@value="' + curr_continent + '"]').attr('selected', 'selected');
}
