	var natDays = [
		[4, 8],
		[4, 9],
		[4, 30],
		[5, 17],
		[5, 27],
		[5, 28],
		[12, 25],
		[12, 26]
	];

$(document).ready(
	function()
	{
		bindAlgemeen();
        doFirstVisit();
        
		switch($("body").attr("id"))
		{
			case 'pageHome': 		bindPageHome();
									break;
			case 'pageReserveren':	bindPageReserveren();
									break;
			case 'pageBeheer':		bindPageBeheer();	
									break;
			case 'pageBezorgen':	bindPageBezorgen();
									break;
		}
        
        
	}
);

function bindAlgemeen()
{
	$("#blokken ul li a").hover(
		function()
		{
			$(this).find("p").slideDown("slow");
		},
		function()
		{
			$(this).find("p").slideUp("slow");			
		}
	);

	$("div#fotos img:not(:first)").hide();

	$("#ddrestaurant").hide();
	
	$(".showmodal").click(
			function()
			{
				showModal($(this).attr("href"));
				return false;
			}
	)
	imageRotate();
}

function imageRotate(iIndex)
{
	var aImages = $("div#fotos img");
	var iMax = aImages.length;

	if (iIndex == undefined)
	{
		iIndex = 0;
	}

	$("div#fotos img:eq("+iIndex+")").fadeOut(2000, function()
		{
			iIndex++;
			iIndex = iIndex >= iMax ? 0 : iIndex;

			$("div#fotos h3").text($("div#fotos img:eq("+iIndex+")").attr("alt"));
			$("div#fotos img:eq("+iIndex+")").fadeIn(1000, function()
				{
					setTimeout("imageRotate("+iIndex+")", 2500);
				}
			);
		}
	);


}

function bindPageHome()
{

}

function bindSoort()
{
	$("input[name=soort]").change(
		function()
		{
			$("#beschikbaarheid").html('');
			if ($(this).fieldValue() == 'restaurant')
			{
				$("#vergaderform").hide();
				$("#restform").show();
				$("select[name=aantalpersonen] option:eq(1)").show();
				$(".stappen.restaurant").show();
				$(".stappen.zaal").hide();
				$("h2.restaurant").show();
				$("h2.zaal").hide();
			}
			else
			{
				$("#vergaderform").show();
				$("#restform").hide();
				$("select[name=aantalpersonen] option:eq(1)").hide();
				$(".stappen.restaurant").hide();
				$(".stappen.zaal").show();
				$("h2.restaurant").hide();
				$("h2.zaal").show();
			}

		}
	);
}

