function exibeOcultaCalendario() {
	if ($('table_calendario').style.display == "none") {
		exibirCalendario();
	} else {
		esconderCalendario();
	}
}
function exibirCalendario() {
	if ( $('table_calendario').style.display != "block" ) {
		$('table_calendario').show();
		if ($('fotoColunistaDireita')) {
			$('fotoColunistaDireita').setStyle({zIndex:0});
		}
		drawCalendar();
	}
}

function esconderCalendario() {
	$('table_calendario').hide();
}


// months as they appear in the calendar's title
var ARR_MONTHS = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
		"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
// week day titles as they appear on the calendar
var ARR_WEEKDAYS = ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"];
// day week starts from (normally 0-Dom or 1-Seg)
var NUM_WEEKSTART = 0;
// path to the directory where calendar images are stored. trailing slash req.
var STR_ICONPATH = 'script/calendar/img/';

var dia_corrente;
var mes_corrente;
var ano_corrente;

// get same date in the previous year
var dt_prev_year = new Date();

// get same date in the next year
var dt_next_year = new Date();

// get same date in the previous month
var dt_prev_month = new Date();

// get same date in the next month
var dt_next_month = new Date();

function calcCalendarVariables() {
	var d; var m; var y;
	// mês anterior
	d=dia_corrente; m=mes_corrente; y=ano_corrente;
	if ( ( m + 1 ) == 1 ) {
		y = y - 1;
		m = 11 + 1;
	} else if ( ( m + 1 ) == 3 && d > 28 ) {
		if ( y % 4 == 0 ) {
			d = 29;
		} else {
			d = 28;
		}
	} else if ( ( ( m + 1 ) == 5 || ( m + 1 ) == 7 || ( m + 1 ) == 10 || ( m + 1 ) == 12 ) && d == 31 ) {
		d = 30;
	}
	m=m-1;
	dt_prev_month.setFullYear( y, m, d );
	// mês posterior
	d=dia_corrente; m=mes_corrente; y=ano_corrente;
	if ( ( m + 1 ) == 12 ) {
		y = y + 1;
		m = -1;
	} else if ( ( m + 1 ) == 1 && d > 28 ) {
		if ( y % 4 == 0 ) {
			d = 29;
		} else {
			d = 28;
		}
	} else if ( ( ( m + 1 ) == 3 || ( m + 1 ) == 5 || ( m + 1 ) == 7 || ( m + 1 ) == 8 || ( m + 1 ) == 10 ) && d == 31 ) {
		d = 30;
	}
	m=m+1;
	dt_next_month.setFullYear( y, m, d );
	// Ano anterior
	d=dia_corrente; m=mes_corrente; y=ano_corrente;
	y=y-1;
	if ( y % 4 != 0 && ( m + 1 ) == 1 && d == 29 ) {
		d = 28;
	}
	dt_prev_year.setFullYear( y, m, d );
	// Ano posterior
	d=dia_corrente; m=mes_corrente; y=ano_corrente;
	y=y+1;
	if ( y % 4 != 0 && ( m + 1 ) == 2 && d == 29 ) {
		d = 28;
	}
	dt_next_year.setFullYear( y, m, d );

}
// function passing selected date to calling window
function mesAnterior() {
	//drawCalendar( dt_previous_month.valueOf() );
	drawCalendar( dt_prev_month );
}
function proximoMes() {
	//drawCalendar( dt_next_month.valueOf() );
	drawCalendar( dt_next_month );
}
function anoAnterior() {
	//drawCalendar( dt_next_year.valueOf() );
	drawCalendar( dt_prev_year );
}
function proximoAno() {
	//drawCalendar( dt_next_year.valueOf() );
	drawCalendar( dt_next_year );
}

var DIAS_MES = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function semanasNoMes(ano, mes) {
	var dataInicio = new Date(ano, mes, 1);
	var diaSemanaInicio = dataInicio.getDay();
	var deslocamento = diaSemanaInicio;
	var totalsemanas = (DIAS_MES[mes_corrente] + deslocamento) / 7;
	totalsemanas = Math.ceil(totalsemanas);
	return totalsemanas;
}

