FEVEREIRO=28;
setBisexto((new Date).getFullYear());
SEMANA = Array("D","S","T","Q","Q","S","S");
MESES = Array("january","february","march","april","may","june","july","august","september","october","november","december");
MESES_PT = Array("Janeiro","Fevereiro","Mar&ccedil;o","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro");
MES_ATUAL= (new Date()).getMonth();
ANO_ATUAL = (new Date()).getFullYear();
DIA_ATUAL = (new Date()).getDate();

function setBisexto(ano) {
	if(((ano%4)==0) && ((ano%100)!=0))
		FEVEREIRO=29;
	else
		FEVEREIRO=28;
}

function getElmt(id) {
	return document.getElementById(id);
}

function showData() {
	DIAS_MES =  Array(31,FEVEREIRO,31,30,31,30,31,31,30,31,30,31);
	sep="/";
	var dia,mes,ano,sem;

	mes = MES_ATUAL;
	ano = ANO_ATUAL;
	
	getElmt('cal').innerHTML='';

	getElmt('titulo').innerHTML=MESES_PT[mes]+' - '+ANO_ATUAL;

	for(i=0;i<7;i++)
		getElmt('cal').innerHTML+='<div class="dia"  style="background-color:#ddd"><b>'+SEMANA[i]+'</b></div>';

	printEmptyFields(getFirstLastDate(mes,ano,1));
		
	for(i=1;i<=DIAS_MES[mes];i++) {
		i==DIA_ATUAL ?	estilo="background-color:#cc0000; color:#fff" : estilo="";
		getElmt('cal').innerHTML+='<div onclick=buscarConteudo("content","buscarArquivos","'+i+sep+(mes+1)+sep+ano+'","") style="'+estilo+'" class="dia">'+i+'</div>';
	}
	
	printEmptyFields(6-getFirstLastDate(mes,ano,DIAS_MES[mes]));
}

function prev() {
	if(MES_ATUAL==0) {
		ANO_ATUAL-=1;
		MES_ATUAL=11;
	}
	else
		MES_ATUAL-=1;
		
	setBisexto(ANO_ATUAL);
	showData();
}

function next() {
	if(MES_ATUAL==11) {
		ANO_ATUAL+=1;
		MES_ATUAL=0;
	}
	else
		MES_ATUAL+=1;
		
	setBisexto(ANO_ATUAL);
	showData();
}

function printEmptyFields(s) {
	estilo="";
	for(i=0;i<s;i++) {
		getElmt('cal').innerHTML+='<div class="dia">&nbsp;</div>';
	}
}

function getFirstLastDate(mes, ano, dia) {
	var first = new Date(MESES[mes]+" "+dia+","+ano);
	return first.getDay();
}

setBisexto((new Date()).getFullYear());