/*
Custom js to make the date select boxes work correctly.
 */
$(document).ready(function(){
		Shadowbox.init({
		// skip the automatic setup again, we do this later manually
		skipSetup: true
		});											 
		// set up Shadowbox for main page
		Shadowbox.setup("a.dive_video", {
		autoplayMovies:true,	
		overlayOpacity:0.8
		});		
		// set up Shadowbox for photo gallery
		Shadowbox.setup("a.gallery", {
		overlayOpacity: 	0.8	,		
		gallery:            "Photo Gallery",
		continuous: true,
		slideshowDelay: 5
		});
	
	});

	$(function()
	{
		$('#start-date').datePicker(
			// associate the link with a date picker
			{
				createButton:false
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{	
				updateSelects($(this).dpGetSelected()[0]);
				//updateSelects2($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
				//updateSelects2(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#end-date').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#start-date').dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
		var updateSelects = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		/*
		var updateSelects2 = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d2 option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m2 option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y2 option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		*/
		// listen for when the start-date 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').dpSetSelected(d.asString());
				}
			);
		// START END DATE CODE
		$('#end-date').datePicker(
			// associate the link with a date picker
			{
				createButton:false
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelects3($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects3(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#start-date').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
		$('#end-date').dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
		var updateSelects3 = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d2 option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m2 option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y2 option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		// listen for when the end-date selects are changed and update the picker
		$('#d2, #m2, #y2')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#y2').val(),
								$('#m2').val()-1,
								$('#d2').val()
							);
					$('#end-date').dpSetSelected(d.asString());
				}
			);
		
		
		// default the position of the selects to today
		var today = new Date();
		updateSelects(today.getTime());
		
		var futdate = new Date();
		var expdate = futdate.getTime();
		expdate += 3600*1000*24*90 //this is milliseconds * 1000 (1 hour) * 24 (24 hours) * 90 days
		futdate.setTime(expdate)
		updateSelects3(today.setTime(expdate));	
		
	




		// SAFARIS DATE SEARCH CODE BEGIN
		
		$('#start-date2').datePicker(
			// associate the link with a date picker
			{
				createButton:false
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{	
				updateSelects10($(this).dpGetSelected()[0]);
				//updateSelects20($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects10(selectedDate);
				//updateSelects20(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#end-date').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		var updateSelects10 = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d3 option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m3 option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y3 option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		/*
		var updateSelects20 = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d4 option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m4 option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y4 option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		*/
		// listen for when the start-date2 selects are changed and update the picker
		$('#d3, #m3, #y3')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#y3').val(),
								$('#m3').val()-1,
								$('#d3').val()
							);
					$('#start-date2').dpSetSelected(d.asString());
				}
			);
		// START END DATE CODE
		$('#end-date2').datePicker(
			// associate the link with a date picker
			{
				createButton:false
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelects30($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects30(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#start-date2').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
		var updateSelects30 = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#d4 option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#m4 option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#y4 option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		// listen for when the end-date2 selects are changed and update the picker
		$('#d4, #m4, #y4')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#y4').val(),
								$('#m4').val()-1,
								$('#d4').val()
							);
					$('#end-date2').dpSetSelected(d.asString());
				}
			);
		
		
		// default the position of the selects to today
		var today2 = new Date();
		updateSelects10(today2.getTime());		
		var futdate2 = new Date();
		var expdate2 = futdate2.getTime();
		expdate2 += 3600*1000*24*90 //this is milliseconds * 1000 (1 hour) * 24 (24 hours) * 90 days
		futdate2.setTime(expdate2)
		updateSelects30(today2.setTime(expdate2));	
		
		
		//RESORT DATE PICKER BEGIN
		// initialise the "Select date" link
		$('#resort-date-pick')
			.datePicker(
				// associate the link with a date picker
				{
					createButton:false
				}
			).bind(
				// when the link is clicked display the date picker
				'click',
				function()
				{
					updateSelectsResort($(this).dpGetSelected()[0]);
					$(this).dpDisplay();
					return false;
				}
			).bind(
				// when a date is selected update the SELECTs
				'dateSelected',
				function(e, selectedDate, $td, state)
				{
					updateSelectsResort(selectedDate);
				}
			).bind(
				'dpClosed',
				function(e, selected)
				{
					updateSelectsResort(selected[0]);
				}
			);
			
		var updateSelectsResort = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#dr option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#mr option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#yr option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		// listen for when the selects are changed and update the picker
		$('#dr, #mr, #yr')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#yr').val(),
								$('#mr').val()-1,
								$('#dr').val()
							);
					$('#resort-date-pick').dpSetSelected(d.asString());
				}
			);
		// default the position of the selects to today
		var today = new Date();
		updateSelectsResort(today.getTime());
		// and update the datePicker to reflect it...
		$('#d').trigger('change');
				
	});	