function drawCalendar( p_data ) {
	td_dia_selecionado = null;

	if ( p_data == null ) {
		dia_corrente = ( new Date() ).getDate() ;
		mes_corrente = ( new Date() ).getMonth();
		ano_corrente = ( new Date() ).getFullYear() ;
	} else {
		dia_corrente = p_data.getDate() ;
		mes_corrente = p_data.getMonth();
		ano_corrente = p_data.getFullYear() ;
	}

	calcCalendarVariables();

	c="<body bgcolor='#FFFFFF' marginheight='5' marginwidth='5' topmargin='5' leftmargin='5' rightmargin='5'>";
	c+="<table id='calendarTable' class='clsOTable' cellspacing='0' border='0' width='100%'>";
		c+="<tr>"
			c+="<td bgcolor='#4682B4'>";
				c+="<table cellspacing='1' cellpadding='3' border='0' width='100%'>";
					c+="<tr>"
						c+="<td colspan='7'>"
							c+="<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
								c+="<tr>";
									c+='<td valign=top width=10%>'+ '<img src="'+STR_ICONPATH+'prev.gif" width=16 height=16 border=0 alt="mês anterior" onClick="mesAnterior()"></td>';
									c+='<td align=center width=80%><font color=#ffffff>'+ARR_MONTHS[mes_corrente]+' '+ano_corrente + '</font></td>';
									c+='<td valign=top width=10%>'+ '<img src="'+STR_ICONPATH+'next.gif" width=16 height=16 border=0 alt="próximo mês" onClick="proximoMes()">' + '</td>';
								c+='</tr>';
							c+='</table>';
						c+='</td>'
					c+='</tr>';
					c+='<tr>';

	// print weekdays titles
	for (var n=0; n<7; n++) {
		c+='<td bgcolor="#87cefa" align="center"><font color="#ffffff">'+ARR_WEEKDAYS[(NUM_WEEKSTART+n)%7]+'</font></td>';
	}
	c+='</tr>';

	// print calendar table
	var hoje = new Date( ano_corrente, mes_corrente, dia_corrente );
	var data = new Date( hoje.getFullYear(), hoje.getMonth(), 1 );
	data.setDate( data.getDay() * ( -1 ) + 1 );
	var dia = 1;
	var mes = data.getMonth();
	var ano = data.getYear();
	var hojeDia = ( new Date() ).getDate();
	var hojeMes = ( new Date() ).getMonth();
	var hojeAno = ( new Date() ).getFullYear();
	var celula = 0;
	
	var totalsemanas = semanasNoMes(ano_corrente, mes_corrente);
	
	// Semanas
	for ( semana = 1; semana <= totalsemanas; semana ++ ) {
		c+= '<tr>';
		// Dias da Semana
		for ( dia_semana = 0; dia_semana < 7; dia_semana ++ ) {
			celula ++;
			color = "navy";
			// Dia atual
			if ( data.getDate()     == hojeDia &&
					  data.getMonth()    == hojeMes &&
					  data.getFullYear() == hojeAno
					) bgcolor="#ffb6c1";
			// se o dia for de outro mês
			else if ( data.getMonth() != mes_corrente ) { bgcolor="Silver"; color = "gray"; }
			// Final de semana (sábado, domingo)
			else if ( dia_semana == 0 || dia_semana == 6 ) bgcolor="#dbeaf5";
			// Dias comuns da semana (segunda a sexta-feira)
			else bgcolor = "#ffffff";
			// Exibir dia
			tag_p = "";
			originalColor = " originalBGColor='" + bgcolor + "' originalFGColor='" + color + "' ";
			if ( data.getDate() == 1 &&
				 data.getMonth() == mes_corrente &&
				 data.getFullYear() == ano_corrente ) {
				//bgcolor = "navy";
				//color   = "white";
				tag_p   = " id='primeira_celula' ";
			}

			c+= "<td " + tag_p + originalColor + " onClick='selecionarData(this, " + data.getDate() + "," + ( data.getMonth() + 1 ) + "," + data.getFullYear() + ")' style='cursor:pointer;color:" + color + ";background-color:" + bgcolor + "' sbgcolor='" + bgcolor + "' align=center width='14%'>" +
					data.getDate()+
				"</td>";

			// Obter próximo dia
			dia = data.getDate();
			mes = data.getMonth() + 1;
			ano = data.getFullYear();
			if ( mes == 2 ) {
				if ( dia == 28 && ( ano % 4 ) == 0 ) {
					dia = 29;
				} else if ( dia > 27 ) {
					dia = 1;
					mes = 3;
				} else {
					dia ++;
				}
			} else if ( dia == 31 && mes == 12 ) {
				dia = 1;
				mes = 1;
				ano ++;
			} else if ( ( dia == 31 ) || ( ( mes == 4 || mes == 6 || mes == 9 || mes == 11 ) && dia == 30 ) ) {
				mes ++;
				dia = 1;
			} else {
				dia ++;
			}

			data.setFullYear( ano, mes - 1, dia );
			

		}
		c+= '</tr>';
	}
	c+= "</table>";

	$('div_calendario').innerHTML = c;
	$('div_calendario').style.visibility = "visible";

}
