var oCalendar = new Object();
oCalendar.Init = function(tagCalendario, tagMes, tagAno, tagEvento, classNormal, classEspecial, classAtual, fncConteudoCelula, aDatas) {

	this.classAtual = classAtual;
	this.classNormal = classNormal;
	this.classEspecial = classEspecial;
	this.fncConteudoCelula = fncConteudoCelula;

	this.aDatas         = aDatas;
	this.tagCalendario  = this.MM_findObj(tagCalendario);
	this.tagMes    = this.MM_findObj(tagMes);
	this.tagAno    = this.MM_findObj(tagAno);
	this.tagEvento = this.MM_findObj(tagEvento);

	this.diasMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	this.nomesMes = new Array('Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho',
								 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');

	this.dataCorrente   = new Date();
	this.hoje           = this.dataCorrente.getDate();

	this.mesCorrente    = this.dataCorrente.getMonth();
	this.anoCorrente    = this.dataCorrente.getFullYear();

	this.diaSelecionado = this.dataCorrente.getDay();
	this.mesSelecionado = this.dataCorrente.getMonth();
	this.anoSelecionado = this.dataCorrente.getFullYear();


	if (ini = this.aDatas['inicio']) {
		this.dataInicio  = new Date(ini.split("/")[2], ini.split("/")[1]-1, ini.split("/")[0]);
	} else {
		this.dataInicio  = new Date(this.anoCorrente, this.mesCorrente, 1);
	}

	if (fim = this.aDatas['fim']) {
		this.dataFim  = new Date(fim.split("/")[2], fim.split("/")[1]-1, fim.split("/")[0]);
	} else {
		this.dataInicio  = new Date(this.anoCorrente, this.mesCorrente, 28);
	}

}

oCalendar.exibeEvento = function(data) {
	this.tagEvento.innerHTML = this.arrayDatas[this.diaSelecionado]['evento'];
}

oCalendar.limpaEvento = function() {
	this.tagEvento.innerHTML = '';
}

oCalendar.setData = function(data) {

	this.dataCorrente   = new Date(data);
	this.hoje           = this.dataCorrente.getDate();

	this.mesCorrente    = this.dataCorrente.getMonth();
	this.anoCorrente    = this.dataCorrente.getFullYear();

	this.diaSelecionado = this.dataCorrente.getDay();
	this.mesSelecionado = this.dataCorrente.getMonth();
	this.anoSelecionado = this.dataCorrente.getFullYear();

}

oCalendar.proximoMes = function() {

  var mudouAno = false;

  this.mesCorrente++;
  if (this.mesCorrente >= 12)
  {
	this.mesCorrente = 0;
	this.anoCorrente++;
	mudouAno = true;
  }

  var dtTemp = new Date(this.anoCorrente, this.mesCorrente, 1);
  if ( dtTemp.getTime() > this.dataFim.getTime() ) {
	if (mudouAno) {
      this.anoCorrente--;
	  this.mesCorrente = 11;
	} else {
	  this.mesCorrente--;
	}
	return false;
  }


  this.exibeAnoMes(this.anoCorrente, this.mesCorrente);
  this.Make();
}


oCalendar.anteriorMes = function() {

  var mudouAno = false;

  this.mesCorrente--;
  if (this.mesCorrente < 0)
  {
	this.mesCorrente = 11;
	this.anoCorrente--;
	mudouAno = true;
  }

  var dtTemp = new Date(this.anoCorrente, this.mesCorrente, 1);
  if ( dtTemp.getTime() < this.dataInicio.getTime() ) {
	if (mudouAno) {
      this.anoCorrente++;
	  this.mesCorrente = 0;
	} else {
	  this.mesCorrente++;
	}
	return false;
  }

  this.exibeAnoMes(this.anoCorrente, this.mesCorrente);
  this.Make();

}

oCalendar.proximoAno = function() {
  this.anoCorrente++;
  this.exibeAnoMes(this.anoCorrente, this.mesCorrente);
  this.Make();
}

oCalendar.anteriorAno = function() {
  this.anoCorrente--;
  this.exibeAnoMes(this.anoCorrente, this.mesCorrente);
  this.Make();
}

oCalendar.exibeAnoMes = function(ano, mes) {
	if (this.tagAno) {
		this.tagMes.innerHTML = this.nomesMes[mes];
		this.tagAno.innerHTML = ano;
	} else {
		this.tagMes.innerHTML = this.nomesMes[mes] + ' ' + ano;
	}
}


oCalendar.limpaCalendario = function() {
	// Remove all other rows available.
	while (this.tagCalendario.rows.length > 0)
		this.tagCalendario.deleteRow(0);
}

oCalendar.ajustaFevereiro = function() {
	Ano = this.anoSelecionado;
	Mes = this.mesSelecionado;
	if (Mes == 1) {
		this.diasMes[1] = ((Ano % 400 == 0) || ((Ano % 4 == 0) && (Ano % 100 != 0))) ? 29 : 28;
	}
}

oCalendar.Make = function() {
	var primeiraData = new Date(this.anoCorrente, this.mesCorrente, 1);
	var primeiroDia = primeiraData.getDay() + 1;

	this.ajustaFevereiro();

	Mes = this.mesCorrente;
	Ano = this.anoCorrente;

	if (((this.diasMes[Mes] == 31) && (primeiroDia >= 6)) || ((this.diasMes[Mes] == 30) && (primeiroDia == 7))) {
		var Rows = 6;
	} else if ((this.diasMes[Mes] == 28) && (primeiroDia == 1)) {
		var Rows = 4;
	} else {
		var Rows = 5;
	}

	this.exibeAnoMes(Ano, Mes);

	var contadorDias = 1;
	var contadorLoops = 1;
	var contadorEventos = 0;

	this.limpaCalendario();

	for (var j = 1; j <= Rows; j++) {
		var oRow = this.tagCalendario.insertRow( -1 );

		for (var i = 1; i < 8; i++) {
			var oCell = oRow.insertCell( -1 );

			if ((contadorLoops >= primeiroDia) && (contadorDias <= this.diasMes[Mes])) {
				// Marca data atual
				if (txt = this.dataEspecial(Ano, Mes, contadorDias)) {
					if (this.fncConteudoCelula != undefined) {
						oCell.innerHTML = this.fncConteudoCelula(this.formataNumero(contadorDias), this.formataNumero(Mes+1), Ano, txt);
					} else {
						oCell.innerHTML = contadorDias;
					}
					contadorEventos++;
				} // Marca dia atual
				else if ((contadorDias == this.hoje) && (Ano == this.anoCorrente) && (Mes == this.mesCorrente)) {
					oCell.className = this.classAtual;
					oCell.innerHTML = contadorDias;
				} // Marca dia com evento
				else {
					oCell.className = this.classNormal;
					oCell.innerHTML = contadorDias;
				}
				contadorDias++;
			} // Marca dia de outro mes
			else {
				oCell.innerHTML = ' ';
			}
			contadorLoops++;
		}
	}
}

oCalendar.dataEspecial = function(ano, mes, dia) {
	mes = parseFloat(mes) + 1;
	ano = this.formataNumero(ano);
	mes = this.formataNumero(mes);
	dia = this.formataNumero(dia);
//	alert(mes)
//	alert(dia + '/' + parseFloat(mes+1) + '/' + ano);
	var texto = this.aDatas[dia + '/' + (mes) + '/' + ano ];
	if (texto)
		return texto;
	else
		return false;
}

oCalendar.proximo = function(Sentido) {

	if (Sentido == '+') {
		if (this.desvio == 1)
			return ;
		if (this.Selected_Month == 11) {
			this.Selected_Month = 0;
			this.Selected_Year++;
		} else {
			this.Selected_Month++;
		}
		this.desvio = this.desvio + 1;
	} else {
		if (this.desvio == 0)
			return ;
		if (this.Selected_Month == 0) {
			this.Selected_Month = 11;
			this.Selected_Year--;
		} else {
			this.Selected_Month--;
		}
		this.desvio = this.desvio - 1;
	}

	this.Clear();
	this.Make();
}

oCalendar.MM_findObj = function(n, d) {
	//v4.01
	//---- Macromedia MM_findObj finds element (document.all, document.getElementById, etc.)
	var p, i, x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

oCalendar.formataNumero = function (num)
{
	// Create a string object to do our formatting on
	var tmpNumStr = new String(num);

	// See if we need to strip out the leading zero or not.
	if (num >= 1 && num <= 9)
		if (tmpNumStr.length == 1)
			tmpNumStr = '0' + tmpNumStr;

	return tmpNumStr;		// Return our formatted string!
}
