var keyword;
var minimumCharsNeeded = 2;
var path;
var searchCity = {};
var timer;
var timerDelay = 500;
var type;
var zipRegExp = /^[0-9]+$/i; // only numbers

searchCity = {

	select: function (cityName, cityId) {
		keyword = cityName;
		// place cityName in input field
		jQuery('#search_city').val(cityName);
		// place cityId in input field
		jQuery('#city_id').val(cityId);
		// remove results
		jQuery('#search_a_city_results').empty().hide();
	},

	reset: function (addEmptyLi) {
		jQuery('#search_a_city_results').empty();
		jQuery('#city_id').val('');

 		if (addEmptyLi === true) {
			jQuery('#search_a_city_results')
				.html('<li style="display:none;"></li>')
				.hide();
 		} else {
			jQuery('#search_a_city_results').hide();
 		}
		// disable search button
		new searchCity.disableButton();
	},

	disableButton: function () {
		// disable search button
		jQuery('#search_city_button').attr('disabled', 'disabled');
	},

	timer: function (keyword) {
		path = jQuery('#weather_path').val();
		// launch timer
		timer = setTimeout(function () {
			// match zip or city
			if (keyword.match(zipRegExp)) {
				type = 'zip';
			} else {
				type = 'city';
			}
			if (type !== null) {
				jQuery.getJSON(
					path + '?mode=normal',
					{
						m: 'getLocationByNeedle',
						needle: keyword
					},
			        function (data) {
						var match = 0;
						var total = 0;
						var firstResultName = '';
						var firstResultId = '';
						new searchCity.reset(false);
						if (data !== null) {
							if (jQuery(data.city).length > 0) {
								// undisable search button
								jQuery('#search_city_button').attr('disabled', '');
							}
							// loop through results
							for (cityId in data.city) {
								// set first results to use later
								if (firstResultName === '') { firstResultName = data.city[cityId]; }
								if (firstResultId === '') { firstResultId = cityId; }
								jQuery('#search_a_city_results').append('<li class="search_a_city_result"><a href="javascript:;">'+data.city[cityId]+'</a> <span class="search_result_sub" style="display:none;">'+cityId+'</span></li>');

								total++;
								if (keyword === data.city[cityId].toLowerCase()) { match = 1; }
							}
							// if there are multiple results and we want to submit without choosing one, take the first result
							if (type === 'zip') {
								jQuery('#city_id').val('100' + firstResultName);
							} else if (type === 'city') {
								jQuery('#city_id').val(firstResultId);
							}
							// show results
							jQuery('#search_a_city_results').show();
							if (type === 'zip') {
								jQuery('.search_result_sub').show();
							}
							// make results clickable
							jQuery('.search_a_city_result').click(function () {
								var cityName = jQuery(this).find('a').text();
								var cityId = jQuery(this).find('span').text();

								if (type === 'zip') {
									cityId = '100' + cityName;
								}
								new searchCity.select(cityName, cityId);
							});
							// if 1 result is found, place it in the input field
							if (total === 1 || match === 1) {
								// check type
								if (type === 'zip') {
									new searchCity.select(firstResultId, '100' + firstResultName); // watch out! id.val = name && name.val = '100' + id
								} else if (type === 'city') {
									new searchCity.select(firstResultName, firstResultId);
								}
							}
						}
					}
				);
			}
		}, timerDelay);
	},

	timeout: function () {
		setTimeout(function () {

			keyword = jQuery('#search_city').val();

			clearTimeout(timer);
			if (keyword !== '' && keyword.length >= minimumCharsNeeded) {
				new searchCity.timer(keyword);
			} else {
				new searchCity.reset(true);
			}

		}, 200);
	},

	_last: true
};

jQuery(document).ready(function () {
	keyword = '';
	jQuery('#search_city').keypress(function (e) {
		if (e.which == 32 || e.which == 224 || e.which == 232 || e.which == 233 || (e.which >= 48 && e.which <= 57) || (65 <= e.which && e.which <= 65 + 25) || (97 <= e.which && e.which <= 97 + 25)) {
			new searchCity.timeout();
			new searchCity.disableButton();
		}
	});
	jQuery('#search_city').keydown(function (e) {
		// backspace in IE only be on keydown
		if (e.which === 8 || e.which === 46 || e.which === 86) {
			new searchCity.timeout();
			new searchCity.disableButton();
		}
	});
	// special select case
	jQuery('#search_city').select(function (selectedText) {
		jQuery('#search_city').keydown(function (e) {
			if (e.which === 8 || e.which === 46) {
				new searchCity.timeout();
				new searchCity.disableButton();
			}
		});
	});
});