$(document).ready(function() {
	var date = new Date();
    $('#sponsors').jcarousel({
		auto: 2,
		scroll: 1,
		vertical: true,
		wrap: "last"
	});
	function daysInMonth(iMonth, iYear)
	{
		return 32 - new Date(iYear, iMonth, 32).getDate();
	}
	
	$('#datepicker').datepicker({
		inline: true,
		dateFormat: 'dd-mm-yy',
		dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Fr', 'Za'],
		monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
		nextText: "Volgende",
		prevText: "Vorige",
		minDate: new Date(date.getFullYear(), date.getMonth(), 1),
		/*maxDate: new Date(date.getFullYear(), date.getMonth(), daysInMonth(date.getMonth(),date.getFullYear())),*/
		beforeShowDay: function(thedate) { 
			var theday = thedate.getDate();
			if($.inArray(theday,specialDays) == -1) return [true,""];
			return [true, "eventDay"];
		},
		onChangeMonthYear: function(year, month, inst){
			$.ajax({
				type: "POST",
				url: "fetchNewDate.php",
				data: "date=" + month + "-" + year,
				success: function(msg){
					specialDays = msg.split(" ,");
					var shtml = "";
					$('#datepicker a').each(function(){
						shtml = $(this).html();
						if(shtml.length == 1) shtml = '0' + $(this).html();
						else shtml = $(this).html();
						
						if($.inArray(shtml,specialDays) == -1){}
						else{
							$(this).parent().attr("class","eventDay");
						}
					});	
				}
			});
		}
	});
	$('#datepicker').change(function(){
		var date = $('#datepicker').val();
		var dateArray = date.split("-");
		
		$.ajax({
			type: "POST",
			url: "fetchEvents.php",
			data: "date=" + date,
			success: function(msg){
				if(msg != "") $('#agenda').load(msg);
			}
		});
		
		$.ajax({
			type: "POST",
			url: "fetchNewDate.php",
			data: "date=" + dateArray[1] + "-" + dateArray[2],
			success: function(msg){
				specialDays = msg.split(" ,");
				$('#datepicker a').each(function(){
					shtml = $(this).html();
					if(shtml.length == 1) shtml = '0' + $(this).html();
					else shtml = $(this).html();
					
					if($.inArray(shtml,specialDays) == -1){}
					else{
						$(this).parent().attr("class","eventDay");
					}
				});	
			}
		});
	});
});
