NoticiasPortada = {
	ContenedorNoticias: 'noticias-portada',
	BotonListado: 'noticias-boton-listado',
	OcultarBotonListado: false,
	TituloNoticiasMes: 'titulo-otras-noticias',
	BarraMeses: 'noticias-listado-meses',
	URLPeticion: 'noticias',
	TextoIdiomas: Object,
	cargaEventos: function() {
		var esto = NoticiasPortada;
		esto.ContenedorNoticias = $(esto.ContenedorNoticias);
		esto.BotonListado = $(esto.BotonListado);
		esto.TituloNoticiasMes = $(esto.TituloNoticiasMes);
		esto.BarraMeses = $(esto.BarraMeses);
		switch ( esto.ContenedorNoticias.getAttribute('idioma') ) {
			case 'en':
				esto.TextoIdiomas.ErrorSinNoticias = 'No news for this month.';
				esto.TextoIdiomas.BotonVerMas = 'more +';
			break;
			default:
				esto.TextoIdiomas.ErrorSinNoticias = 'No hay noticias para este mes.';
				esto.TextoIdiomas.BotonVerMas = 'ver +';
			break;
		}
		esto.pideContenido('js=');
	},
	pideContenido: function(enlace) {
		var esto = NoticiasPortada;
		var jsonRequest = new Request.JSON({
			url: esto.URLPeticion , 
			data: enlace,
			onSuccess: function(transport){
				if ( transport != null ) {
					esto.cargaBarraMeses(transport.fechas);
					esto.cargaNoticias(transport.noticias);
					esto.cargaTituloMes(transport.mesActual);
				}
			}, 
			onFailure: function(xhr) {
			}, 
			onException: function(headerName, value) {
			}
		});
		jsonRequest.post();
	},
	cargaBarraMeses: function(Meses) {
		var esto = NoticiasPortada;
		esto.BarraMeses.empty();
		
		var contenedor = new Element('p', {});
		
		for (i=0 ; i<Meses.length ; i++ ) {
			var elemento = new Element('span', {
				'class':'boton-mes',
				'enlace': Meses[i].Enlace,
				'html': Meses[i].Nombre
			});
			contenedor.adopt(elemento);
			if ( i+1 <Meses.length ) {
				contenedor.appendText(' | ');
			}
		}
		
		esto.BarraMeses.adopt(contenedor);
		esto.cargaEventosMeses();
	},
	clickMes: function(enlace) {
		var esto = NoticiasPortada;
		esto.pideContenido(enlace);
	},
	cargaTituloMes: function(Texto) {
		var esto = NoticiasPortada;
		esto.TituloNoticiasMes.innerHTML = Texto;
	},
	cargaEventosMeses: function() {
		var esto = NoticiasPortada;
		$$('#noticias-listado-meses .boton-mes').each(function(s,index) {
			s.setStyles({cursor:'pointer'});
			s.addEvent('click',function(e) {
				esto.clickMes(s.getAttribute('enlace'));
			});
		});
	},
	cargaEventosNoticias: function() {
		var esto = NoticiasPortada;
		$$('#noticias-portada .noticia-titulo').each(function(s,index) {
			s.setStyles({cursor:'pointer'});
			s.addEvent('click',function(e) {
				esto.clickNoticia(s.getAttribute('n'));
			});
		});
	},
	clickNoticia: function(n) {
		var esto = NoticiasPortada;
		$$('.noticia').each(function(s,index) {
			if ( n != s.getAttribute('n') ) {
				esto.toggleNoticia(s);
			}
		});
		$$('.noticia-texto').each(function(s,index) {
			if ( n == s.getAttribute('n') ) {
				esto.toggleTextoNoticia(s);
			}
		});
		if ( esto.OcultarBotonListado ) {
			esto.toggleBotonListado();
		}
	},
	toggleTextoNoticia: function(contenedor) {
/*		var EfectoTextoNoticia = new Fx.Slide(contenedor);
		EfectoTextoNoticia.toggle();*/
		if ( contenedor.getStyle('display') == 'none' ) {
			contenedor.setStyles({display:'block'});
		}
		else {
			contenedor.setStyles({display:'none'});
		}
	},
	toggleNoticia: function(contenedor) {
/*		var EfectoNoticia = new Fx.Slide(contenedor);
		EfectoNoticia.toggle();*/
		if ( contenedor.getStyle('display') == 'none' ) {
			contenedor.setStyles({display:'block'});
		}
		else {
			contenedor.setStyles({display:'none'});
		}
	},
	toggleBotonListado: function() {
		var esto = NoticiasPortada;
		if ( esto.BotonListado.getStyle('display') == 'none' ) {
			esto.BotonListado.setStyles({display:'block'});
		}
		else {
			esto.BotonListado.setStyles({display:'none'});
		}		
	},
	limpiaContenedorNoticias: function() {
		var esto = NoticiasPortada;
		esto.ContenedorNoticias.empty();
	},
	cargaNoticias: function(Noticias) {
		var esto = NoticiasPortada;
		
		esto.limpiaContenedorNoticias();
		
		if ( Noticias.length > 0 ) {
			for (var i=0 ; i<Noticias.length; i++ ) {
				esto.ContenedorNoticias.adopt(esto.creaLineaNoticia(Noticias[i],i));
			}
			esto.cargaEventosNoticias();
		}
		else {
			esto.ContenedorNoticias.adopt(Element('p', {								  
				'class':'centrado',
				'html':esto.TextoIdiomas.ErrorSinNoticias
			}));
		}
	},
	creaLineaNoticia: function(Noticia,indice) {
		var esto = NoticiasPortada;
		var contenedor = new Element('div', {
			'n':indice,
			'class':'noticia'
		});
		
		var titulo = new Element('div',{
			'n':indice,
			'class':'noticia-titulo',
			'html':'<h3>'+Noticia.Titulo+'</h3>'
		});
		
		var texto = new Element('div',{
			'n':indice,
			'class':'noticia-texto',
			'styles': {
				'display':'none'
			},
			'html':'<p>'+Noticia.Resumen+'</p><p><a href=noticia?id='+Noticia.IdNoticia+' target="_self" class="enlace-mas derecha"><span><span>'+esto.TextoIdiomas.BotonVerMas+'</span></span></a></p>'
		});
		
		contenedor.adopt(titulo);
		contenedor.adopt(texto);
		
		return contenedor;
	}
}

window.addEvent('domready',NoticiasPortada.cargaEventos);
