$(document).ready(function()
{
	$('#datepicker').datepicker({
		beforeShowDay: addClasses,
		dayNamesMin: ['DO', 'LU', 'MA', 'ME', 'GI', 'VE', 'SA'],
		monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre']
	});
	$('.datapicker-input').datepicker({
		showOn: 'both',
		buttonImage: '../images/calendar.png',
		buttonImageOnly: true,
		dayNamesMin: ['DO', 'LU', 'MA', 'ME', 'GI', 'VE', 'SA'],
		monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre']
	});
	$(".ui-datepicker-calendar th").eq(0).addClass("first");
	initDates();
});
function addClasses(date){
	var _class;
	if(date.getDate() < 10) _class = '0' + date.getDate() + '/';
	else _class = date.getDate() + '/';
	if(date.getMonth() < 9) _class += '0' + (date.getMonth()+1) + '/';
	else _class += date.getMonth() + '/';
	return [false, _class];
}

function initDates(){
	var _list = $('#datepicker table td');
	$.ajax({
		url: 'inc/dates.xml',
		cache: false,
		dataType: (jQuery.browser.msie) ? 'text' : 'text',
		success: function(xmlData){
			var _tmp = $('<div></div>').html(xmlData).hide();
			$('.event', _tmp).each(function(){
				var _date = this.getAttribute('date');
				var popup_text = this.innerHTML;
				var _info = this.getAttribute('info');
				_list.each(function(){
					if(this.className.indexOf(_date) != -1){
						$(this).wrapInner(popup_text);
						$(this).addClass("events");
					}
				});
			});
		}
	});
}
