(function($){

function receita( dia, nomereceita, linkreceita ) { 
    this.dia = dia; 
    this.nomereceita = nomereceita; 
    this.linkreceita = linkreceita; 
}  

function addTipsys(year, month, calendarId){
   $.ajax(
		{ 
			type: 'post',
			data: "year="+year+"&month="+month,
			url: "../ajax/getevents.php", 
			context: document.body, 
			//timeout: 65000,  
			//contentType: "text/html;charset=UTF-8",			
			success: function(data){
				var lstrec = new Array(); 
				
				arraydados = data.split(",");
				//alert(arraydados.length);
				for(i=0; i < arraydados.length; i++ ){
					infore = arraydados[i].split(":");
					lstrec[i] = new receita(infore[0],infore[1],infore[2]); 					
				}
				
				if(arraydados.length == 1){
					//alert(lstrec[0].dia);
					if((!lstrec[0].dia)&&(!lstrec[0].nomereceita)&&(!lstrec[0].linkreceita))
					{
						lstrec.length = 0;
					}
				}
				
				var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
				//alert(lstrec[0].nomereceita);
				//alert(lstrec[0].nomereceita);
				for(x=0; x < lstrec.length; x++){
					theDateLinks.eq(lstrec[x].dia-1)  // select the right link
					//.attr('id','eventdate')
					.attr('class','ui-state-default ui-state-highlight ui-state-active')
					
					.attr('href',lstrec[x].linkreceita)
					.parent().attr('onclick','')
					.attr('original-title', lstrec[x].nomereceita)  // set the text
					.tipsy({html: true }); 
				}
			},
			error: function(){
				alert('Erro a carregar eventos no calendario');
			}
		});
}

// Because the the event `onChangeMonthYear` get's called before updating 
// the items, we'll add our code after the elements get rebuilt. We will hook 
// to the `_updateDatepicker` method in the `Datepicker`.
// Saves the original function.
var _updateDatepicker_o = $.datepicker._updateDatepicker;
// Replaces the function.
$.datepicker._updateDatepicker = function(inst){ 
   // First we call the original function from the appropiate context.
   _updateDatepicker_o.apply(this, [inst]); 
   // No we can update the Tipsys.
   addTipsys(inst.drawYear, inst.drawMonth+1, inst.id);
};

// Finally the calendar initializer.
$(function(){
	// Creates the date picker, with your options.
	var d = new Date();
	year = d.getFullYear();
	month = d.getMonth();
	minmonth = d.getMonth() - 1;
	maxmonth = d.getMonth() + 1;
	daysmonth = 32 - new Date(year, maxmonth, 32).getDate(); 
	$('#datepicker').datepicker({
		/*onChangeMonthYear: function(year, month, inst) {
			addTipsys(year, maxmonth, inst.id);
		},*/
		inline: true,
		dateFormat: 'yy-mm-dd',
		minDate: new Date(year,minmonth, 1),
		maxDate: new Date(year,maxmonth,daysmonth),
		monthNames: ["Janeiro", "Fevereiro", "Mar\u00e7o", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
		dayNamesMin: ["Do", "Se", "Te", "Qu", "Qi", "Se", "Sa"]
	});	
   // Gets the date and initializes the first round of tipsies.
   var currentDate = $('#datepicker').datepicker('getDate');
   // month+1 because the event considers January->1
   // Last element is null, because, it doesn't actualy get used in the hanlder.
   //addTipsys(currentDate.getYear(), currentDate.getMonth()+1, 'datepicker');
});

})(jQuery);
