/**************************************************************
	Programador: FRAN
	Fecha: 25/01/2006
	Clase: CalendarioHTML
	Descripción: genera un canlendario HTML para 
				 imprimirlo en un document
 **************************************************************/

document.write('<style type="text/css">');
document.write('.dia-hoy{ border-style:solid; border-width:1px; border-color:#000000; font-weight:bold; }');
document.write('.dia-festivo{ color:#FF0000; font-weight:bold; }');
document.write('</style>');

// Constructor
function CalendarioHTML(date, festivos){
	
	if(!date) date = new Date();
	
	if(date instanceof Date){
		this.__anyo = date.getFullYear();
		this.__hoy = date;
	}
	else if(typeof(date) == "number"){
		if(date < 1900)
			throw new Error("El año no puede ser menor de 1900");
		else{
			this.__anyo = date;
			this.__hoy = null;
		}
	}
	
	this.setFestivos(festivos);
}




CalendarioHTML.prototype = {
	
	// Métodos públicos
	getAnyo: function(){ return this.__anyo; },
	
	getMesHoy: function(){ return (this.__hoy ? this.__hoy.getMonth()+1 : null); },
	
	getDiaHoy: function(){ return (this.__hoy ? this.__hoy.getDate() : null); },
	
	getHoy: function(){ return this.__hoy; },
	
	setFestivos: function(festivos){ 
		if(festivos){
			if(festivos instanceof Dictionary)
				this.__festivos = festivos;
			else
				throw new Error("Error en el parámetro festivos");
		}
		else
			this.__festivos = null;
	},
	
	printCalendario: function(){
		document.write(this.getAnyoHTML());	
	},
	
	getAnyoHTML: function(){
		var anyoHTML = '';
		
		anyoHTML += '<table id="CalendarioHTML" width="100%" border="0" cellspacing="0" cellpadding="0">';
		anyoHTML += '<tr>';
		anyoHTML += '<td align="center"><strong>' + this.getAnyo() + '</strong></td>';
		anyoHTML += '</tr>';
		anyoHTML += '<tr>';
		anyoHTML += '<td>';
		anyoHTML += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		
		var mesAnyo = 1;
		for(var i = 0; i < 4; i++){
			anyoHTML += '<tr>';
			for(var j = 0; j < 3; j++){
				anyoHTML += '<td>' + this.getMesHTML(mesAnyo) + '</td>';
				mesAnyo++;
			}
			anyoHTML += '</tr>';
		}
		anyoHTML += '</table>';
		anyoHTML += '</td>';
		anyoHTML += '</tr>';
		anyoHTML += '</table>';
		
		return anyoHTML;

	},
	
	getMesHTML: function(mes){
		if(!mes && !this.__hoy)
			throw new Error("No se ha especificado el més.");
			
		var mes = (mes ? mes : this.getMesHoy())-1;
		var meses = [
					 	'Enero', 
						'Febrero', 
						'Marzo', 
						'Abril', 
						'Mayo', 
						'Junio', 
						'Julio', 
						'Agosto', 
						'Septiembre', 
						'Octubre', 
						'Noviembre', 
						'Diciembre'
					];

		var dia = this.__hoy ? ((mes == this.__hoy.getMonth()) ? this.__hoy.getDate() : null) : null;
		var aMes = this.__getArrayMes(mes);
		var mesHTML = '';
		
		mesHTML += '<table width="100%" border="1" cellspacing="0" cellpadding="0">';
		mesHTML += '<tr>';
		mesHTML += '<td align="center"><strong>' + meses[mes] + '</strong></td>';
		mesHTML += '</tr>';
		mesHTML += '<tr>';
		mesHTML += '<td>';
		mesHTML += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		mesHTML += '<tr>';
		mesHTML += '<td align="center">LU</td>';
		mesHTML += '<td align="center">MA</td>';
		mesHTML += '<td align="center">MI</td>';
		mesHTML += '<td align="center">JU</td>';
		mesHTML += '<td align="center">VI</td>';
		mesHTML += '<td align="center">SA</td>';
		mesHTML += '<td align="center">DO</td>';
		mesHTML += '</tr>';
		
		var id = '';
		var css = '';
		var pre = '';
		var pos = '';
		var diaMes = 0;
		for(var s = 0; s < aMes.length; s++){
			mesHTML += '<tr>';
			for(var d = 0; d < 7; d++){
				if(aMes[s][d]){
					diaMes++;
					if(this.__esFestivo(diaMes, mes+1) || d == 6)
						css += 'dia-festivo ';
					if(diaMes == dia) //señalamos el día de hoy
						css += 'dia-hoy';

					id = 'id_' + diaMes + '_' + (mes+1) + '_' + this.__anyo;
				}
				
				mesHTML += '<td align="center"><div id="' + id + '" class="' + css + '">' + (aMes[s][d] ? aMes[s][d] : '&nbsp;' ) + '</div></td>';
				css = '';
				id = '';
			}
			mesHTML += '<tr>';
		}
		
		mesHTML += '</table>';
		mesHTML += '</td>';
		mesHTML += '</tr>';
		mesHTML += '</table>';
		
		return mesHTML;
		
	},
	
	esBisiesto: function(anyo){
		anyo = anyo ? anyo : this.getAnyo();
		if((anyo % 4 == 0 && anyo % 100 != 0) || (anyo % 400 == 0))
			return true;
		else
			return false;
	},
	
	//Métodos privados
	__getArrayMes: function(mes){
		
		var aMes = [  // L  M  M  J  V  S  D
						[0, 0, 0, 0, 0, 0, 0], 
						[0, 0, 0, 0, 0, 0, 0], 
						[0, 0, 0, 0, 0, 0, 0], 
						[0, 0, 0, 0, 0, 0, 0], 
						[0, 0, 0, 0, 0, 0, 0], //pudiera estár vacía (solo en febrero)
						[0, 0, 0, 0, 0, 0, 0]  //pudiera estar vacía
					];
		var traductorDia = [6, 0, 1, 2, 3, 4, 5];
		var diasMeses = [31, (this.esBisiesto() ? 29 : 28 ), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		var primerDia = traductorDia[(new Date(this.getAnyo(), mes, 1)).getDay()];
		var diasMes = 1;
		
		iteraciones:
		for(var semana = 0; semana < 6; semana++){
			for(var dia = primerDia; dia < 7; dia++){
				aMes[semana][dia] = diasMes;
				if(++diasMes > diasMeses[mes]) break iteraciones;
			}
			primerDia = 0;
		}
		
		this.__reduceArrayMes(aMes);
		return aMes;
	},
	
	__getArrayAnyo: function(){
		
		var aAnyo = [ // E   F   M
						[], [], [], 
						
					  // A   M   J
						[], [], [], 
						
					  // J   A   S
						[], [], [], 
						
					  // O   N   D
						[], [], [] 
					];
		
		for(var mes = 0; mes < 12; mes++)
			aAnyo[mes] = this.__getArrayMes(mes);
		
		return aAnyo
		
	},
	
	__reduceArrayMes: function(aMes){
		if(!aMes[5][0]) aMes.splice(5, 1);
		if(!aMes[4][0]) aMes.splice(4, 1);
	},
	
	__esFestivo: function(dia, mes){
		if(this.__festivos)
			return this.__festivos.Exist(dia+'/'+mes);
		else
			return false;
	}
}