function bindPageReserveren()
{
	/*$("select .ochtend").hide();
	$("select .middag").hide();
	$("select .avond").hide();*/
	
	$("#avondrest").hide();
	
	$("select[name=aantalpersonen] option:eq(1)").hide();

	$("#restform select").attr("disabled", "disabled");



	
	$("input[name=wachtwoord2]").blur(
		function()
		{
			if ($(this).val() != $("input[name=wachtwoord]").val())
			{
				alert("'Wachtwoord' en 'herhaal wachtwoord' komen niet overeen!");
				$("input[name=wachtwoord]").val('');
				$("input[name=wachtwoord2]").val('');
				$("input[name=wachtwoord]").focus();
			}
		}
	);
	
	$("input[name=wachtwoord]").blur(
		function()
		{
			if ($("input[name=wachtwoord2]").val() != '')
			{
				if ($(this).val() != $("input[name=wachtwoord2]").val())
				{
					alert("'Wachtwoord' en 'herhaal wachtwoord' komen niet overeen!");
					$("input[name=wachtwoord]").val('');
					$("input[name=wachtwoord2]").val('');
					$("input[name=wachtwoord]").focus();
				}
			}
		}
	);
	
	$("form#formww").submit(
			function()
			{
				if ($("input[name=wachtwoord2]").val() != $("input[name=wachtwoord]").val())
				{
					alert("'Wachtwoord' en 'herhaal wachtwoord' komen niet overeen!");
					return false;
				}
			}
	);
	
	$("form#formrest2").submit(
			function()
			{
				if ($("input.restitem:checked").length == 0)
				{
					alert("U moet een keuze maken voor een van de opties");
					return false;
				}
					
			}
	
	);
	
	$(".restitem").click(
		function()
		{
			if ($(this).is(":checked"))
			{
				$(this).parent().parent().find("select").attr("disabled", "");
			}
			else
			{
				$(this).parent().parent().find("select").attr("disabled", "disabled");	
			}
		}
	);

	$(".restitem").each(
			function()
			{
				if ($(this).is(":checked"))
				{
					$(this).parent().parent().find("select").attr("disabled", "");
				}
				else
				{
					$(this).parent().parent().find("select").attr("disabled", "disabled");	
				}
			}
	);
	
	
	
	bindSoort();
	getOverzicht();

	if ($("input[name=soort]").fieldValue() == 'restaurant')
	{
		getRestaurant();
		$("#vergaderform").hide();
		$("#restform").show();
		$(".stappen.restaurant").show();
		$(".stappen.zaal").hide();
		$("h2.restaurant").show();
		$("h2.zaal").hide();
		$("#legenda").hide();
	}
	else
	{
		$("select[name=aantalpersonen] option").show();
		$("select[name=aantalpersonen] option:eq(1)").hide();

		//getZalen();
		$("#vergaderform").show();
		$("#restform").hide();
		$(".stappen.restaurant").hide();
		$(".stappen.zaal").show();
		$("h2.restaurant").hide();
		$("h2.zaal").show();

	}

	$(".optieaantal").attr("disabled", "disabled");
	$(".optieaantal").addClass("disabled");

	$("input[type=checkbox].optie").each(function()
		{	
			if ($(this).is(":checked"))
			{
				var optieID = $(this).val();
				$("#optie_"+optieID).attr("disabled", "").removeClass("disabled");			
			}
			else
			{
			}
		}
	);
	
	
	$("input[type=checkbox].optie").click(function()
		{
			var optieID = $(this).val();
		
			if ($(this).is(":checked"))
			{
				$("#optie_"+optieID).attr("disabled", "").removeClass("disabled");
			}
			else
			{
				$("#optie_"+optieID).attr("disabled", "disabled").addClass("disabled");
			}

			getOverzicht();
		}
	);
	
	$("#ddzaal input[type=checkbox]").click(function()
		{	
			var visible = false;
//			$("select[name=aanvangstijdzaal] option").hide();
			checkDagdelenZaal();
		}
	);

	$("#ddrestaurant input[type=radio]").click(function()
			{	
				var visible = false;
				$("select[name=aanvangstijdrest] option").hide();
				checkDagdelenRestaurant();
			}
	);


	$("select.optieaantal").change(function()
		{
			getOverzicht();
		}
	);
	
	$("input.optieaantal").keyup(function()
		{
			getOverzicht();
		}
	);
	
	function checkDagdelenZaal()
	{
		$("#ddzaal input[type=checkbox]:checked:first").each(				
				function()
				{
					var visible = false;
					var naam = $(this).attr("name");						
						
					$("select[name=aanvangstijdzaal]").html('');
					$("select[name=aanvangstijdzaal]").html($("select[name=copy_aanvangstijdzaal]").html());
					$("select[name=aanvangstijdzaal] option:not(."+naam+")").remove();

					$("select[name=aanvangstijdzaal] option").each(
						function()
						{
							if ($(this).text() == $("input.atz").val())
							{
								$(this).attr("selected", "selected");
							}
						}
					);
				}
		);		
	}

	function checkDagdelenRestaurant()
	{
		$("#ddrestaurant input[type=radio]:checked:first").each(
				function()
				{
					var naam = $(this).attr("value");
			
					if ($(this).is(":checked"))
					{
						if ($(this).attr("value") == 'avond')
						{
							$("select[name=aantalpersonen] option:lt(19)").hide();
							$("#avondrest").show();
						}
						else
						{
							$("select[name=aantalpersonen] option").show();
							$("#avondrest").hide();
						}
						
						$("select[name=aanvangstijdrest] ."+naam).show();
					}
					else
					{
						$("select[name=aanvangstijdrest] ."+naam).hide();
					}
				}
		);		
	}





	$("#datepicker").datepicker({
		'minDate': new Date(),
		'altField':'#altdatepicker',
		'altFormat':'yy-mm-dd',
		'beforeShowDay': datepickerDisabled 
	});

	$("#checkav").click(function()
		{	
			if ($("input[name=soort]").fieldValue() == 'restaurant')
			{
				if (	$("input#altdatepicker").fieldValue() == '' ||
						$("input[name=dagdeel]").fieldValue() == '' ||
						$("select[name=aanvangstijdrest]").fieldValue() == '' ||
						$("select[name=aantalpersonen]").fieldValue() == ''
				
				)
				{
					alert("U moet alle velden invullen");
				}
				else
				{
					getRestaurant();
				}
			}
			else
			{
				if (	$("input#altdatepicker").fieldValue() == '' ||
						($("input#dagdeel-zaal-ochtend").fieldValue() == '' &&
						$("input#dagdeel-zaal-middag").fieldValue() == '' &&
						$("input#dagdeel-zaal-avond").fieldValue() == '') ||
						$("select[name=aanvangstijdzaal]").fieldValue() == '' ||
						$("select[name=aantalpersonen]").fieldValue() == ''
				
				)
				{
					alert("U moet alle velden invullen");
				}
				else
				{
					getZalen();
				}
			}


			return false;
		}
	);
	
	checkDagdelenZaal();
	checkDagdelenRestaurant();
}

