var objects = null;

updateEventsByDate = function(year, month) {
	if (year && month ) {
		$.get("/ru/info/AJAXGetEvents/" + year + "/" + month, function(data) {
			objects = eval(data);
			$(".ui-datepicker-calendar tr td a").each(function() {
				for (var i in objects) {
					var day = $(this).html().length > 1 ? $(this).html() : 0 + $(this).html();
					var localMonth = month < 10 ? "0" + month : month;
					var id = objects[i].Id;
					if (year + '-' + localMonth + '-' + day == objects[i].date) {
						$(this).css('border', '1px solid red');
						$(this).css('color', 'red');
					}
				}
			});
		});
	}
};

$(document).ready(function() {
	$('#datepicker').datepicker({
		inline: true,
		firstDay: 1,
		onChangeMonthYear: function(year, month, inst) { 
			updateEventsByDate(year, month);
		},
		onSelect: function(dateText, inst) {
			dateText = dateText.replace(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/,'$3-$1-$2');
			for (var i in objects) {
				if (dateText == objects[i].date) {
					document.location.href = '/ru/info/events/' + objects[i].Id;
				}
			}
		}
	});
	
	var currentTime = new Date();
	updateEventsByDate(currentTime.getFullYear(), currentTime.getMonth()+1);
});
