$(document).ready(function()
{
	$('input[type="text"]', '#weather, #temperature').focus(function() { $(this).select(); });
	
	$('#temperature input[name="temp_to_convert"]').keydown(function(e){
		if (e.keyCode == 13) {
			return do_temp_calculation();
		}
	});
	$('#temperature input[name="bereken"]').click(do_temp_calculation);
	
	$('#weather input[name="country"]').keydown(function(e){
		if (e.keyCode == 13) {
			return search_country();
		}
	}).autocomplete('/autocomplete/ajax', {
		autoFill: true,
		extraParams: {
			type: 'country'
		}
	}).blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val(this.getAttribute('value'));
		}
	});
	
	$('#weather input[name="search_country"]').click(search_country);
	
	$('#weather input[name="city"]').keydown(function(e){
		if (e.keyCode == 13) {
			return search_city();
		}
	}).autocomplete('/autocomplete/ajax', {
		autoFill: true,
		minChars: 3,
		extraParams: {
			type: 'city'
		}
	}).blur(function() {
		if ($.trim($(this).val()) == "") {
			$(this).val(this.getAttribute('value'));
		}
	});
	$('#weather input[name="search_city"]').click(search_city);
	$('#alphabar > li').click(function(){
		$.ajax({
			method: 'POST',
			url: '/home/ajax',
			data: {
				action: 'country_alphabar',
				letter: this.innerHTML
			},
			success: function(msg) {
				$('#alphabar-countries').html(msg);
			}
		});
	});
});

function do_temp_calculation()
{
	var value = $('#temperature input[name="temp_to_convert"]').val();
	if (!$.utils.validate_number(value))
	{
		$('#temperature #temperature_convert_result').addClass('error').html(
			'Please enter a valid number');
	}
	else
	{
		var msg = '';
		switch ($('#temperature select[name="conversion"]').val())
		{
			case 'c2f': {
				result = Math.round($.utils.convert_celsius_to_farenheit(value)*100)/100;
				msg = value + ' degrees celsius<br />' +
					'&nbsp;&nbsp;&nbsp;&nbsp;= <strong>'+result+' degrees farenheit</strong>';
			}
			break;
			
			case 'f2c': {
				result = Math.round($.utils.convert_farenheit_to_celsius(value)*100)/100;
				msg = value + ' degrees farenheit<br />' +
					'&nbsp;&nbsp;&nbsp;&nbsp;= <strong>'+result+' degrees celsius</strong>';
			}
			break;
		}
		$('#temperature #temperature_convert_result').removeClass('error').html(msg);
	}
	return false;
};

function search_country()
{	$.ajax({		method: 'POST',		url: '/home/ajax',		data: {			action: 'find_country',			country: $('input[name="country"]').val()		},		success: function(msg) {			result = $.trim($(msg).find('#result').text());			if (result == 0) {				$.dialogutils.message_dialog('Niet gevonden', 'Het gezochte land kon helaas niet gevonden worden.');				return;			}			window.location = $(msg).find('#url').text();		}	});
	return false;
};

function search_city()
{	$.ajax({		method: 'POST',		url: '/home/ajax',		data: {			action: 'find_city',			city: $('input[name="city"]').val()		},		success: function(msg) {			result = $.trim($(msg).find('#result').text());			if (result == 0) {				$.dialogutils.message_dialog('Niet gevonden', 'De gezochte stad kon helaas niet gevonden worden.');				return;			}			window.location = $(msg).find('#url').text();		}	});
	return false;
};