function bindPageBezorgen()
{
	
// STAP 1
	$(".stap1submit").hide();
	
	if ($("#altdatepicker").val() == '')
	{
		$("#bezorgmoment").hide();
	}
	
	$("#datepicker").datepicker({
		'minDate': new Date(),
		'altField':'#altdatepicker',
		'altFormat':'yy-mm-dd',
		'beforeShowDay': datepickerDisabled,
		'onSelect': checkVandaag
	});	
	
	$("select[name=bezorgmoment]").change(
		function()
		{
			if ($(this).val() > 0)
			{
				$(".stap1submit").show();
			}
			else
			{
				$(".stap1submit").hide();
			}
		}
	);
	
	
	$("input[name=wachtwoord2]").blur(
		function()
		{
			if ($(this).val() != $("input[name=wachtwoord]").val())
			{
				alert("'Wachtwoord' en 'herhaal wachtwoord' komen niet overeen!");
				$("input[name=wachtwoord]").val('');
				$("input[name=wachtwoord2]").val('');
				$("input[name=wachtwoord]").focus();
			}
		}
	);
	
	$("input[name=wachtwoord]").blur(
		function()
		{
			if ($("input[name=wachtwoord2]").val() != '')
			{
				if ($(this).val() != $("input[name=wachtwoord2]").val())
				{
					alert("'Wachtwoord' en 'herhaal wachtwoord' komen niet overeen!");
					$("input[name=wachtwoord]").val('');
					$("input[name=wachtwoord2]").val('');
					$("input[name=wachtwoord]").focus();
				}
			}
		}
	);
	
// STAP 2
	if ($("input[name=stap]").val() > 1)
	{
		$.getJSON(	"/ajax/bezorg_add.php",
					{
						stap: $("input[name=stap]").val()
					},
				function(data)
				{
					showBezorgoverzicht(data)
				}
		);
	}
	
	$(".addSchaal").click(
			function()
			{
				var schaalID = $(this).parent().find("input[name=schaalID]").val();
				
				$.getJSON(	"/ajax/bezorg_add.php",
						{
							schaalID: schaalID,
							stap: $("input[name=stap]").val()
						},
						function(data)
						{
							showBezorgoverzicht(data)
						}
				);
			}
	);
	
	
	$(".addOptie").click(
			function()
			{
				var optieID = $(this).parent().find("input[name=optieID]").val();
				
				$.getJSON(	"/ajax/bezorg_add_optie.php",
						{
							optieID: optieID,
							stap: $("input[name=stap]").val()
						},
						function(data)
						{
							showBezorgoverzicht(data)
						}
				);
			}
	);
	
// STAP 3
	$(".afladres input").attr("disabled", "disabled");
	
	$("input[name=awfafl]").click(
		function()
		{
			if ($("input[name=awfafl]:checked").length > 0)
			{
				$(".afladres input").attr("disabled", "");
			}
			else
			{
				$(".afladres input").attr("disabled", "disabled");
			}
		}
	);
	
}

