function book_date_pickers()
{
	$('#book-arrival-date-pick')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				updateSelectsStart($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelectsStart(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelectsStart(selected[0]);
			}
		);
		
	$('#book-departure-date-pick')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				updateSelectsEnd($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelectsEnd(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelectsEnd(selected[0]);
			}
		);
		
	var updateSelectsStart = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#aday')[0]).selectedIndex = d - 1;
		($('#amonth')[0]).selectedIndex = m;
		($('#ayear')[0]).selectedIndex = y - today.getFullYear();
	}
	
	var updateSelectsEnd = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		
		($('#dday')[0]).selectedIndex = d - 1;
		($('#dmonth')[0]).selectedIndex = m;
		($('#dyear')[0]).selectedIndex = y - today.getFullYear();
	}
	
	$('#aday, #amonth, #ayear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#ayear').val(),
							$('#amonth').val()-1,
							$('#aday').val()
						);
				$('#book-arrival-date-pick').dpSetSelected(d.asString());
			}
		);
	
	$('#dday, #dmonth, #dyear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#dyear').val(),
							$('#dmonth').val()-1,
							$('#dday').val()
						);
				$('#book-departure-date-pick').dpSetSelected(d.asString());
			}
		);
	var today = new Date();
	if($('#aday')[0] && $('#amonth')[0] && $('#ayear')[0] && $('#dday')[0] && $('#dmonth')[0] && $('#dyear')[0])
	{
		if($('#aday').val()!="" && $("#amonth").val()!="" && $("#ayear").val()!="")
		{
			today = new Date(
				$('#ayear').val(),
				$('#amonth').val()-1,
				$('#aday').val()
			);
		}
		
		($('#aday')[0]).selectedIndex = today.getDate() - 1;
		($('#amonth')[0]).selectedIndex = today.getMonth();
		($('#ayear')[0]).selectedIndex = today.getFullYear() - today.getFullYear();
		
		if($('#dday').val()!="" && $("#dmonth").val()!="" && $("#dyear").val()!="")
		{
			today = new Date(
				$('#dyear').val(),
				$('#dmonth').val()-1,
				$('#dday').val()
			);
		}
		
		($('#dday')[0]).selectedIndex = today.getDate() - 1;
		($('#dmonth')[0]).selectedIndex = today.getMonth();
		($('#dyear')[0]).selectedIndex = today.getFullYear() - today.getFullYear();
		
		// and update the datePicker to reflect it...
		$('#aday').trigger('change');
		$('#dday').trigger('change');
	}
}

$(document).ready(
function(){
	$('#details_gallery a').lightBox({fixedNavigation:true, show_helper_text:true});
	$('#villa_layout a').lightBox({fixedNavigation:true, show_helper_text:true});
	$("#details_map").vrs4_gmap('init',
	{
		"map_center":[$("#lat").val(),$("#long").val()],
		"map_zoom":$("#zoom").val()
	}
	);
	/*
	$("#details_map").vrs4_gmap(
		'move_to',
		{
			'map_center':[$("#lat").val(),$("#long").val()],
			'map_zoom' : $("#zoom").val()+0
		}
	);*/
	if(typeof $("#details_map")=="object")
	{
		var ico = new GIcon(G_DEFAULT_ICON);
		ico.image = site_domain+"public/images/gmap_marker.png";
		ico.shadow = site_domain+"public/images/gmap_marker_shadow.png";
		var map = gmaps_class.maps.get($("#details_map"));
		var point = new GLatLng($("#lat").val(),$("#long").val());
		marker = new GMarker(point,{draggable:false, autoPan:false, icon:ico});
		map.addOverlay(marker);
	}
	$(".scrollable").scrollable({});
	$("#print_page").click(
		function(){ window.print();}
	);
	$("#send_to_a_friend").facebox();
	book_date_pickers();
});
