/*
 * Agenda
 * 
 * Version: 	$Revision: 1.0 $
 * Author:  Antonio Hernández Ritoré 
 */
	
function Agenda(){
// 	Definicion de atributos
	this.eventos = new Array();
	this.actualidad = new Array();
		
//	Definicion de metodos
	this.addEvento = addEvento;
	this.remEvento = remEvento;

	this.addNoticia = addNoticia;
	this.remNoticia = remNoticia;
	
	this.renderActuaciones = renderActuaciones;
	this.renderAgenda = renderAgenda;
	this.renderActualidad = renderActualidad;
}

function addEvento(evento){
	this.eventos.push(evento);
}

function remEvento(titulo){
}


function renderActuaciones(){

	this.eventos.sort(compareEventoAsc);
	// falta renderizar por meses
	// listar por estado
	for(i=0;i<this.eventos.length;i++){
		evt = this.eventos[i];
		if(evt.estado=="AC" || evt.estado=="TD"){
			document.write(evt.toString());
		}
	}
}

function renderAgenda(){

	this.eventos.sort(compareEventoDesc);
	// falta renderizar por meses
	// listar por estado
	document.write("<TABLE width=\"90%\">");
	m = -1;
	y = -1;
	ancla = true;
	for(i=0;i<this.eventos.length;i++){
		evt = this.eventos[i];
		if(evt.estado=="AG" || evt.estado=="TD"){
			if(evt.ano!=y){
				document.write("<TR><TD colspan=2><H1>" + evt.ano + "</H1></TD></TR>");
				y = evt.ano;
			}
		
			if(evt.mes!=m){
				document.write("<TR><TD colspan=2><H5>" + getNombreMes(evt.mes) + "</H5></TD></TR>");
				m = evt.mes;
			}
			document.write("<TR class=\"agitem\">");
			document.write("<TD class=\"agdia\">");
			document.write(evt.dia);
			document.write("</TD>");
			document.write("<TD>" + evt.toAgenda() + "</TD>");
			document.write("</TR>");
		}
	}
	document.write("</TABLE>");
}

function renderActualidad(){

	for(i=0;i<this.actualidad.length;i++){
		noticia = this.actualidad[i];
		document.write(noticia.nToString());
	}
}


function addNoticia(noticia){
	this.actualidad.push(noticia);
}

function remNoticia(titulo){
}


function compareEventoAsc(e1, e2){

	f1 = e1.ano + e1.mes + e1.dia;
	f2 = e2.ano + e2.mes + e2.dia;
	h1 = e1.hora;
	h2 = e2.hora;

	if(f1 > f2){
		return 1;
	}else if(f1 < f2){
		return -1;
	}else if(f1 == f2){
		if(e1.hora > e2.hora){
			return 1;
		}else if(e1.hora < e2.hora){
			return -1;
		}else{
			return 0;
		}
	}
}

function compareEventoDesc(e1, e2){

	f2 = e1.ano + e1.mes + e1.dia;
	f1 = e2.ano + e2.mes + e2.dia;
	h2 = e1.hora;
	h1 = e2.hora;

	if(f1 > f2){
		return 1;
	}else if(f1 < f2){
		return -1;
	}else if(f1 == f2){
		if(e1.hora > e2.hora){
			return 1;
		}else if(e1.hora < e2.hora){
			return -1;
		}else{
			return 0;
		}
	}
}


var meses = new Array();
	meses[0] = "ENERO";
	meses[1] = "FEBRERO";
	meses[2] = "MARZO";
	meses[3] = "ABRIL";
	meses[4] = "MAYO";
	meses[5] = "JUNIO";
	meses[6] = "JULIO";
	meses[7] = "AGOSTO";
	meses[8] = "SEPTIEMBRE";
	meses[9] = "OCTUBRE";
	meses[10] = "NOVIEMBRE";
	meses[11] = "DICIEMBRE";

	
function getNombreMes(m){
	return meses[eval(m)-1];
}