function bindBezorgoverzicht()
{
	fvInt();
	
	$("input[name=aantal].schaal").keyup(
			function()
			{
				$.getJSON(	"/ajax/bezorg_add.php",
						{
							schaalID: $(this).parent().find("input[name=schaalID]").val(),
							aantal: $(this).val(),
							stap: $("input[name=stap]").val()
						},
						function(data)
						{
							showBezorgoverzicht(data);
						}
				);	
			}
	);
	
	$("input[name=aantal].overig").keyup(
			function()
			{
				$.getJSON(	"/ajax/bezorg_add_optie.php",
						{
							optieID: $(this).parent().find("input[name=optieID]").val(),
							aantal: $(this).val(),
							stap: $("input[name=stap]").val()
						},
						function(data)
						{
							showBezorgoverzicht(data);
						}
				);	
			}
	);
}

function showBezorgoverzicht(data)
{
	if (data.aantal > 0)
	{
		$("#overig").show();
		$("#content-right").html(data.html);
	}
	else
	{
		$("#overig").hide();
		$("#content-right").html('');
	}
	bindBezorgoverzicht()	
}

function checkVandaag(dateText, inst)
{
	var datum = dateText.split("-");
	var keuze = new Date(datum[2], (datum[1]-1), datum[0]);
	var vandaag = new Date();
	
	$("#bezorgmoment select[name=bezorgmoment] option").removeAttr("disabled");

	
	
	if (keuze.getDate() == vandaag.getDate() && keuze.getMonth() == vandaag.getMonth())
	{
		alert("Beste klant,\n\nWij willen graag dat u zich realiseert dat DROOM! Bezorgservice \u20AC 20,- bezorgkosten rekent voor de bestelling die u vandaag bezorgd wilt hebben. \n\nMet hartelijke groet,\n\n\nHet DROOM! team ");

	//	alert(vandaag.getHours());
		$("#bezorgmoment select[name=bezorgmoment]").html($("#bezorgmoment select[name=copy_bezorgmoment]").html());
		if (vandaag.getHours() >= 15)
		{
			$("#bezorgmoment select option:lt(5):gt(0)").attr("disabled", "disabled");;
		}
		else if (vandaag.getHours() >= 13)
		{
			$("#bezorgmoment select option:lt(4):gt(0)").attr("disabled", "disabled");;
		}
		else if (vandaag.getHours() > 9 || (vandaag.getHours() == 9 && vandaag.getMinutes() > 30))
		{
			$("#bezorgmoment select option:lt(3):gt(0)").attr("disabled", "disabled");;
		}
		else if (vandaag.getHours() > 8 || (vandaag.getHours() == 8 && vandaag.getMinutes() > 30))
		{
			$("#bezorgmoment select option:eq(1)").attr("disabled", "disabled");;
		}
	}
	
	$("#bezorgmoment").show();
}

function getRestaurant()
{
	if ($("input[name=stap]").val() == 1)
	{
		$.post("/ajax/restaurant.php",
			$("#form").serialize(),
			function(data)
			{
				$("#beschikbaarheid").html(data);
			}
		);
	}
}

function getZalen()
{
	if ($("input[name=stap]").val() == 1)
	{
		$.post("/ajax/zalen.php",
			$("#form").serialize(),
			function(data)
			{
				$("#beschikbaarheid").html(data);
				$("#legenda").show();
				
				var pos = $("div#beschikbaarheid h2").position();
				var pos = pos.top - 438;
				
				$("#legenda").css("margin-top", pos+"px");
			}
		);
	}
}

