$(function()
{
	
	$('#calender_anreise')
		.datePicker(
			{
				createButton:false,
				/*startDate:'01/04/2010',*/
				endDate:'31/12/2015'
			}
		).bind(
			'click',
			function()
			{
				updateSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);
		
	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		
		$('#startDay option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#startMonth option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
		$('#startYear option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
	}
	// listen for when the selects are changed and update the picker
	$('#startDay, #startMonth, #startYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#startDay').val(),
							$('#startMonth').val()-1,
							$('#startYear').val()
						);
				$('#calender_anreise').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());
	
	// and update the datePicker to reflect it...
	$('#startDay').trigger('change');


	$('#calender_abreise')
		.datePicker(
			{
				createButton:false,
				endDate:'31/12/2015'
			}
		).bind(
			'click',
			function()
			{
				updateSelects2($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects2(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects2(selected[0]);
			}
		);
	
	var updateSelects2 = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		$('#departDay option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#departMonth option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
		$('#departYear option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
	}
	
	// listen for when the selects are changed and update the picker
	$('#departDay, #departMonth, #departYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#departDay').val(),
							$('#departMonth').val()-1,
							$('#departYear').val()
						);
				$('#calender_abreise').dpSetSelected(d.asString());
			}
		);
		
		// default the position of the selects to today
	var tomorrow = new Date();
	tomorrow.setDate(tomorrow.getDate()+1);
	updateSelects2(tomorrow.getTime());
	
	// and update the datePicker to reflect it...
	$('#departDay').trigger('change');

});
