jQuery(document).ready(function(){
	jQuery('.roomsSelection').change(function(){ 
		updateSelectElements(1); 
	});
	
	updateSelectElements(1); 
});

/* Search Seclect Start */
function updateSelectElements(hotel_id){
	var numRooms = jQuery('#rooms'+hotel_id).val();
	var table = jQuery('.selections_'+hotel_id);
	
	var exTRs = table.find('div.selection_tr').length;

	if(numRooms > exTRs){
		var diff = numRooms - exTRs;

		for (var j = 0; j < diff; j++) {
			var i = j + exTRs + 1;
			var TR = jQuery('<div>').addClass('tr_' + i).addClass('selection_tr');
			if(numRooms == 1){
				jQuery('<div>').addClass('td_' + i + '_0').html('Room 1: ').addClass('roomNum').css('width','90px').css('display','none').appendTo(TR);
			}else{
				jQuery('.td_1_0').css('display','block');
				jQuery('<div>').addClass('td_' + i + '_0').addClass('roomNum').html('Room '+i+': ').css('width','90px').appendTo(TR);
			}
			 
			jQuery('<div>').addClass('td_' + i + '_1').css('width','90px').append(selectAdultElemnt('adult_' + i)).appendTo(TR);
			jQuery('<div>').addClass('td_' + i + '_2').css('width','90px').append(selectChildElemnt('child_' + i, hotel_id)).appendTo(TR);
			jQuery('<div>').addClass('td_' + i + '_3').css('width','90px').appendTo(TR);
			jQuery('<div>').addClass('td_' + i + '_4').css('width','90px').appendTo(TR);
			jQuery('<div>').addClass('td_' + i + '_5').css('width','90px').appendTo(TR);
			TR.appendTo(table);
		}
	}else{
		if(numRooms == 1){
			jQuery('.td_1_0').css('display','none');
		}
					
		for(var i = exTRs;i > numRooms ; i--){
			jQuery('div.tr_'+i).remove();
		}
	}
	

}

function updateAgeLabels(value, hotel_id){
	var cont = jQuery('.selections_'+hotel_id);
	
	jQuery('.selections_'+hotel_id).find('.age_label_1').html(' ');
	jQuery('.selections_'+hotel_id).find('.age_label_2').html(' ');
	jQuery('.selections_'+hotel_id).find('.age_label_3').html(' ');
	
	var max = 0;
	for (var j = 1; j <= 3; j++) {
		if(cont.find('#child_'+j).val() > max){
			max = cont.find('#child_'+j).val();
		}
	}
	
	for(var i = max; i >= 1; i--){
		jQuery('.selections_'+hotel_id).find('.age_label_'+i).html('Age');
	}
}

function updateChildSelection(ele,hotel_id){
	var index = ele.attr('id').replace('child_',''); 

	if(ele.val() == 0){
		updateAgeLabels(ele.val(), hotel_id);
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_3').html(' ');
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_4').html(' ');
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_5').html(' ');
	}else if(ele.val() == 1){
		updateAgeLabels(ele.val(), hotel_id);
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_3').html(selectAgeElemnt('child_'+index+'_1',1));
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_4').html(' ');
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_5').html(' ');
	}else if(ele.val() == 2){
		updateAgeLabels(ele.val(), hotel_id);
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_3').html(selectAgeElemnt('child_'+index+'_1',1));
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_4').html(selectAgeElemnt('child_'+index+'_2',2));
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_5').html(' ');
	}else{
		updateAgeLabels(ele.val(), hotel_id);		
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_3').html(selectAgeElemnt('child_'+index+'_1',1));
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_4').html(selectAgeElemnt('child_'+index+'_2',2));	
		jQuery('.selections_'+hotel_id).find('.td_' + index + '_5').html(selectAgeElemnt('child_'+index+'_3',3));
	}
}

function selectAdultElemnt(ele_name){
	var selectEle = jQuery('<select>').attr('name','adults[]').attr('id',ele_name).addClass(ele_name);
								  
	for(var i = 1; i < 7; i++){
		jQuery('<option>').attr('value',i).html(i).appendTo(selectEle);	
	}		

	return selectEle;	
}

function selectChildElemnt(ele_name, hotel_id){
	var selectEle = jQuery('<select>').attr('name','children[]').attr('id',ele_name).change(function(){ updateChildSelection(jQuery(this),hotel_id) }).addClass(ele_name);
									  
	for(var i = 0; i < 4; i++){
		jQuery('<option>').attr('value',i).html(i).appendTo(selectEle);	
	}		
	
	return selectEle;	
}

function selectAgeElemnt(ele_name,index){ 
	var selectEle = jQuery('<select>').attr('name','children_'+index+'[]').attr('id',ele_name).addClass(ele_name);
									  
	for(var i = 0; i < 18; i++){
		var value = (i == 0)? '< 1': i;
		jQuery('<option>').attr('value',i).html(value).appendTo(selectEle);	
	}		
	
	return selectEle;	
}
/* Search Select End */
