var dataAtual =  new Date();
var months = new Array('Janeiro',
                       'Fevereiro',
                       'Março',
                       'Abril',
                       'Maio',
                       'Junho',
                       'Julho',
                       'Agosto',
                       'Setembro',
                       'Outubro',
                       'Novembro',
                       'Dezembro');

function constroi(direcao, secao)
	{

	if (direcao == '+') dataAtual.setMonth(dataAtual.getMonth() + 1);
	if (direcao == '-') dataAtual.setMonth(dataAtual.getMonth() - 1);
	
		var url = '/index.php';
		var pars = 'act=getDadosCalendario&secao_id='+secao+'&data=01/'+strPad(dataAtual.getMonth()+1, 2, '0')+'/'+dataAtual.getFullYear();

    $('tableCalendario').innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><thead></thead><tbody><tr><td>Carregando informações do calendário de ' + months[dataAtual.getMonth()]+'/'+dataAtual.getFullYear() + '</td></tr></tbody></table>';

		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: showResponse
			});
		
	}

	function showResponse(originalRequest) {
		
		eval(originalRequest.responseText);
		
    setupCalendar( strPad(dataAtual.getMonth(), 2, '0'), 
    							 dataAtual.getFullYear(), 
    							 'tableCalendario', 
    							 'tabela_mes');
  }

function setupCalendar(mes, ano, lblCalendario, lblMes) {
        
//debugger;
         if ( (mes == null) || (ano == null) )
            date = new Date();
         
         if ( (mes != null) && (ano != null) )
            date = new Date(ano, mes, 1);

         day = date.getDate();
         month = date.getMonth();
         year = date.getFullYear();
         
         this_month = new Date(year, month, 1);
         next_month = new Date(year, month + 1, 1);
         
         //Find out when this month starts and ends.         
         first_week_day = this_month.getDay();
         days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
         
         
         calendar_html = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><thead><tr><td>D</td><td>S</td><td>T</td><td>Q</td><td>Q</td><td>S</td><td>S</td></tr></thead><tbody>';
         
         calendar_html += '<tr>';
          
         //Fill the first week of the month with the appropriate number of blanks.       
         for(week_day = 0; week_day < first_week_day; week_day++)
            {
            calendar_html += '<td></td>';   
            }
            
         week_day = first_week_day;
         for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
            {
            week_day %= 7;
            
            if(week_day == 0)
               calendar_html += '</tr><tr>';
            
            //Do something different for the current day.
							var data = strPad(day_counter, 2, '0')+'/'+strPad(parseInt(mes, 10)+1, 2, '0')+'/'+ano;

							if ( (!dados['eventos']) || (!dados['eventos'][data]) )
               calendar_html += '<td>' + day_counter + '</td>';
              else
               calendar_html += '<td class="' + dados['eventos'][data]['tag'] + '"><a href="index.php?act=leituraCalendario&codigo=' + dados['eventos'][data]['id'] + '" title="' + dados['eventos'][data]['titulo'] + '">' + day_counter + '</a></td>';
            
            week_day++;
            }
            
         calendar_html += '</tr></tbody></table>';
         
         //Display the calendar.     
         $(lblCalendario).innerHTML = calendar_html;
         $(lblMes).innerHTML = months[month] + '/' + ano;
                           
}
         
	function strPad(iVar,iLng,iChr) { // bruges til formattering af tal
		iVar=""+iVar;
		if (!iChr || iChr=="") {
			iChr="0";
		}
		while (iVar.length<iLng) {
			iVar=iChr+iVar;
		}
		return iVar;
	}
