$(function()
{
	//imposto la visibilità nulla x il menu dx
	$("#menuibiza").css('display', 'none');

	
	//date picker
	
	$('.start-date').datePicker({
		createButton		: false,
		inline				: true
							}).val(new Date().asString()).trigger('change');
	
	$('.end-date').datePicker({
		createButton		: false,
		inline				: true
							}).val(new Date().asString()).trigger('change');

	// listen for when the selects are changed and update the picker
	$('#d, #m, #y')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#y').val(),
							$('#m').val()-1,
							$('#d').val()
						);
				$('.start-date').dpClose();						
				$('.start-date').dpSetSelected(d.asString());
				$('.start-date').dpDisplay();
			}
		);
	// listen for when the selects are changed and update the picker
	$('#dend, #mend, #yend')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#yend').val(),
							$('#mend').val()-1,
							$('#dend').val()
						);
				$('.end-date').dpClose();						
				$('.end-date').dpSetSelected(d.asString());
				$('.end-date').dpDisplay();
			}
		);		
	$('.start-date').bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelectsstart(selectedDate);
			}
		)
	$('.end-date').bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelectsend(selectedDate);
			}
		)		
	var updateSelectsstart = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#d')[0]).selectedIndex = d - 1;
		($('#m')[0]).selectedIndex = m;
		($('#y')[0]).selectedIndex = y - 2008;
	}
	var updateSelectsend = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#dend')[0]).selectedIndex = d - 1;
		($('#mend')[0]).selectedIndex = m;
		($('#yend')[0]).selectedIndex = y - 2008;
	}	
	// default the position of the selects to today
	var today = new Date();
	($('#d')[0]).selectedIndex = today.getDate() - 1;
	($('#m')[0]).selectedIndex = today.getMonth();
	($('#y')[0]).selectedIndex = today.getFullYear() - 2008;
	($('#dend')[0]).selectedIndex = today.getDate() - 1;
	($('#mend')[0]).selectedIndex = today.getMonth();
	($('#yend')[0]).selectedIndex = today.getFullYear() - 2008;
	// and update the datePicker to reflect it...
	$('#d').trigger('change');		
	$('#dend').trigger('change');		
});
