(function($) {
	$.fn.dynamicBookingForm = function(options) {
		var container = $(this);
		var setprcontainer = $('input[name="pr"]', container.find('form'));
		
		if (setprcontainer.length > 0) {
			return;
		}
		var prcontainer = $('<input type="hidden" name="pr" value="" />').appendTo(container.find('form'));

		var roomSelector = $('#select_rooms', this);
		roomSelector.change(function() {
			$('#roomwrapper div.extra').remove();
			for (var i = 2; i <= $(this).val(); i++) {
				// Clone room and set new id	
				var room = $('#room1').clone(true);
				room.attr('id', 'room'+i);
				room.addClass('extra');
				
				// Clone selects/label and append them again and active uniform (required for uniform)
				var description = $('#description_1', room).clone(true);
				var select_adults = $('#select_adults_1', room).clone(true);
				var select_children = $('#select_children_1', room).clone(true);
				$(room).children('label').empty();			
				$(room).children('label').append(description);
				$(room).children('label').append(select_adults);
				$(room).children('label').append(select_children);
				
				// Clone selects/label and append them again and active uniform (required for uniform)
				var select_childage = $('#select_childage_1_1', room).clone(true);
				var label_childage = $('#childage_1_1', room).children('.description').clone(true);
				$('#childage_1_1', room).empty();
				$('#childage_1_1', room).append(label_childage);
				$('#childage_1_1', room).append(select_childage);
				
				// Set new id on selects and other elements
				$('#select_adults_1', room).attr('id', 'select_adults_' + i);
				$('#select_children_1', room).attr('id', 'select_children_' + i);
				$(select_childage).attr('id', 'select_childage_'+i+'_1');
				$('#childrenwrapper1', room).attr('id', 'childrenwrapper'+i);
				
				// Get and change Guest text, then change id
				var guest_text = $(description).html();
				guest_text = guest_text.substring(0, guest_text.length - 1);
				$(description).html(guest_text + i);
				$(description).attr('id', 'description_' + i);
					
				// Hide Childage select and change id and remove any possible extra selects	
				$('#childage_1_1', room).addClass('hidden');
				$('#childage_1_1', room).attr('id', 'childage_'+i+'_1');
				$(room).find('label.childrenlist').remove();
				
				// Finally add the new room to the wrapper												
				$('#roomwrapper').append(room);
			}
		}).change();
		
		$('select.children', this).change(function() {	
			var children = $(this).val();		
			var room = $(this).parents('.room');
			var roomid = room.attr('id').replace("room", "");
			$('#childrenwrapper'+roomid+' label.childrenlist').remove();

			if (children == 0) {
				$('#childage_'+roomid+'_1').addClass('hidden');
			} else if (children == 1) {
				$('#childage_'+roomid+'_1').removeClass('hidden');
			} else {
				$('#childage_'+roomid+'_1').removeClass('hidden');
				for (var i = 2; i <= children; i++) {
					// Clone childage and set id
					var childeage = $('#childage_'+roomid+'_1').clone(true);
					childeage.attr('id', 'childage_'+roomid+'_'+i);
					
					// Clone selects/label and append them again and active uniform (required for uniform)
					var select_childage = $('#select_childage_'+roomid+'_1', childeage).clone(true);
					var description = $(childeage).children('.description').clone(true);
					$(childeage).empty();
					$(childeage).append(description);
					$(childeage).append(select_childage);
					
					// Set id for select 
					$(select_childage).attr('id', 'select_childage_'+roomid+'_'+i);
										
					// Finally add class for childage (so it can be removed later) and chidlage to wrapper
					childeage.addClass('childrenlist');
					$('#childrenwrapper'+roomid).append(childeage);
				}
			}			
		}).change();
		
		var makePrString = function () {
			var str = "";
			var noRooms = roomSelector.val();

			for (var i = 1; i <= noRooms; i++) {
				if (str.length > 0) {
					str += "r";
				}
				str += $('#select_adults_' + i).val();
				
				var nrchildren = $('#select_children_' + i).val();

				if (nrchildren > 0) {
					str += "a";
				}
				for (var j = 1; j <= nrchildren; j++) {
					if (j > 1) {
						str += "c";
					}
					str += $('#select_childage_' + i + '_' + j).val();
				}
			}
			prcontainer.val(str);
		}
		
		$('select', this).change(makePrString).change();
	}
	
	$.fn.pairedDatePickers = function(fromId, toId) {
		options = {
				minDate: 1,
				defaultDate: 1,
				dateFormat: 'yy-mm-dd'
		};
		toId = "#date_"+toId;
		fromId = "#date_"+fromId;
		$(toId).datepicker(options);
		options.minDate = 0;
		options.defaultDate = 0;
		options.onSelect = function(dateText, inst) {
			d = $(this).datepicker('getDate');
			if (d) {
				ed = new Date(d.setDate(d.getDate()+1));
				if ($(toId).datepicker('getDate') <= d) {
					$(toId).datepicker('setDate', ed);
				}
				$(toId).datepicker('option', 'minDate', ed);
			}
		};
		$(fromId).datepicker(options);
	}

	$.fn.setDefaultDates = function (fromId, toId) {
		toId = "#date_" + toId;
                fromId = "#date_" + fromId;
		var today = Date.today().toString('yyyy-MM-dd');
		var tomorrow = Date.today().addDays(1).toString('yyyy-MM-dd');
		$(fromId).val(today);
		$(toId).val(tomorrow);
	}	
	
})(jQuery);	

$(function() {
	$('#bookingform').dynamicBookingForm();
	$('#bookingform').pairedDatePickers('fromDate', 'toDate');
	$('#bookingform').setDefaultDates('fromDate', 'toDate');		
});

