$(function()
{
	// initialise the "Select date" link
	$('#pick-date')
		.datePicker(
			{
				createButton:false,
				startDate:'01/01/1995',
				endDate:'12/31/2025'
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);		
	var updateSelects = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#d0')[0]).selectedIndex = d - 1;
		($('#m0')[0]).selectedIndex = m;
		($('#y0')[0]).selectedIndex = y - 1995;
	}
	// listen for when the selects are changed and update the picker
	$('#d0, #m0, #y0')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#y0').val(),
							$('#m0').val()-1,
							$('#d0').val()
						);
				$('#pick-date').dpSetSelected(d.asString());
			}
		);
	// default the position of the selects to today
	// var today = new Date();
	// ($('#d0')[0]).selectedIndex = today.getDate() - 1;
	// ($('#m0')[0]).selectedIndex = today.getMonth();
	// ($('#y0')[0]).selectedIndex = today.getFullYear() - 1995;
	// and update the datePicker to reflect it...
	$('#d0').trigger('change');
});
