﻿var todaydate = new Date();
var month = todaydate.getMonth()+1;
var curyear = todaydate.getFullYear();

function previous(){
	if(month != 1){
		month = month - 1;
		updateCalendar();
	} else {
		curyear = curyear - 1;
		month = 13;
		previous();
	}
}

function next(){
	if(month != 12){
		month = month+1;
		updateCalendar();
	} else {
		month = 0;
		curyear = curyear + 1;
		next();
	}
}

var dicDataCursos;

function message(msg){
	
	$('a').style.display = 'none';
	$('b').style.display = 'block';
	$('b').innerHTML = msg;
	
}

function cLoaded(){
	
	$('calendar').className = 'box calendar';
	$('e').style.display = 'block';
}

function updateCalendar(){
	$('calendar').className += ' loading';
	$('e').style.display = 'none';
	if(dicDataCursos == undefined){
		new Ajax('/Cursos.txt', {
			method: 'get',
			onComplete: function(response){
				dicDataCursos = new Json.evaluate(response);
				build(month, curyear);
			},
			onFailure: function(){
				
				cLoaded();
				$('e').innerHTML = 'Ocorreu um erro ao tentar carregar a Agenda de Cursos, <a href="" style="margin-top:10px; clear:both; float:left">Clique aqui para atualizar</a>';
				
			}
		}).request();
	} else {
		build(month, curyear);
	}
}

function build(m, y){
	
	var mn = ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var dim = [31,0,31,30,31,30,31,31,30,31,30,31];
	
	var oD = new Date(y, m-1, 1);
	oD.od = oD.getDay()+1;
	
	var d = '';
	
	dim[1] = (((oD.getFullYear() % 100 != 0) && (oD.getFullYear() % 4 == 0)) || (oD.getFullYear() % 400 == 0)) ? 29 : 28;
	
	$('month').innerHTML = mn[m-1] + ' - ' + y;
	
	var curDtaCursos = filterCursos();
	
	var tmpDataCursos = null;			
	var tmpCurso = null;			
	var x = null;
	for(var i = 1; i <= 42; i++){
		
		x = ((i - oD.od >= 0) && (i - oD.od < dim[m-1])) ? (i - oD.od + 1) : '&nbsp;';
		
		for(var a = 0; a < curDtaCursos.length; a++){
			tmpDataCursos = curDtaCursos[a];
			if(x == tmpDataCursos.Date[0]){
				if (tmpDataCursos.Cursos.length > 0)
				{
					if (tmpDataCursos.Cursos.length == 1)
					{
						tmpCurso = tmpDataCursos.Cursos[0];
						if (!tmpCurso.url)
							tmpCurso.url = '/Cursos/Basicos/FormacaoInvestidores.aspx';
						x = '<a href="' + tmpCurso.url + '" title="' + tmpCurso.Title + '::' + tmpCurso.Description + '" class="pop ' + tmpCurso.Type + '">' + x + '</a>';
					}
					else
					{
						var tipo = '';
					
						for (var t = 0; t < tmpDataCursos.Cursos.length; t++)
						{
							if (t == 0)
								tipo = tmpDataCursos.Cursos[t].Type;
							else if (tipo != tmpDataCursos.Cursos[t].Type)
								tipo = '';
						}
					
						x = '<a href="javascript:;" countCursos="' + tmpDataCursos.Cursos.length + '" title="' + tmpDataCursos.Cursos.length + ' programações" class="pop ' + tipo + '">' + x + '</a>';
					}
				}
			}
		}
		
		d += '<li>' + x + '</li>';
	}
	
	$('days').innerHTML = d;
	
	var Tips1 = new Tips($$('.pop'), {
		hideDelay: 400,
		maxTitleChars: 50,
		maxTextChars: 150,
		fixed: true
	});
	
	cLoaded();
}

function filterCursos(dia)
{
	var curDtaCursos = [];
	var ci = 0;
	var tmpDataCursos = null;
	for(a = 0; a < dicDataCursos.length; a++){
		tmpDataCursos = dicDataCursos[a];
		if((curyear == tmpDataCursos.Date[2]) 
			&& (month == tmpDataCursos.Date[1])
			&& ((dia == null) || (dia == tmpDataCursos.Date[0]))
			){
			curDtaCursos[ci] = tmpDataCursos;
			ci++;
		}
	}
	return curDtaCursos;
}