var dataAtual =  new Date();
var mesAtual = dataAtual.getMonth()+1;
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&secao_id='+secao+'&data=01/'+strPad(dataAtual.getMonth()+1, 2, '0')+'/'+dataAtual.getFullYear();

  $('tableCalendario').html('<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>');

	$.ajax({
 		type: "GET",
 		url: url,
 		dataType: "json",
 		data: pars,
 		complete: function(requestObj, msg) {
		    setupCalendar( strPad(dataAtual.getMonth(), 2, '0'), 
	  	  							 dataAtual.getFullYear(), 
									 2009,
    								 '#tableCalendario', 
    								 '#tabela_mes','#destMes');
    	}
	})
		
}

function setupCalendar(mes, ano, ano2009, 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));
         funcAnt =     "constroi('-', '30');";     
         funcPro =     "constroi('+', '30');";     
         calendar_html = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><thead><tr><td><a class="mesAnterior" href="javascript:;" onclick="' + funcAnt + 'return false;" title="Mês anterior">&lt;</a></td><td>D</td><td>S</td><td>T</td><td>Q</td><td>Q</td><td>S</td><td>S</td><td><a class="mesProximo" href="javascript:;" onclick="' + funcPro + 'return false;" title="Próximo mês">&gt;</a></td></tr></thead><tbody>';
         
         calendar_html += '<tr><td>&nbsp;</td>\n';
          
         //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>\n';   
            }
           var mesCal = strPad(parseInt(mes, 10)+1, 2, '0');
         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><td>&nbsp;</td>\n';
            
            //Do something different for the current day.
							var data = strPad(day_counter, 2, '0')+'/'+strPad(parseInt(mes, 10)+1, 2, '0')+'/'+ano2009;

							if ( (!dados['eventos']) || (!dados['eventos'][data]) )
               calendar_html += '<td>' + day_counter + '</td>\n';
              else
               calendar_html += '<td><a onclick="$(\''+destaque+'\').html(\'<li>Carregando...</li>\');$(\''+destaque+'\').load(\'index.php?act=calendario&dia='+day_counter+'&mes='+mesCal+'\');" href="javascript:;" title="' + dados['eventos'][data]['titulo'] + '">' + day_counter + '</a></td>\n';
            
            week_day++;
            }
            
         calendar_html += '<td>&nbsp;</td>\n</tr>\n</tbody>\n</table>';
	 		 var mes = strPad(parseInt(mes, 10)+1, 2, '0');
	     
	     
        
         //Display the calendar.     
         $(lblCalendario).html(calendar_html);
         $(lblMes).html(months[month] + '/' + ano);
		 //$(lblMes).html(months[month]);
         if(mes == mesAtual){
		     dia = dataAtual.getDate();
		     $(destaque).load('index.php?act=calendario&dia='+dia+'&mes='+mes);
		 }else{
	       	 $(destaque).html('');
	     }
}
         
	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;
	}
