

function onRoomTypeChange ($e) {
	// show/hide child age dropdown based on room type
	var $room_types = {"3": {"option_id": "3","title": "Divvietīgs","long_title": "","child_count": "0","adults": "2","gta_code": "DB"},"4": {"option_id": "4","title": "Vienvietīgs","long_title": "","child_count": "0","adults": "1","gta_code": "SB"},"5": {"option_id": "5","title": "Trīsvietīgs","long_title": "","child_count": "0","adults": "3","gta_code": "TR"},"9": {"option_id": "9","title": "Divvietīgs ar bērnu","long_title": "","child_count": "1","adults": "2","gta_code": "DB"},"54": {"option_id": "54","title": "Četrvietīgs","long_title": "","child_count": "0","adults": "4","gta_code": "Q"},"59": {"option_id": "59","title": "Divvietīgs vienam cilvēkam","long_title": "","child_count": "0","adults": "1","gta_code": "TS"},"63": {"option_id": "63","title": "Divvietīgs (ar atsevišķam gultām un bērnu)","long_title": "","child_count": "1","adults": "2","gta_code": "TB"},"62": {"option_id": "62","title": "Divvietīgs (ar atsevišķam gultām)","long_title": "","child_count": "0","adults": "2","gta_code": "TB"}};

	var $has_children = false;
	var $room_type = $(this).val();
	for (var $i in $room_types) {
		if ($room_types[$i].option_id == $room_type) {
			$has_children = $room_types[$i].child_count > 0;
			break;
		}
	}

	var $child_age = $("select[name='" + $(this).attr('name').replace('room_type', 'child_age') + "']");
	if ($has_children) {
		$child_age.show();
	}
	else {
		$child_age.val('').hide();
	}

	var $prev_title = this.getAttribute('title');
	var $new_title = $('option:selected:first', this).get(0).getAttribute('title');

	if ($new_title != $prev_title) {
		this.setAttribute('title', $new_title);
	}
}

function onRoomCountChange ($e) {
	var $new_room_count = $(this).val();

	if ($new_room_count == $room_count) {
		return ;
	}

	var $i = parseInt($room_count);
	var $row_mask = $('#room_element_mask').val();

	if ($new_room_count > $room_count) {
		// add rows
		while ($i < $new_room_count) {
			var $row = $row_mask.replace(/#ROOM_NUMBER#/g, $i + 1);
			$('#rooms').append($row);

			// bind to newly added elements
			$('select.room-details-type', '#room_row_' + ($i + 1))
			.click( function($e) { onRoomTypeChange.call(this, $e); } )
			.change( function($e) { onRoomTypeChange.call(this, $e); } );

			$i++;
		}
	}
	else {
		// remove rows
		while ($i > $new_room_count) {
			$('#room_row_' + $i).remove();
			$i--;
		}
	}

	if ( $new_room_count > 0) {
		$("#rooms_table").show();
	}
	else {
		$("#rooms_table").hide();
	}

	$room_count = $new_room_count;

	// hide "show_all_room_types" checkbox when room types are specified
	/*if ($room_count > 0) {
		$('#show_all_room_types_row').addClass('hidden-row');
	}
	else {
		$('#show_all_room_types_row').removeClass('hidden-row');
	}*/
}

$(document).ready(
	function() {
		if($.browser.msie) {
			$('#advanced_search').css('padding-left', '22px');
		}
		$('#advanced_search, #advanced_search_container legend').click(
			function ($e) {
				if (this.id == 'advanced_search') {
					$(this).hide();
				}
				$('#advanced_search_container').slideToggle(
					'slow',
					function() {
						$('#is_advanced_search').val( $('#advanced_search_container').is(':visible') ? 1 : 0 );
						if (!$('#advanced_search_container').is(':visible')) {
							$('#advanced_search').show();
						}
					}
				);
			}
		);

		$('#room_count')
		.click( function($e) { onRoomCountChange.call(this, $e); } )
		.change( function($e) { onRoomCountChange.call(this, $e); } );

		// bind for existing elements
		$('select.room-details-type')
		.click( function($e) { onRoomTypeChange.call(this, $e); } )
		.change( function($e) { onRoomTypeChange.call(this, $e); } );

		// mark hidden fields, on checkbox clicks (, #_cb_show_all_room_types)
		$('#_cb_all_inclusive_only, #_cb_only_available_hotels').click(
			function ($e) {
				var $id = $(this).attr('id').replace(/^_cb_/, '');

				$('#' + $id).val( $(this).is(':checked') ? 1 : 0 );
			}
		);

		$('#hotel_name')
		.focus(
			function ($e) {
				selectRange(this, 0, $(this).val().length);
			}
		)
		.click(
			function ($e) {
				selectRange(this, 0, $(this).val().length);
			}
		);
	}
);