// JavaScript Document
var $jz = jQuery.noConflict();

function addDays(myDate,days) {
	return new Date(myDate.getTime() + days*24*60*60*1000);
}

$jz(function()
			{				
				// initialise the "Select date" link
				$jz('#date-pick')
					.datePicker(
						// associate the link with a date picker
						{
							createButton:false,
							startDate:'01/01/2011',
							endDate:'01/01/2020'
						}
					).bind(
						// when the link is clicked display the date picker
						'click',
						function()
						{
							updateSelects($jz(this).dpGetSelected()[0]);							
							$jz(this).dpDisplay();							
							return false;
						}
					).bind(
						// when a date is selected update the SELECTs
						'dateSelected',
						function(e, selectedDate, $td, state)
						{
							updateSelects(selectedDate);
							//kvantanet
							updateSelects2(addDays(selectedDate,1));							
							$jz('#date-pick2').dpSetSelected(addDays(selectedDate,1).asString());							
							//
						}
					).bind(
						'dpClosed',
						function(e, selected)
						{
							updateSelects(selected[0]);	
							//kvantanet
							updateSelects2(addDays(selected[0],1));
							//
						}
					);
					
				// initialise the "Select date" link
				$jz('#date-pick2')
					.datePicker(
						// associate the link with a date picker
						{
							createButton:false,
							startDate:'01/01/2011',
							endDate:'01/01/2020'
						}
					).bind(
						// when the link is clicked display the date picker
						'click',
						function()
						{
							updateSelects2($jz(this).dpGetSelected()[0]);
							$jz(this).dpDisplay();
							return false;
						}
					).bind(
						// when a date is selected update the SELECTs
						'dateSelected',
						function(e, selectedDate, $td, state)
						{
							updateSelects2(selectedDate);
						}
					).bind(
						'dpClosed',
						function(e, selected)
						{
							updateSelects2(selected[0]);
						}
					);
					
				var updateSelects = function (selectedDate)
				{
					var selectedDate = new Date(selectedDate);
					$jz('#check-in-d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
					$jz('#check-in-m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
					$jz('#check-in-y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
				}
				
				var updateSelects2 = function (selectedDate)
				{
					var selectedDate = new Date(selectedDate);
					$jz('#check-out-d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
					$jz('#check-out-m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
					$jz('#check-out-y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
				}
				
				// listen for when the selects are changed and update the picker
				$jz('#check-in-d, #check-in-m, #check-in-y')
					.bind('change',
						function()
						{
							var d = new Date(
										$jz('#check-in-y').val(),
										$jz('#check-in-m').val()-1,
										$jz('#check-in-d').val()
									);							
							
							$jz('#date-pick').dpSetSelected(d.asString());
							// kvantanet
							$jz('#date-pick2').dpSetSelected(addDays(d,1).asString());
							//
							
						}
					);
				
				// listen for when the selects are changed and update the picker
				$jz('#check-out-d, #check-out-m, #check-out-y')
					.bind('change',
						function()
						{
							var d = new Date(
										$jz('#check-out-y').val(),
										$jz('#check-out-m').val()-1,
										$jz('#check-out-d').val()
									);
							$jz('#date-pick2').dpSetSelected(d.asString());
						}
					);
				
				// default the position of the selects to today
				var today = new Date();								

				updateSelects(today.getTime());
				// kvantanet
				var tomorrow = addDays(today,1);
				updateSelects2(tomorrow.getTime());
				//
				
				// and update the datePicker to reflect it...
				$jz('#check-in-d').trigger('change');
				$jz('#check-out-d').trigger('change');
				
			});
