var curDate = new Date();

$(document).ready(function()
{
	$("a[rel='photos']").colorbox();
	$("#webcam").colorbox({width:"736px", height:"440px", iframe:true});
	$("#header_button").colorbox({width:"940px", height:"500px", iframe:true});
});

$(function()
{		
	// initialise the "Select date" link
	$('#arrival-date-pick')
		.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]);
				$(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)
	{
		var selectedDate = new Date(selectedDate);
		
		ad = selectedDate.getDate();
		am = selectedDate.getMonth()+1;
		dd = selectedDate.getDate()+1;
		dm = selectedDate.getMonth()+1;
		
		var days = daysInMonth(dm);
			
		if (dd > days)
		{
			dd = 1;
			dm = am+1;
	
			if (dm > 12)
			{
				dm = 1;
			}
		}

		$('#ad option[value=' + ad + ']').attr('selected', 'selected');
		$('#am option[value=' + am + ']').attr('selected', 'selected');
		$('#dd option[value=' + dd + ']').attr('selected', 'selected');
		$('#dm option[value=' + dm + ']').attr('selected', 'selected');
	}
	
	// listen for when the selects are changed and update the picker
	$('#ad, #am')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#am').val()-1,
							$('#ad').val()
						);
				$('#arrival-date-pick').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());
	
	// and update the datePicker to reflect it...
	$('#ad').trigger('change');
});

$(function()
{		
	// initialise the "Select date" link
	$('#departure-date-pick')
		.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]);
				$(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)
	{
		var selectedDate = new Date(selectedDate);
		
		dd = selectedDate.getDate()+1;
		dm = selectedDate.getMonth()+1;
		
		var days = daysInMonth(dm);
			
		if (dd > days)
		{
			dd = 1;
			dm = am+1;
	
			if (dm > 12)
			{
				dm = 1;
			}
		}

		$('#dd option[value=' + dd + ']').attr('selected', 'selected');
		$('#dm option[value=' + dm + ']').attr('selected', 'selected');
	}
	
	// listen for when the selects are changed and update the picker
	$('#dd, #dm')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#dm').val()-1,
							$('#dd').val()
						);
				$('#departure-date-pick').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());
	
	// and update the datePicker to reflect it...
	$('#dd').trigger('change');
});

$(function()
{		
	$('#ad,#am').change(function()
	{
		m = parseInt($('#am').val());
		d = parseInt($('#ad').val());

		if (! m == 0 && ! d == 0)
		{
			var days = daysInMonth(m);
			
			if (d > days)
			{
				$('#ad option[value=' + days + ']').attr('selected', 'selected');
			}	
			
			nd = d+1;
			nm = m;
		
			if (nd > days)
			{
				nd = 1;
				nm = m+1;
		
				if (nm > 12)
				{
					nm = 1;
				}
			}

			$('#dd option[value=' + nd + ']').attr('selected', 'selected');
			$('#dm option[value=' + nm + ']').attr('selected', 'selected');
		}
	});
	
	$('#dd,#dm').change(function()
	{
		m = parseInt($('#dm').val());
		d = parseInt($('#dd').val());

		if (! m == 0 && ! d == 0)
		{
			var days = daysInMonth(m);
			
			if (d > days)
			{
				$('#dd option[value=' + days + ']').attr('selected', 'selected');
			}	
		}
	}); 
});

function daysInMonth(mon)
{           
	if (mon == 2)
	{
		Year = curDate.getFullYear();
		
		if(Math.round(Year/4) == Year/4)
		{
			if(Math.round(Year/100) == Year/100)
			{
				if(Math.round(Year/400) == Year/400)
					return 29;
				
				else 
					return 28;
			}
			
			else
				return 29;
		}
		
		return 28;
	}
	else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)
	{
		return 30;
	}
	
	return 31;
}

$(document).ready(function()
{
	$('#check').click(function()
	{
	  $("<div id=\"hoteliers\"><iframe name=\"hoteliersIframe\" src=\"\" width=\"890\" height=\"420\" frameborder=\"0\"></div>").appendTo("div#wrapper");
	  
	  $('#reservation_form').submit(function()
	  {
	  	$.fn.colorbox({width:"940px", height:"500px", href:"#hoteliers", open:true, inline: true, scrolling: false, onLoad:function(){$("#hoteliers").show();},onClosed:function(){$("#hoteliers").remove();}});
	 	});
	});
});

$(document).ready(function()
{
  $("#subscribe").click(function ()
  {
  	lan = $('#lang').val();

		if (lan == 'en')
		{
			def = 'E-mail address';
			text = 'Subscription received.';
		}
		
		else if (lan == 'de')
		{
			def = 'E-mail Adresse';
			text = 'Anmeldung in Ordnung.';
		}
		
		else if (lan == 'fr')
		{
			def = 'Adresse email';
			text = 'R&eacute;gistration effectu&eacute;e.';
		}
		
		else
		{
			def = 'E-mail adres'
			text = 'Aanmelding ontvangen.';
		}
		
  	$("<div id=\"confirmation\">"+text+"</div>").appendTo("div#wrapper"); 

    $.post("/subscribe.php",
    $("#newsletter").serialize(),
    function(data){
    	$.fn.colorbox({
    		width: "240px", 
    		height: "120px", 
    		href:"#confirmation", 
    		open:true,
    		scrolling: false,
    		inline: true, 
    		onLoad:function(){$("#confirmation").show();},
    		onClosed:function(){$("#confirmation").remove();$("#email").val(def);}
    	});
    },
    "html");
  });
});

$(document).ready(function()
{
	$("#email").focus(function()
	{
		if( this.value == this.defaultValue )
		{
			this.value = "";
		}
		}).blur(function()
		{
			if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
}); 

(function() {
  var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  po.src = 'https://apis.google.com/js/plusone.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