function getOverzicht()
{
	$.post("/ajax/overzicht.php",
		$("#form").serialize(),
		function(data)
		{
			$("#overzicht").html(data);
		}
	);
}


// MODAL

function bindModal()
{
	$("#modal .modal-close").unbind("click");
	$("#modal .modal-close").click(
			function()
			{
					$("#modal").hide();
					$("#modal-shadow").hide();
			}
	);
}

function showModal(link)
{
	if (link != '')
	{
		$.get(	link,
				function(data)
				{
					$("#modal-content").html(data);
					$("#modal-content h2.titel").hide();
					$("#modal-title h2").text($("#modal-content h2.titel").text());
				}
		);
		
		
		var iWidth = $(document).width();
		var iHeight = $(document).height();
		
		var iLeftModal;
		var iTopModal;
		
		iLeftModal = Math.round((iWidth / 2) - 310);
		iTopModal = 20;
		
		$("#modal-shadow").height(iHeight).width(iWidth).show();
		$("#modal").css("top", iTopModal).css("left", iLeftModal).show();
		
		bindModal();
	}
}

/*function datepickerDisabled(date) 
{

	var noWeekend = $.datepicker.noWeekends(date);
	if (noWeekend[0]) 
	{
		for (i = 0; i < natDays.length; i++) 
		{
			if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) 
			{
				return [false, natDays[i][2] + '_day'];
			}
		}
		
		var vandaag = new Date();
		if (vandaag.getHours() > 13 || (vandaag.getHours() == 13 && vandaag.getMinutes() > 29))
		{
			return [false, 'vandaag_day'];
		}
		
		return [true, ''];
	}
	else 
	{
		return noWeekend;
	}
}*/

function datepickerDisabled(date) 
{
	var noWeekend = $.datepicker.noWeekends(date);
	if (noWeekend[0]) 
	{
		for (i = 0; i < natDays.length; i++) 
		{
			if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) 
			{
				return [false, natDays[i][2] + '_day'];
			}
		}
		return [true, ''];
	}
	else 
	{
		return noWeekend;
	}
}


// BEHEER

function bindPageBeheer()
{
	$("#datepicker:not(.noredirect)").datepicker({
		dateFormat: 'yy-mm-dd',
		defaultDate: curdate,
		onSelect: function(dateText, inst)
				{
					document.location.href = 'index.php?datum='+dateText;
				}
	});
	
	$("#datepicker.noredirect").datepicker({
		'dateFormat': 'dd-mm-yy',
		'altField':'#altdatepicker',
		'altFormat':'yy-mm-dd'
	});
	
	$("#datepickerBezorg").datepicker({
		dateFormat: 'yy-mm-dd',
		defaultDate: curdate,
		onSelect: function(dateText, inst)
				{
					document.location.href = 'bezorgen.php?datum='+dateText;
				}
	});
	
	$(".datepicker").datepicker({
		dateFormat: 'yy-mm-dd',
		defaultDate: curdate
	});
}


/*  
 * FIRST VISIT
 ************************/

function doFirstVisit()
{
    
    $(".modal-close").click(function(e){
        e.preventDefault();
        
		closeModal();

    });
    
    
    var message = $("#firstVisitMessage").html();
    
    if(message != null)
    {
        $("#modal-content-inhoud").html(message);
		Cufon.replace('#modal-content h2');
		Cufon.replace('#modal-content h3');
        
        $("#modal-bg").css("width",$(document).width()).css("height",$(document).height());
        centerModal("#modal-content-wrapper", 50);
        $("#modal-bg").fadeIn(750);
        $("#modal-content-wrapper").fadeIn(750);
    }
}

function centerModal(modal, top)
{        
    var topPos  = $(modal).offset().top;
    var leftPos = ($(document).width() / 2) - ($(modal).width() / 2);
    
    $(modal).css("left", leftPos);
    topPos += top;
    $(modal).css("top", topPos);
    $(modal).css("position", "absolute");    
}

function closeModal()
{
    $("#modal-bg").fadeOut(750);
    $("#modal-content-wrapper").fadeOut(750);
}
