$(function () {

	/* room config */
	$('#room-1 td:first').data('text', $('#room-1 td:first').text());
	if ($('#rooms').val() == 1)
		$('#room-1 td:first').text('');
	
	$('#rooms').change(function () {
		var roomCount = $(this).val(), clone, roomRow = $('#room-config tbody tr:first');

		/* hide 'Room 1:' label if only one room */
		if (roomCount == 1) {
			$('#room-1 td:first').text('');
		} else {
			$('#room-1 td:first').text($('#room-1 td:first').data('text'));	
		}

		if ($('#room-config tbody tr').length > roomCount) {
			/* remove rooms (table rows) */
			$('#room-config tbody tr:gt(' + ($(this).val() - 1) + ')').each(function () {
				
				/* remove children */
				var roomNumber = $(this).attr('id').substring(5);
				$('#childrow-' + roomNumber).remove();
				
				$(this).remove();
			});
			
			$(window).triggerHandler('childChange');
		
		} else {
			/* add rooms */
			
			var originalCount = $('#room-config tbody tr').length;
			for (var i = originalCount; i < roomCount; i++) {
				clone = roomRow.clone().attr('id', 'room-' + (i + 1));
				label = clone.children('td:first');
				label.text(label.text().replace(/[\d]+/, (i + 1)));
				clone.find('select.child-count').val(0);
				clone.find('select:first').val(2);
				clone.appendTo($('#room-config tbody'));
			}
			$(window).triggerHandler('roomChange', {originalCount: originalCount});
		}
		
	});

	
	$(window).bind('roomChange', function (event, data) {
		
		/* child config */
		$('#room-config tbody tr' + (data.originalCount > 0 ? ':gt(' + (data.originalCount - 1) + ')' : '') +' select.child-count').change(function () {
			var roomNumber = $(this).closest('tr').attr('id').substring(5), children = $(this).val();
			
			/* add child row */
			if ($('#childrow-' + roomNumber).length == 0) {
				var roomIndex = $(this).closest('tr').prevAll('tr:has(select.child-count[value!=0])').length;
				
				clone = $('#child-config tfoot tr').clone();
				clone.attr('id', 'childrow-' + roomNumber);
				clone.find('select').attr('name', 'child-age['+(roomNumber-1)+'][]');
				label = clone.children('td:first');
				label.text(label.text().replace(/[\d]+/, roomNumber));

				if (roomIndex == 0)
					clone.prependTo($('#child-config tbody'));
				else
					clone.insertAfter($('#child-config tbody tr:eq(' + (roomIndex - 1) + ')'));
			}
			
		
			var row = $('#childrow-' + roomNumber ), childrenLength = row.children('td').length - 1;
			
			if (childrenLength > children) {
				/* remove child ages (cells) */
				if (children == 0)
					row.remove();
				else
					row.children('td:gt(' + children + ')').remove();
				
			} else {
				/* add child ages */
				var childAge = $('#child-config tfoot tr td:has(select)');
				for (var i = childrenLength; i < children; i++) {
					clone = childAge.clone();
					clone.find('select').attr('name', 'child-age['+(roomNumber-1)+'][]');
					clone.appendTo(row);
				}
			}
			
			$(window).triggerHandler('childChange');
			
		});
	});

	$(window).bind('childChange', function () {

		/* fix column headers */
		var maxChildrenLength = 0, currentLength;
		$('#child-config tbody tr').each(function () {
			currentLength = $(this).children('td').length;
			if (currentLength > maxChildrenLength)
				maxChildrenLength = currentLength;
		
		});
		
		if (maxChildrenLength == 0)
			$('#child-config').hide();
		else
			$('#child-config').show();

		var columnCount = $('#child-config thead th').length;

		if (columnCount > maxChildrenLength) {
			/* remove headers */
			if (maxChildrenLength > 0)
				$('#child-config thead th:gt(' + (maxChildrenLength -1 ) + ')').remove();
			
		} else {
			/* add headers */
			var headerCell = $('#child-config thead tr th:eq(1)');
			for (var i = columnCount; i < maxChildrenLength; i++) {
				clone = headerCell.clone();	
				clone.text(clone.text().replace(/[\d]+/, i));
				clone.appendTo( $('#child-config thead tr'));
			}
		}
		
	});

	$(window).triggerHandler('roomChange', {originalCount: 0});

});