$(document).ready( function() {
	/* РЕДАКТИРОВАНИЕ ОТЕЛЯ */

	/* бронирование */
	$book_by = $('#entity-edit').find('input[name="book_by"]:checked');
	booking_url_change($book_by.val());

	$book_by.live('change', function(){
		booking_url_change ( $('#entity-edit').find('input[name="book_by"]:checked').val() );
	});

	/* функция скрывает или показывает текстовое поле с URL на bookin.com */
	function booking_url_change(book_by){
		$booking_url = $('#booking_url');
		if (book_by == 0)
			$booking_url.addClass('hidden');
		else {
			if ($booking_url.hasClass('hidden'))
				$booking_url.removeClass('hidden');
		}
	}

	/* ПОИСК ГОСТИНИЦ */

	if ( $('#non-js').length > 0 ) {
		$titleInput = $('#search-form').find('input[name="title"]');
		searchHotelsAutocomplete( $titleInput, true );
		
		/* выбор элементов в popup-окне */
		createSearchQuery( false );

		$('a.choice').die().live('click', function() {
			$anchor = $(this);
			type = $('#elements').attr('rel');

			if ( $anchor.hasClass('pushed') ) {
				$anchor.removeClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы выделить');

				$('select[name="' + type + '[]"]').children('option[value="' + $anchor.attr('rel') + '"]').removeAttr('selected');
			} else {
				$anchor.addClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы снять выделение');

				$('select[name="' + type + '[]"]').children('option[value="' + $anchor.attr('rel') + '"]').attr('selected', 'selected');
			}
		});

		/* создание поисковой строки */
		$('#save-choice').die().live('click', function() {
			createSearchQuery( true );
			$.fancybox.close();
		});

		$('#non-js').addClass('hidden');

		$('#selects').append('<a title="Выберите район" rel="district" class="block single item" href="javascript:;"><span>Район</span></a>');
		$('#selects').append('<a title="Выберите метро" rel="subway" class="block single item" href="javascript:;"><span>Метро</span></a>');
		$('#selects').append('<a title="Выберите тип гостиницы" rel="type" class="block single item" href="javascript:;"><span>Тип гостиницы</span></a>');
		
		$('#selects').append('<a title="Выберите номер" rel="room" class="block room" href="javascript:;"><span>Номер &#x25be;</span></a>');

		$('#selects').append('<a title="Выберите питание" rel="food" class="block single item" href="javascript:;"><span>Питание</span></a>');
		$('#selects').append('<a title="Выберите инфраструктуру" rel="infra" class="block infra" href="javascript:;"><span>Инфраструктура &#x25be;</span></a>');
		$('#selects').append('<a title="Выберите услуги" rel="service" class="block service" href="javascript:;"><span>Услуги &#x25be;</span></a>');

		$('#selects').append('<a title="Выберите бизнес-услуги" rel="business" class="block single item" href="javascript:;"><span>Бизнес-услуги</span></a>');
		$('#selects').append('<a title="Выберите оборудование" rel="equipment" class="block single item" href="javascript:;"><span>Оборудование</span></a>');
		$('#selects').append('<a title="Красота/Здоровье" rel="beauty" class="block single item" href="javascript:;"><span>Красота/Здоровье</span></a>');
		$('#selects').append('<a title="Активный отдых" rel="active_leisure" class="block single item" href="javascript:;"><span>Активный отдых</span></a>');
		$('#selects').append('<a title="Выберите развлечения" rel="entertainment" class="block single item" href="javascript:;"><span>Развлечения</span></a>');

		$('#selects').append('<div class="clear"></div>');
		$('#selects').append('<a title="Выберите метки" rel="tags" class="block item" href="javascript:;"><span>Метки</span></a>');
		$('#selects').append('<div class="blank-item"></div>');
		$('#selects').append('<div class="clear"></div>');

		var $searchDiv = $('#search-form #selects');
		var $searchTerms = $('#search-terms');
		var $noScriptTable = $('#search-form #selects').children('#non-js');

		// НОМЕРА
		$('a.room[rel="room"]').live('click', openRoomFB );
		$('a.room.seval[rel="room[]"]').live('click', openRoomFB );

		$('a.room-choice').live('click', function() {
			$anchor = $(this);
			$roomClassElement = $noScriptTable.find('select[name="room_class[]"]');
			$roomEquipmentElement = $noScriptTable.find('select[name="room_equipment[]"]');
			$roomTypeElement = $noScriptTable.find('select[name="room_type[]"]');

			if ( $anchor.hasClass('pushed') ) {
				$anchor.removeClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы выделить');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('room-equipment') != -1 )
					$roomEquipmentElement.children('option[value="' + rel.substr(15) + '"]').removeAttr('selected');
				else if ( rel.indexOf('room-type') != -1 )
					$roomTypeElement.children('option[value="' + rel.substr(10) + '"]').removeAttr('selected');
				else
					$roomClassElement.children('option[value="' + rel.substr(11) + '"]').removeAttr('selected');
			} else {
				$anchor.addClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы снять выделение');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('room-equipment') != -1 )
					$roomEquipmentElement.children('option[value="' + rel.substr(15) + '"]').attr('selected', 'selected');
				else if ( rel.indexOf('room-type') != -1 )
					$roomTypeElement.children('option[value="' + rel.substr(10) + '"]').attr('selected', 'selected');
				else
					$roomClassElement.children('option[value="' + rel.substr(11) + '"]').attr('selected', 'selected');
			}
		});
		// <----- НОМЕРА ----->

		// ИНФРАСТРУКТУРА
		$('a.infra[rel="infra"]').live('click', openInfraFB );
		$('a.infra.seval[rel="infra[]"]').live('click', openInfraFB );

		$('a.infra-choice').live('click', function() {
			$anchor = $(this);
			$infraGuestzoneElement = $noScriptTable.find('select[name="infrastructure_guestzone[]"]');
			$infraFoodElement = $noScriptTable.find('select[name="infrastructure_food[]"]');
			$infraAdditionalElement = $noScriptTable.find('select[name="infrastructure_additional[]"]');

			if ( $anchor.hasClass('pushed') ) {
				$anchor.removeClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы выделить');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('infra-guestzone') != -1 )
					$infraGuestzoneElement.children('option[value="' + rel.substr(16) + '"]').removeAttr('selected');
				else if ( rel.indexOf('infra-food') != -1 )
					$infraFoodElement.children('option[value="' + rel.substr(11) + '"]').removeAttr('selected');
				else
					$infraAdditionalElement.children('option[value="' + rel.substr(17) + '"]').removeAttr('selected');
			} else {
				$anchor.addClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы снять выделение');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('infra-guestzone') != -1 )
					$infraGuestzoneElement.children('option[value="' + rel.substr(16) + '"]').attr('selected', 'selected');
				else if ( rel.indexOf('infra-food') != -1 )
					$infraFoodElement.children('option[value="' + rel.substr(11) + '"]').attr('selected', 'selected');
				else
					$infraAdditionalElement.children('option[value="' + rel.substr(17) + '"]').attr('selected', 'selected');
			}
		});
		// <----- ИНФРАСТРУКТУРА ----->

		// УСЛУГИ
		$('a.service[rel="service"]').live('click', openServiceFB );
		$('a.service.seval[rel="service[]"]').live('click', openServiceFB );

		$('a.service-choice').live('click', function() {
			$anchor = $(this);
			$serviceReceptionElement = $noScriptTable.find('select[name="service_reception[]"]');
			$servicePaymentElement = $noScriptTable.find('select[name="service_payment[]"]');
			$serviceAdditionalElement = $noScriptTable.find('select[name="service_additional[]"]');

			if ( $anchor.hasClass('pushed') ) {
				$anchor.removeClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы выделить');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('service-reception') != -1 )
					$serviceReceptionElement.children('option[value="' + rel.substr(18) + '"]').removeAttr('selected');
				else if ( rel.indexOf('service-payment') != -1 )
					$servicePaymentElement.children('option[value="' + rel.substr(16) + '"]').removeAttr('selected');
				else
					$serviceAdditionalElement.children('option[value="' + rel.substr(19) + '"]').removeAttr('selected');
			} else {
				$anchor.addClass('pushed');
				$anchor.attr('title', 'Нажмите, чтобы снять выделение');

				rel = $anchor.attr('rel');
				if ( rel.indexOf('service-reception') != -1 )
					$serviceReceptionElement.children('option[value="' + rel.substr(18) + '"]').attr('selected', 'selected');
				else if ( rel.indexOf('service-payment') != -1 )
					$servicePaymentElement.children('option[value="' + rel.substr(16) + '"]').attr('selected', 'selected');
				else
					$serviceAdditionalElement.children('option[value="' + rel.substr(19) + '"]').attr('selected', 'selected');
			}
		});
		// <----- УСЛУГИ ----->

		$('a.item').live('click', function() {
			$item = $(this);

			var options = new Array();
			$('#non-js').find('select[name="' + $item.attr('rel') + '[]"]').children().each( function() {
				if ( $(this).attr('selected') ) {
					selected = ' pushed';
					title = 'Нажмите, чтобы снять выделение';
				} else {
					selected = '';
					title = 'Нажмите, чтобы выделить';
				}

				options.push('<a title="' + title + '" class="choice' + selected + '" rel="' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
			});

			h2 = $item.attr('title');
			if ( h2.length == 0 )
				h2 = $('#selects a[rel="' + $item.attr('rel') + '"]').attr('title');

			$.fancybox(
				'<h2>' + h2 + '</h2><div id="elements" rel="' + $item.attr('rel') + '">' + options.join('') + '</div><input type="button" id="save-choice" value="Сохранить"> или <input type="button" id="reset-choice" value="Очистить все">', {
					'autoDimensions' : false,
					'width' : 500,
					'height' : 382,
					'transitionIn' : 'none',
					'transitionOut' : 'none'
				}
			);
		}).each( function() {
			if ( $(this).hasClass('seval') )
				return true;

			$(this).children('span').append(' &#x25be;');
		});
	}
});

function openServiceFB() {
	var $noScriptTable = $('#search-form #selects').children('#non-js');
	var $serviceReceptionElement = $noScriptTable.find('select[name="service_reception[]"]');
	var $servicePaymentElement = $noScriptTable.find('select[name="service_payment[]"]');
	var $serviceAdditionalElement = $noScriptTable.find('select[name="service_additional[]"]');

	var serviceReceptionOptions = new Array();
	$serviceReceptionElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		serviceReceptionOptions.push('<a title="' + title + '" class="service-choice' + selected + '" rel="service-reception-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var servicePaymentOptions = new Array();
	$servicePaymentElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		servicePaymentOptions.push('<a title="' + title + '" class="service-choice' + selected + '" rel="service-payment-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var serviceAdditionalOptions = new Array();
	$serviceAdditionalElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		serviceAdditionalOptions.push('<a title="' + title + '" class="service-choice' + selected + '" rel="service-additional-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var options = new Array();

	options.push( '<div class="floatLeft options-col"><b>Прием</b><br>' );
	for ( i=0, len=Math.max( serviceReceptionOptions.length ); i<len; i++ ) {
			options.push( serviceReceptionOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Оплата</b><br>' );
	for ( i=0, len=Math.max( servicePaymentOptions.length ); i<len; i++ ) {
			options.push( servicePaymentOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Дополнительные услуги</b><br>' );
	for ( i=0, len=Math.max( serviceAdditionalOptions.length ); i<len; i++ ) {
			options.push( serviceAdditionalOptions[i] );
	}
	options.push( '</div>' );

	$.fancybox(
		'<h2>Выберите услуги</h2><div id="options">' + options.join('') + '</div><input type="button" id="save-choice" value="Сохранить"> или <input type="button" id="reset-choice" value="Очистить все">', {
			'autoDimensions' : false,
			'width' : 600,
			'height' : 380,
			'transitionIn' : 'none',
			'transitionOut' : 'none'
		}
	);
}

function openInfraFB() {
	var $noScriptTable = $('#search-form #selects').children('#non-js');
	var $infraGuestzoneElement = $noScriptTable.find('select[name="infrastructure_guestzone[]"]');
	var $infraFoodElement = $noScriptTable.find('select[name="infrastructure_food[]"]');
	var $infraAdditionalElement = $noScriptTable.find('select[name="infrastructure_additional[]"]');

	var infraGuestzoneOptions = new Array();
	$infraGuestzoneElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		infraGuestzoneOptions.push('<a title="' + title + '" class="infra-choice' + selected + '" rel="infra-guestzone-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var infraFoodOptions = new Array();
	$infraFoodElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		infraFoodOptions.push('<a title="' + title + '" class="infra-choice' + selected + '" rel="infra-food-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var infraAdditionalOptions = new Array();
	$infraAdditionalElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		infraAdditionalOptions.push('<a title="' + title + '" class="infra-choice' + selected + '" rel="infra-additional-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var options = new Array();

	options.push( '<div class="floatLeft options-col"><b>Гостевые зоны</b><br>' );
	for ( i=0, len=Math.max( infraGuestzoneOptions.length ); i<len; i++ ) {
			options.push( infraGuestzoneOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Инфраструктура питания</b><br>' );
	for ( i=0, len=Math.max( infraFoodOptions.length ); i<len; i++ ) {
			options.push( infraFoodOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Дополнительная инфраструктура</b><br>' );
	for ( i=0, len=Math.max( infraAdditionalOptions.length ); i<len; i++ ) {
			options.push( infraAdditionalOptions[i] );
	}
	options.push( '</div>' );

	$.fancybox(
		'<h2>Выберите параметры инфраструктуры</h2><div id="options">' + options.join('') + '</div><input type="button" id="save-choice" value="Сохранить"> или <input type="button" id="reset-choice" value="Очистить все">', {
			'autoDimensions' : false,
			'width' : 650,
			'height' : 380,
			'transitionIn' : 'none',
			'transitionOut' : 'none'
		}
	);
}

function openRoomFB() {
	var $noScriptTable = $('#search-form #selects').children('#non-js');
	var $roomClassElement = $noScriptTable.find('select[name="room_class[]"]');
	var $roomEquipmentElement = $noScriptTable.find('select[name="room_equipment[]"]');
	var $roomTypeElement = $noScriptTable.find('select[name="room_type[]"]');

	var roomClassOptions = new Array();
	$roomClassElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		roomClassOptions.push('<a title="' + title + '" class="room-choice' + selected + '" rel="room-class-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var roomTypeOptions = new Array();
	$roomTypeElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		roomTypeOptions.push('<a title="' + title + '" class="room-choice' + selected + '" rel="room-type-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var roomEquipmentOptions = new Array();
	$roomEquipmentElement.children().each( function() {
		if ( $(this).attr('selected') ) {
			selected = ' pushed';
			title = 'Нажмите, чтобы снять выделение';
		} else {
			selected = '';
			title = 'Нажмите, чтобы выделить';
		}

		roomEquipmentOptions.push('<a title="' + title + '" class="room-choice' + selected + '" rel="room-equipment-' + $(this).val() + '" href="javascript:;"><span>' + $(this).html() + '</span></a>');
	});

	var options = new Array();

	options.push( '<div class="floatLeft options-col"><b>Класс номера</b><br>' );
	for ( i=0, len=Math.max( roomClassOptions.length ); i<len; i++ ) {
			options.push( roomClassOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Тип номера</b><br>' );
	for ( i=0, len=Math.max( roomTypeOptions.length ); i<len; i++ ) {
			options.push( roomTypeOptions[i] );
	}
	options.push( '</div>' );

	options.push( '<div class="floatLeft options-col"><b>Оснащение номера</b><br>' );
	for ( i=0, len=Math.max( roomEquipmentOptions.length ); i<len; i++ ) {
			options.push( roomEquipmentOptions[i] );
	}
	options.push( '</div>' );

	$.fancybox(
		'<h2>Выберите параметры номера</h2><div id="options">' + options.join('') + '</div><div class="clear"></div><input type="button" id="save-choice" value="Сохранить"> или <input type="button" id="reset-choice" value="Очистить все">', {
			'autoDimensions' : false,
			'width' : 600,
			'height' : 380,
			'transitionIn' : 'none',
			'transitionOut' : 'none'
		}
	);
}

function createSearchQuery( performJsonReq ) {
	var $noScriptTable = $('#search-form #selects').children('#non-js');

	var $roomClassElement = $noScriptTable.find('select[name="room_class[]"]');
	var $roomEquipmentElement = $noScriptTable.find('select[name="room_equipment[]"]');
	var $roomTypeElement = $noScriptTable.find('select[name="food[]"]');
	
	$nonJs = $('#non-js');

	/* итоговая строка */
	query = '';

	/* район */
	$selectedDistrict = $nonJs.find('select[name="district[]"]').children('option:selected');
	if ( $selectedDistrict.length > 0 )
		query += 'в ' + processOptions('district', $selectedDistrict, true ) + ' районе ';

	/* метро */
	$selectedSubway = $nonJs.find('select[name="subway[]"]').children('option:selected');
	if ( $selectedSubway.length > 0 )
		query += 'поблизости  от ст.м. ' + processOptions('subway', $selectedSubway, true ) + ' ';

	/* Тип номера */
	$selectedRoomType = $nonJs.find('select[name="room_type[]"]').children('option:selected');
	if ( $selectedRoomType.length > 0 )
		query += ', где есть ' + processOptions('room_type', $selectedRoomType, false ) + ' номер ';

	/* Класс номера */
	$selectedRoomClass = $nonJs.find('select[name="room_class[]"]').children('option:selected');
	if ( $selectedRoomClass.length > 0 )
		query += 'класса ' + processOptions('room_class', $selectedRoomClass, false ) + '  ';

	/* Оснащение номера */
	$selectedRoomEquipment = $nonJs.find('select[name="room_equipment[]"]').children('option:selected');
	if ( $selectedRoomEquipment.length > 0 )
		query += ', в котором есть ' + processOptions('room_equipment', $selectedRoomEquipment, false ) + '  ';

	/* бизнес-услуги */
	$selectedBusiness = $nonJs.find('select[name="business[]"]').children('option:selected');
	if ( $selectedBusiness.length > 0 )
		query += 'с бизнес-услугами ' + processOptions('business', $selectedBusiness, false ) + ' ';

	/* спорт и развлечения */
	$selectedEntert = $nonJs.find('select[name="entertainment[]"]').children('option:selected');
	if ( $selectedEntert.length > 0 )
		query += ', а также ' + processOptions('entertainment', $selectedEntert, false ) + ' ';

	/* питание */
	$selectedFood = $nonJs.find('select[name="food[]"]').children('option:selected');
	if ( $selectedFood.length > 0 )
		query += 'с питанием ' + processOptions('food', $selectedFood, false ) + ' ';

	/* Гостевые зоны */
	$selectedInfraGuestzone = $nonJs.find('select[name="infrastructure_guestzone[]"]').children('option:selected');
	if ( $selectedInfraGuestzone.length > 0 )
		query += ', с гостевой зоной ' + processOptions('infrastructure_guestzone', $selectedInfraGuestzone, false );

	/* Инфраструктура питания */
	$selectedInfraFood = $nonJs.find('select[name="infrastructure_food[]"]').children('option:selected');
	if ( $selectedInfraFood.length > 0 )
		query += ', где есть ' + processOptions('infrastructure_food', $selectedInfraFood, false );

	/* Доп. инфраструктура */
	$selectedInfraAdditional = $nonJs.find('select[name="infrastructure_additional[]"]').children('option:selected');
	if ( $selectedInfraAdditional.length > 0 )
		query += ', где есть ' + processOptions('infrastructure_additional', $selectedInfraAdditional, false );

	/* Услуги прием */
	$selectedServiceReception = $nonJs.find('select[name="service_reception[]"]').children('option:selected');
	if ( $selectedServiceReception.length > 0 )
		query += ', с услугами ' + processOptions('service_reception', $selectedServiceReception, false );

	/* Услуги оплата */
	$selectedServicePayment = $nonJs.find('select[name="service_payment[]"]').children('option:selected');
	if ( $selectedServicePayment.length > 0 )
		query += ', с оплатой через ' + processOptions('service_payment', $selectedServicePayment, false );

	/* Доп услуги */
	$selectedServiceAdditional = $nonJs.find('select[name="service_additional[]"]').children('option:selected');
	if ( $selectedServiceAdditional.length > 0 )
		query += ', с услугами ' + processOptions('service_additional', $selectedServiceAdditional, false );

	/* Доп услуги */
	$selectedEquipment = $nonJs.find('select[name="equipment[]"]').children('option:selected');
	if ( $selectedEquipment.length > 0 )
		query += ', где есть ' + processOptions('equipment', $selectedEquipment, false );

	/* Красота Здоровье */
	$selectedBeauty = $nonJs.find('select[name="beauty[]"]').children('option:selected');
	if ( $selectedBeauty.length > 0 )
		query += ', где есть ' + processOptions('beauty', $selectedBeauty, false );

	/* активный отдых */
	$selectedActiveLeisure = $nonJs.find('select[name="active_leisure[]"]').children('option:selected');
	if ( $selectedActiveLeisure.length > 0 )
		query += ', с активным отдыхом ' + processOptions('active_leisure', $selectedActiveLeisure, false );

	/* метки */
	$selectedTags = $nonJs.find('select[name="tags[]"]').children('option:selected');
	if ( $selectedTags.length > 0 )
		query += 'с метками ' + processOptions('tags', $selectedTags, false ) + ' ';

	/* тип */
	$selectedTypes = $nonJs.find('select[name="type[]"]').children('option:selected');
	if ( $selectedTypes.length > 0 ) {
		query = processOptions('type', $selectedTypes ) + ' ' + query;
	} else {
		if ( query == '' )
			$('#search-terms').addClass('hidden');
		else
			query = 'Гостиницу ' + query;
	}

	if ( query.length > 1 )
		$('#search-terms').removeClass('hidden').html( '<span>Вы ищете:</span> ' + query );

	if ( performJsonReq ) {
		$('#search-results').html( loadingImg );
		$.getJSON( '/hotels2/catalogue?' + $('#sform').serialize(), function( data ) {
			$('#search-results').replaceWith(data.content);

			//yaCounter1087174.hit(url, title, referer);
		});
	}
}

function processOptions( selectName, $selectedOptions, leaveCase ) {
	var output = new Array();
	var item = '';
	var name = '';
	$selectedOptions.each( function() {
		if ( selectName == 'district' )
			item = inflect.district[ $(this).val() ];
		else if ( selectName == 'cuisine' )
			item = inflect.cuisine[ $(this).val() ];
		else
			item = $(this).html();

		if ( !leaveCase )
			item = item.toLowerCase();

		if ( selectName == 'room_class' )
			name = 'title="Класс номера"';
		if ( selectName == 'room_type' )
			name = 'title="Тип номера"';
		if ( selectName == 'room_equipment' )
			name = 'title="Оснащение номера"';
		if ( selectName == 'infrastructure_guestzone' )
			name = 'title="Гостевые зоны"';
		if ( selectName == 'infrastructure_food' )
			name = 'title="Инфраструктура еды"';
		if ( selectName == 'infrastructure_additional' )
			name = 'title="Дополнительная инфраструктура"';
		if ( selectName == 'service_reception' )
			name = 'title="Прием"';
		if ( selectName == 'service_payment' )
			name = 'title="Оплата"';
		if ( selectName == 'service_additional' )
			name = 'title="Дополнительные услуги"';
		
		item = '<a '+ name +' class="item seval" rel="' + selectName + '" href="javascript:;"><span>' + item + '</span></a>';
		output.push(item);
	});

	last = output.pop();
	if ( $selectedOptions.length == 1 )
		return last;

	if ( $selectedOptions.length == 2 )
		return output[0] + ' или ' + last;

	return output.join(', ') + ' или ' + last;
}

