$(function()
{
	var todayTmp = new Date();
	var startDateTmp = '1/1/' + todayTmp.getFullYear();
	var endDateTmp = '31/12/' + (todayTmp.getFullYear()+1);

$('<option value="' + todayTmp.getFullYear() + '">' + todayTmp.getFullYear() + '</option>').appendTo(($('#aa')[0]))
$('<option value="' + (todayTmp.getFullYear()+1) + '">' + (todayTmp.getFullYear()+1) + '</option>').appendTo(($('#aa')[0]))

	//($('#aa')[0]).item.add('<option value="' + todayTmp.getFullYear() + '">' + todayTmp.getFullYear() + '</option>')
	//($('#aa')[0]).item.add('<option value="' + (todayTmp.getFullYear()+1) + '">' + (todayTmp.getFullYear()+1) + '</option>');


	// initialise the "Select date" link
	$('#date-pick')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:startDateTmp,
				endDate:endDateTmp
			}
		).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();
		($('#gg')[0]).selectedIndex = d - 1;
		($('#mm')[0]).selectedIndex = m;
		($('#aa')[0]).selectedIndex = y - todayTmp.getFullYear();
	}
	// listen for when the selects are changed and update the picker
	$('#gg, #mm, #aa')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#aa').val(),
							$('#mm').val()-1,
							$('#gg').val()
						);
				$('#date-pick').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	($('#gg')[0]).selectedIndex = today.getDate() - 1;
	($('#mm')[0]).selectedIndex = today.getMonth();
	($('#aa')[0]).selectedIndex = today.getFullYear() - todayTmp.getFullYear();
	
	// and update the datePicker to reflect it...
	$('#gg').trigger('change');



});
