var type = 1;

$(function () {
        var numOfMonths = 4;        
        if($("#box-count").val()) {
            numOfMonths = $("#box-count").val();
            type = 2;
        }

	$.ajax({  
		url: "/kalendar/get-events-date/",
		data: "action=showdates&id=1",
		dataType: "json",
		success: function(calendarEvents){   
			$("#calendarwidget").datepicker({  				
				// [rows, columns] if you want to display multiple calendars.                           
				numberOfMonths: [1, numOfMonths],
				showCurrentAtPos: 0, 
				firstDay: 1,
				beforeShowDay: function (date){  					
					for (i = 0; i < calendarEvents.length; i++) {  
						if (date.getMonth() == calendarEvents[i][0] - 1   
						&& date.getDate() == calendarEvents[i][1]  
						&& date.getFullYear() == calendarEvents[i][2]) {  
						//[disable/enable, class for styling appearance, tool tip]  
						return [true,"active","Zobraziť podujatia"];
						}  
					}  
					return [false, ""]; //enable all other days
				},
				onSelect: function(date, inst) { 
					reloadEvents(date);
				}
			});  
		}  
	}); 

});

function reloadEvents(date) {
    $('#events').ajaxProgress();
    var url = '/kalendar/reload-events/';
    if (typeof(date) != 'undefined' && date != '') {
        url += 'edate/' + date + '/';
        url += 'type/' + type + '/';
    }
    $('#events').load(url, function () {
        //$.getScript('/public/js/admin/usermanager/user_items.js');
    });
}

