/**********************************************************************
* Funções JavaScript para o calendário
*
* Mário Pinto - 2009.03.22
*********************************************************************/

function bissexto(ano){
  if ( ((ano % 4 == 0) && (ano % 100 != 0) )||(ano % 400 == 0) )
    return true;
  else
    return false;
}

function dias_mes(mes,ano){
  var num_dias = 0;
  mes = Number(mes); // Tratar o mês como sendo um número
  switch (mes){
    case 4: case 6: case 9: case 11: num_dias = 30; break;
    case 2: num_dias = ( bissexto(ano) ) ? 29 : 28; break;
    default: num_dias = 31; break;
  }
  return num_dias;
}

function nome_mes(mes){
  var nome;
  mes = Number(mes);
  switch (mes){
    case 1: nome = 'Janeiro';break;
    case 2: nome = 'Fevereiro';break;
    case 3: nome = 'Março';break;
    case 4: nome = 'Abril';break;
    case 5: nome = 'Maio';break;
    case 6: nome = 'Junho';break;
    case 7: nome = 'Julho';break;
    case 8: nome = 'Agosto';break;
    case 9: nome = 'Setembro';break;
    case 10: nome = 'Outubro';break;
    case 11: nome = 'Novembro';break;
    case 12: nome = 'Dezembro';break;
    default: nome = 'Desconhecido';break;
  }
  return nome;
}

function draw_month(mes,ano){
  var html = '';
  mes = Number(mes);
  ano = Number(ano);
  n_dias = dias_mes(mes,ano);
  
  var temp_data = new Date();
  temp_data.setFullYear(ano);
  temp_data.setMonth(mes-1,1);
  temp_var = 0; // Domingo = 0; 2ª- = 1; ...; Sábado = 6
  // criam-se células vazias até ao dia em questão
  html = '<table>';
  
  //html += '<tr><td colspan="3">'+ano+'</td><td colspan="4">'+nome_mes(mes)+'</td></tr>';
  
  html += '<tr><td>D</td><td>2ª</td><td>3ª</td><td>4ª</td><td>5ª</td><td>6ª</td><td>S</td></tr>';

  while (temp_var != temp_data.getDay()){
    html +='<td></td>';
    temp_var = ++temp_var % 7;
  }
  for (var i=1;i<=n_dias;i++)
  {
    temp_data.setDate(i);
    temp_dia_semana = temp_data.getDay();
    
    if (temp_dia_semana % 7 == 0)
      html +='<tr>';
    
    html +='<td class="calendario" id="'+ano+'.'+mes+'.'+i+'">'+i+'</td>';
    
    if (temp_dia_semana % 7 == 6)
      html +='</tr>';
  }
  html +='</tr></table>';
  return html;
}

function calendar_controls(){
  var html = '';
  var temp_data = new Date();
  var month = temp_data.getMonth();
  var year = temp_data.getFullYear();
  month++;
  
  // Mês
  html += '<button onclick="document.getElementById(\'month\').value--;calendar_show()">-</button>';
  html += '<select id="month" onchange="calendar_show()">';
  for (i=1; i<=12; i++)
    if (i==month)
      html += '<option value="'+i+'" SELECTED>'+nome_mes(i)+'</option>';
    else
      html += '<option value="'+i+'">'+nome_mes(i)+'</option>';
  html += '</select>';  
  html += '<button onclick="document.getElementById(\'month\').value++;calendar_show()">+</button>';
  
  // Ano
  html += '<button onclick="document.getElementById(\'year\').value--;calendar_show()">-</button>';
  html += '<select id="year">';
  for (i=2000; i<=year+5; i++)
    if (i==year)
      html += '<option value='+i+' SELECTED>'+i+'</option>';
    else
      html += '<option value='+i+'>'+i+'</option>';
  html += '</select>';
  html += '<button onclick="document.getElementById(\'year\').value++;calendar_show()">+</button>';

  html +='<button onclick="calendar_show()">Ver</button>';
  
  html +='<button onclick="switch_visibility(\'calendario\',\'show_calendario\',\'hide_calendario\');">X</button>';

  return html;
}

function calendar_show(){
  var mes = document.getElementById('month').value;
  var ano = document.getElementById('year').value;

  document.getElementById('div_month').innerHTML = draw_month(mes,ano);
}
