// JavaScript Document

// Calendario "Laboratorio de Ágora"
// Programa principal

// Copyright 2009 El Laboratorio de Agora
// Copyright 2009 Gonzalo Esteban
// Contacto: eolo.travis.kun@gmail.com

// Acuerdo de licencia GNU GPL

// This file is part of Calendario "Laboratorio de Ágora".
//
// Calendario "Laboratorio de Ágora" is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Calendario "Laboratorio de Ágora" is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Calendario "Laboratorio de Ágora".  If not, see <http://www.gnu.org/licenses/>.



// Calendario

function calendario(hoy, act, taller, salida) {
// Captura de la fecha
	hoy_dia = hoy.getDate();
	hoy_mes = hoy.getMonth();
	hoy_sem = hoy.getDay();
	hoy_yar = hoy.getFullYear();
	
// Cadenas de valores
	var meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
	var numdias = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
	var diasmes = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
// Calcular en que día de la semana empieza el mes
	var inic_mes = hoy_sem;
	for(j = hoy_dia; j > 1; j--) {
		if(inic_mes == 0) {inic_mes = 7};
		inic_mes = inic_mes - 1
	}
// Cambio al orden de fechas español
	if(inic_mes == 0) {inic_mes = 6}
	else {inic_mes = inic_mes - 1}
	
// Creacion del calendario
	var enlaces = new Array();
	var dia_semana = inic_mes - 1
	for(i in numdias) {
		if(i == diasmes[hoy_mes] + 1) {break;}
		if(i == act[0][2]) {
			enlaces[i-1] = "<td class=\"negrita1\" title=\"" + act[0][0] + "\"><a href=\"" + act[0][1]  + "\" class=\"enlaceCalendario\">" + act[0][2] + "</a></td>"
			act.shift();
			dia_semana = dia_semana + 1
			continue;
		}
		if(i == taller[0][2]) {
			enlaces[i-1] = "<td class=\"negrita2\" title=\"" + taller[0][0] + "\"><a href=\"" + taller[0][1]  + "\" class=\"enlaceCalendario\">" + taller[0][2] + "</a></td>"
			dia_semana = dia_semana + 1
			taller.shift();
			continue;
		}
		if(i == salida[0][2]) {
			enlaces[i-1] = "<td class=\"negrita3\" title=\"" + salida[0][0] + "\"><a href=\"" + salida[0][1]  + "\" class=\"enlaceCalendario\">" + salida[0][2] + "</a></td>"
			salida.shift();
			dia_semana = dia_semana + 1
			continue;
		}
		if(dia_semana == 7) {dia_semana = 0}
		if(dia_semana == 6) {
			enlaces[i-1] = "<td class=\"domingo\" >" + i  + "</td>"
			dia_semana = 0
			continue;
		}
		enlaces[i-1] = "<td class=\"normal\" >" + i  + "</td>"
		dia_semana = dia_semana + 1

	}		
	
// Numero de celdas
	var num_celdas = inic_mes + diasmes[hoy_mes] + 7 - (inic_mes + diasmes[hoy_mes]) % 7
	
// La tabla
	document.write("<table class=\"calendario\" align=\"center\">");
	document.write("<tr><td colspan=\"7\" class=\"titulo\">" + meses[hoy_mes] + " " + hoy_yar +  "</td></tr>");
	document.write("<tr><td class=\"normal\">L</td><td class=\"normal\">M</td><td class=\"normal\">X</td><td class=\"normal\">J</td><td class=\"normal\">V</td><td class=\"normal\">S</td><td class=\"domingo\">D</td></tr>");
	
	for(i = 0; i < num_celdas; i++) {
		if(i % 7 == 0) {document.write("<tr>");}
		if(i < inic_mes || i > inic_mes + diasmes[hoy_mes] - 1) {
			document.write("<td class=\"vacio\">&nbsp;</td>");
			continue;
		}
		if(i % 7 == 6) {
			document.write(enlaces[i-inic_mes] + "</tr>");
			continue;
		}
		document.write(enlaces[i-inic_mes]);
	}	
}

function leyenda() {
	document.write("</table>");	
	document.write("<div align=\"center\"><table class=\"leyenda\" align=\"center\">");
	document.write("<tr><td colspan=\"2\" class=\"titulo\">Leyenda</td></tr>");
	document.write("<tr><td class=\"negrita1\">A</td><td class=\"normal\">Actividades</td></tr>");
	document.write("<tr><td class=\"negrita2\">T</td><td class=\"normal\">Talleres</td></tr>");
	document.write("<tr><td class=\"negrita3\">S</td><td class=\"normal\">Salidas</td></tr>");
	document.write("</table></div>");
}