
function Calendar(year,month){
	if(month>currentM && year>=currentY) return;
	var head = [[],[],[],[],[],[],[]];
	var months = ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var days = ['D','S','T','Q','Q','S','S'];
	var date = new Date(year, month - 1, 1);
	var n = date.getDay();
	$.ajax({
		url: "/xml/calendario/"+year+"/"+month+"/calendario.xml",
		cache: false,
		success: function(xml) {
			for(k=0; k < n; k++)
				head[k].push('');
			$(xml).find("d").each(function() {
				n = (n > 6 ? 0 : n); 
				var t = $(this).text(),
				_d = (new Number(t) < 10 ? '0'+t : t ),
				_m = new Number(month) < 10 ? '0'+month : month;
				if($(this).attr("h") == "y")
					t = '<a target="_blank" href="/boletim/'+_d+_m+year+'">'+t+'</a>';
				head[n].push(t);
				n++;
			});
			for(q=0;q <= (6-n);q++)
				head[n+q].push('');
			var cm = '', cy = '';
			for(y=0; y<months.length; y++)
				cm += '<option value="'+(y+1)+'">'+months[y]+'</option>';
			$("#_month").html(cm);
			$("#_month option[value='"+month+"']").attr("selected", "selected");
			for(s=2003;s<=currentY;s++)
				cy += '<option value="'+s+'">'+s+'</option>';
			$("#_year").html(cy);
			$("#_year option[value='"+year+"']").attr("selected", "selected");
			var thead = '<thead><tr>';
			for(x=0;x<head.length;x++)
				thead += '<td>'+days[x]+'</td>';
			thead += '</tr></thead>';
			var tbody = '<tbody>';
			for(i=0;i<head[0].length+1;i++){
				tbody += "\n<tr>";
				for(j=0;j<head.length;j++){
					if(head[j][i] || head[j][i]==''){
						var fds = (j==0 || j==6 ? ' class="fds"' : '');
						tbody += "\n\t<td"+fds+">"+head[j][i]+"</td>";
					}
				}
				tbody += '</tr>';
			}
			tbody += '</tbody>';
			$("#_calendar").html( thead + tbody );
		},
		error: function(){
			for(k=0;k<n;k++)
				head[k].push('');
			for(i=1;i<30;i++){
				n=(n>6 ? 0 : n);
				_d = (new Number(i) < 10 ? '0'+i : i),
				_m = new Number(month) < 10 ? '0'+month : month;
				if($(this).attr("h") == "y")
					i = '<a target="_blank" href="/boletim/'+_d+_m+year+'">'+i+'</a>';
				head[n].push(i);
				n++;
			}
			var thead = '<thead><tr>';
			for(x=0;x<head.length;x++)
				thead+='<td>'+days[x]+'</td>';
			thead+='</tr></thead>';
			var tbody = '<tbody>';
			for(i=0;i<head[0].length+1;i++){
				tbody += "\n<tr>";
				for(j=0;j<head.length;j++)
					if(head[j][i] || head[j][i]=='')
						tbody += "\n\t<td>"+head[j][i]+"</td>";
				tbody += '</tr>';
			}
			tbody += '</tbody>';
			$("#_calendar").html(thead+tbody);
		}
	});
}
function Go(to){
	var cur='';
	cur = new Number($("#_month").val())+(to);
	if(to<0)
		if(cur<1){
			g=12;
			$("#_year option[value='"+(new Number($("#_year").val())-1)+"']").attr("selected", "selected");
		}else
			g=cur;
	else if(to>0)
		if(cur>12){
			g=1;
			$("#_year option[value='"+(new Number($("#_year").val())+1)+"']").attr("selected", "selected").val();
		}
		else
			g=cur;
	$("#_month option[value='"+g+"']").attr("selected", "selected");
	Calendar( $("#_year").val(), $("#_month").val() );
}
$(document).ready(function() {
	Calendar(currentY, currentM);
	$("#_month").change(function() { Calendar($("#_year").val(), this.value);});
	$("#_year").change(function() { Calendar(this.value, $("#_month").val());});
	$("#_next").click(function() { Go( + 1); });
	$("#_prev").click(function() { Go( - 1); })
});