(function() {
// function() {
	/**
	 * объект foodOrderingData определен в файле food-ordering.php
	*/
	
	var searchByCity = document.getElementById('search-by-city');
	var searchByCityResult = document.getElementById('search-by-city-result');
	var searchByCityComments = document.getElementById('search-by-city-comments');
	
	var searchByFood = document.getElementById('search-by-food');
	var searchByFoodResult = document.getElementById('search-by-food-result');
	var searchByFoodComments = document.getElementById('search-by-food-comments');
	
	var fillSearchByCityResult = function(cityID) {
		emptyNode(searchByCityResult, true);
		var div, code, data = foodOrderingData['cities_food'][cityID];
		
		if (typeof data == 'undefined') {return;}
		for (var i=0; i<data.length; i++) {
			div = searchByCityResult.appendChild(document.createElement('div'));
			div.className = 'result-item';
			div.innerHTML = data[i].title;
			code = div.insertBefore(document.createElement('span'), div.firstChild);
			code.className = 'code';
			code.innerHTML = data[i].value;
		}
		setSearchComments(searchByCityComments, 
			foodOrderingData.lang == 'ru' ?
				'В аэропорту '+ searchByCity.options[searchByCity.selectedIndex].text +'<br/> вам могут быть предложены<br/>следующие виды спецпитания<span style="position: absolute">:</span>' :
				'At the airport of '+ searchByCity.options[searchByCity.selectedIndex].text +'<br/>the following special in-flight<br/>meals is available<span style="position: absolute">:</span>'
		);
	};
	
	var fillSearchByFoodResult = function(foodID) {
		emptyNode(searchByFoodResult, true);
		var cities = [], data = foodOrderingData['food_cities'][foodID];
		
		if (typeof data == 'undefined') {return;}
		for (var i=0; i<data.length; i++) {
			cities.push(data[i].title);
		}
		searchByFoodResult.innerHTML = cities.join(', ');
		setSearchComments(searchByFoodComments, 
			foodOrderingData.lang == 'ru' ?
				''+ searchByFood.options[searchByFood.selectedIndex].text +' вы можете заказать в аэропортах<span style="position: absolute">:</span>' :
				''+ searchByFood.options[searchByFood.selectedIndex].text +' type of special meals is available at the following airports<span style="position: absolute">:</span>'
		);
	};
	
	var setSearchComments = function(box, text) {
		box.innerHTML = text;
	}
	
	searchByCity.onchange = function() {
		fillSearchByCityResult(this.value);
	}
	
	searchByFood.onchange = function() {
		fillSearchByFoodResult(this.value);
	}
	
	fillComboBox(searchByCity, foodOrderingData['cities'], true);
	fillSearchByCityResult(searchByCity.value);
	
	fillComboBox(searchByFood, foodOrderingData['food'], true);
	fillSearchByFoodResult(searchByFood.value);
	
	return;
	
	var search_type_switch = document.getElementById('food-search-type');
	var search_filter = document.getElementById('food-search-filter');
	var search_button = document.getElementById('food-search-button');
	var search_result = document.getElementById('food-search-result');
	var search_comments = document.getElementById('food-search-comments');
	
	var get_search_type = function() {
		switch (search_type_switch.value) {
			case '0': search_type = false; break;
			case '1': search_type = 'cities'; break;
			case '2': search_type = 'food'; break;
		}
		return search_type;
	}
	
	var set_search_comments = function(name) {
		if (!name) {
			search_comments.innerHTML = '&nbsp;';
			return;
		}
		switch(get_search_type()) {
			case 'cities':
				search_comments.innerHTML = food_ordering.lang == 'ru' ?
					'В аэропорту <strong>'+ name +'</strong> вам могут быть предложены следующие виды спецпитания:' :
					'At the airport of <strong>'+ name +'</strong> the following special in-flight meals is available:';
			break;
			case 'food':
				search_comments.innerHTML = food_ordering.lang == 'ru' ?
					'<strong>'+ name +'</strong> вы можете заказать в аэропортах:' :
					'<strong>'+ name +'</strong> type of special meals is available at the following airports:';
			break;
		}
	}
	
	search_type_switch.onchange = function() {
		var search_type = get_search_type();
		if (!search_type) {
			emptyNode(search_filter, true);
			emptyNode(search_result, true);
			return;
		}
		set_search_comments(false);
		emptyNode(search_result, true);
		fillComboBox(search_filter, food_ordering[search_type], true);
	};
	
	search_filter.onchange = function() {
		emptyNode(search_result, true);
		set_search_comments(false);
		var li, data = food_ordering[search_type == 'cities' ? 'cities_food' : 'food_cities'][search_filter.value];
		if (typeof data == 'undefined') {return;}
		for (var i=0; i<data.length; i++) {
			li = search_result.appendChild(document.createElement('li'));
			li.innerHTML = data[i];
		}
		set_search_comments(search_filter.options[search_filter.selectedIndex].text);
	}
	
	if (get_search_type()) {
		fillComboBox(search_filter, food_ordering[get_search_type()], true);
	}
})();
// }
