//Devuelves los días del mes especificado, teniendo en cuenta los años bisiestos
function diasDelMes(mes,anho) {
	var dias = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (mes != 2) return dias[mes - 1];
	if (anho%4 != 0) return dias[1];
	if (anho%100 == 0 && anho%400 != 0) return dias[1];
	return dias[1] + 1;
}

//Añade ceros hasta llenar un número (en cadena) de 2 cifras
function ponerCeros(cadena) {
	return (cadena.length==1) ? "0" + cadena:""+cadena;
}

//Clase principal con métodos dentro
function Calendario(contenedor,nombreObjeto) {
	//Eventos: por ejemplo eventos[20080420] = "Reunión" (indicaría 20/4/2008)
	this.eventos = new Array();	
	this.contenedor = contenedor;
	this.nombreObjeto = nombreObjeto;

	this.mover=function mover(avance) {
		//OJO: JAVASCRIPT MANEJA MESES EMPEZANDO DESDE 0
		siguienteMes = (this.mes + avance) % 12;
		siguienteMes = (siguienteMes == 0) ? 12:siguienteMes;
		if (avance < 0) siguienteAnho = (siguienteMes == 12) ? this.anho - 1 : this.anho;
		else siguienteAnho = (siguienteMes == 1) ? this.anho +1 : this.anho;
		//else siguienteAnho = (siguienteMes == 1) ? this.anho-1:this.anho;
		this.imprimir(siguienteMes,siguienteAnho);
	};

	this.imprimir=function imprimir(mes,anho) {
		this.mes = mes;
		this.anho =anho;
		var meses = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
		fecha = new Date(anho,mes-1,1);
		salida = "<table cellspacing='0' border='0' class='calendario'>";
		salida = salida + "<thead><tr style=\"background: silver\"><th style=\"background: silver\"><a href='#' onClick='" + this.nombreObjeto + ".mover(-1)'><img src='imgsrc/izqda.png' border='0'></a></th><th colspan='5' style=\"background: silver; color: #333\">" + meses[mes-1] + " de " + anho + "</th><th style=\"background: silver\"><a href='#' onClick='" + this.nombreObjeto + ".mover(1)'><img src='imgsrc/dcha.png' border='0'></a></th></tr>";
		salida = salida + "<tr><th>Lu</th><th>Ma</th><th>Mi</th><th>Ju</th><th>Vi</th><th>Sa</th><th>Do</th></tr></thead><tbody><tr>";

		//Bucle para imprimir cada día del mes (una celda de tabla por día)
		for (i=1; i<diasDelMes(mes,anho)+1; i++) {

			//Celdas en blanco antes del primer día del mes
			primerDia = fecha.getDay();
			primerDia = (primerDia == 0) ? 7:primerDia; //Para JavaScript, el primer día de la semana es Domingo. Esto lo pone como último
			if (i == 1) {
				for (j=0; j<primerDia-1; j++)
					salida = salida + "<td></td>";
			}

			if (i !=1 && fecha.getDay() == 0) {
				salida = salida + "</tr><tr>"; //Inicio de fila (lunes)
			}

			//Imprime la celda con el número y el enlace al evento, si hay
			salida = salida + "<td>";
			hayEvento = false;
			if (this.eventos[parseInt(anho+ponerCeros(""+mes)+ponerCeros(""+i))]) {
				hayEvento=true;
				enlace = this.eventos[parseInt(anho+ponerCeros(""+mes)+ponerCeros(""+i))][0];
				salida = salida + "<a style='background: pink' class='tt2' href='" + ((enlace) ? enlace:"#") + "'><span class='tooltip2' style='width: 200px; top: 5px; left: 8px'><span class=\"middle2\" style=\"text-decoration: none\">" + this.eventos[parseInt(anho+ponerCeros(""+mes)+ponerCeros(""+i))][1] + "</span></span>";
				
			}
			salida = salida + i;
			if (hayEvento) salida = salida + "</a>";
			salida = salida + "</td>";

			fecha=new Date(anho,mes-1,i); //Reescribe la fecha para el bucle
		}

		salida = salida + "</tr></tbody></table>"; //Imprime la celda con el número
		this.contenedor.innerHTML = salida;
		this.contenedor.style.width=contenedor.getElementsByTagName("table")[0].offsetWidth + "px";
	};

	this.agregarEvento=function agregarEvento(eDia,eMes,eAnho,descripcion,enlace) {
		this.eventos[parseInt(eAnho+ponerCeros(""+eMes)+ponerCeros(""+eDia))] = new Array(enlace, descripcion);
	};
}
