/*
 * Rooms & Children
 *
 */
var currentValue = 1;
//ROOM
function Rooms(no){
	//人数
	var people = 8;
	
	$('table.room tbody').append('<tr id="r'+no+'" class="RoomNum"></tr>');
	$('table.room tbody tr#r'+no).append('<th class="alignright">Room:'+no+'</th> ');

	$('table.room tbody tr#r'+no).append('<td><select id="Adults'+no+'" name="">');
	for(ai=0; ai<=people; ai++){
		$('table.room tbody tr#r'+no+' td select#Adults'+no).append('<option value='+ai+'>'+ai+'</option>');
	}
	$('table.room tbody tr#r'+no).append('</select></td>');
	
	
	$('table.room tbody tr#r'+no).append('<td><select id="Children'+no+'" name="">');
	for(ci=0; ci<=people; ci++){
		$('table.room tbody tr#r'+no+' td select#Children'+no).append('<option value='+ci+'>'+ci+'</option>');
	}
	$('table.room tbody tr#r'+no).append('</select></td>');
	
	setChildChangeFunction($('table.room tbody tr#r'+no+' td select#Children'+no) , no);
	
}

function setChildChangeFunction(obj , no) {
	$(obj).change(function(){
		var ChildVal = $(this).children("option:selected").val();
		
		if(ChildVal > 0){
			//ChildCount = ChildVal - ChildValue;
			var ChildCount = 0;
			var label = 0;
			var ChildSet = 0;
			if(!$('div.children-age table#'+no).text()){
				label = 1;
				ChildCount = ChildVal;
			}else{
				ChildSet = $('div.children-age table#'+no+' tbody tr td p').size();
				ChildCount = ChildVal - ChildSet;
			}
			Children(ChildCount , no , label , Number(ChildSet));
				
			if(ChildCount < 0){
				minusNum = ChildSet - ChildVal;
				for(i=0; i<minusNum; i++){
					$('div.children-age div#room'+no+'-area table  tbody tr td  p.Peple'+(Number(ChildSet)-i)).remove();
				}
			}
		}
		if(ChildVal == 0){
			$('div.children-age div#room'+no+'-area table').remove();
		}
		ChildSet = ChildVal;
		
	});
}


//Children
function Children(ChildCount, no , label , ChildValue){
	//何歳
	var Age = 17;
	
	if(label == 1){
		$('div.children-age div#room'+no+'-area').append('<table cellpadding="0" cellspacing="0" border="0" id="'+no+'"><thead></thead></table>');
		$('div.children-age table#'+no+' thead').append('<tr><th><label>Room '+no+' Child Ages:</label></th></tr><tr><th colspan="2">Ages (0-17) at time of travel:</th></tr>');
		$('div.children-age table#'+no).append('<tbody><tr><td></td></tr></tbody>');
	}
	
	for(i=1; i<=ChildCount; i++){
		$('div.children-age table#'+no+' tbody tr td').append('<p class="Peple'+(ChildValue+i)+'"> '+(ChildValue+i)+': <select name="">');
		$('div.children-age table#'+no+' tr td p.Peple'+(ChildValue+i)+' select').append('<option value="?">?</option>');
		for(a=0; a<=Age; a++){
			$('div.children-age table#'+no+' tr td p.Peple'+(ChildValue+i)+' select').append('<option value="'+a+'">'+a+'</option>');
		}
		$('div.children-age table#'+no+' tbody tr td').append('</select></p>');
	}
}

function RoomChildren(){
	$('#Room').change(function(){
		//Room数を取得
		var RoomNum = $(this).children("option:selected");
		
		
		var plusNum = RoomNum.val() - currentValue;
		if(plusNum>0){
			for(i=0; i<plusNum; i++){
				Rooms(Number(currentValue)+Number(i)+1);
			}
		}
		if(Number(plusNum)<0){
			var minusNum = currentValue - RoomNum.val();
			for(i=0; i<minusNum; i++){
				$('table.room tbody tr#r'+(Number(currentValue)-Number(i))).remove();
			}
		}
		currentValue = RoomNum.val();
	});
}
$(document).ready(function(){
	RoomChildren();
	setChildChangeFunction($('table.room tbody tr#r1 td select#Children1') , 1);
});