var dataAtual =  new Date();
var b = '';
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&tag=calEventos&data=01/'+strPad(dataAtual.getMonth()+1, 2, '0')+'/'+dataAtual.getFullYear();

  $('tableCalendario').html('table width="100%" height="85px" 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>');

	$.ajax({
 		type: "GET",
 		url: url,
 		dataType: "json",
 		data: pars,
 		complete: function(requestObj, msg) {
	    setupCalendar( strPad(dataAtual.getMonth(), 2, '0'), 
  	  							 dataAtual.getFullYear(), 
    								 '#tableCalendario', 
    								 '#tabela_mes','#destMes');
    }
	})
		
}

function setupCalendar(mes, ano, lblCalendario, lblMes, destaque) {

         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=calendario&dataini=' + data + '" title="' + dados['eventos'][data]['titulo'] + '">' + day_counter + '</a></td>';
            
            week_day++;
            }
            
         calendar_html += '</tr></tbody></table>';
	     
        
         //Display the calendar.     
         $(lblCalendario).html(calendar_html);
         $(lblMes).html(months[month] + '/' + ano);
	       //$(destaque).load('index.php?act=calendario&mes='+mes);
}
         
	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;
	}

