/**
* Autocompletion
*/

// Lorsque la totalité de la page est chargée
	$(document).ready(function() {
		function formatItem(row) {
			return row[0] + " (<strong>id: " + row[1] + "</strong>)";
		}
		function formatResult(row) {
			return row[0].replace(/(<.+?>)/gi, '');
		}

		/**
		 * Autocomplétion ville de départ
		 */
		idChampValue_depart = '#villeDepart';
		idChampHiddenValue_depart = '#idVilleDepart';
		idChampPaysValue_depart = '#paysDepart';

		$(idChampValue_depart).autocomplete("/ajax/geonames.php?action=SearchCityMemory", {
			width: 260,
			minChars: 3,
			selectFirst: true,
			delay: 200,
			cacheLength: 1,
			max: 150
		}).focus();

		$(idChampValue_depart).result(function(event, data_depart, formatted) {
			if (data_depart) {
				//Valeur du champ input après sélection
				$(idChampValue_depart).val(data_depart[2]);
				//Valeur de l'idVille après sélection
				$(idChampHiddenValue_depart).val(data_depart[1]);
				//Valeur de l'idPays après sélection
				$(idChampPaysValue_depart).val(data_depart[3]);

				//Lieu correspondant au Depart
				GetLieuPublic($(idChampHiddenValue_depart).val(), '#idLieuDepart');

			}
		});

		/**
		 * Autocomplétion ville d'arrivée
		 */
		idChampValue_arrivee = '#villeArrivee';
		idChampHiddenValue_arrivee = '#idVilleArrivee';
		idChampPaysValue_arrivee = '#paysArrivee';

		$(idChampValue_arrivee).autocomplete("/ajax/geonames.php?action=SearchCityMemory", {
			width: 260,
			minChars: 3,
			selectFirst: true,
			delay: 200,
			cacheLength: 1,
			max: 150
		}).focus();

		$(idChampValue_arrivee).result(function(event, data_arrivee, formatted) {
			if (data_arrivee) {
				//Valeur du champ input après sélection
				$(idChampValue_arrivee).val(data_arrivee[2]);
				//Valeur de l'idVille après sélection
				$(idChampHiddenValue_arrivee).val(data_arrivee[1]);
				//Valeur de l'idPays après sélection
				$(idChampPaysValue_arrivee).val(data_arrivee[3]);

				//Lieu correspondant au Arrivee
				GetLieuPublic($(idChampHiddenValue_arrivee).val(), '#idLieuArrivee');
			}
		});
	});