
Event.observe(window, 'load', function() {
	New.viewer = new NewsViewer('newsviewer');
	New.Templates = {
		list: '<li id="new$id" class="new"><a href="$link"><span class="date">$date</span> <span class="title">$title</span></a></li>',
		detail: '<div class="new"><div class="image"><img src="upload/noticias/max/$photo" alt="" /></div><div class="info"><span class="date">$date</span><h2 class="title">$title</h2><p class="description">$description</p><p class="link"><a href="$link" class="highlighted"><span>Leer más +</span></a></p></div></div>'
	};
	
	// Pillamos la noticia remarcada
	var highlighted = New.viewer.getNew();
	if (!highlighted) return;

	// Metemos la noticia remarcada en la lista
	var list = $('newslist');
	if (!list) return;
	list = list.getElementsByClassName('list')[0];
	list.innerHTML = highlighted.fillTemplate(New.Templates.list) + list.innerHTML;

	// Para evitar ids repetidos le quitamos el id a la noticia remarcada
	$('new'+highlighted.id).removeAttribute('id');

	// Marcamos la noticia remarcada como seleccionada en la lista
	new New(highlighted.id).setSelected(true);

	// Recorremos las noticias de la lista cambiando los enlaces
	for(var item in newslist) {
		var links = $('new'+item).getElementsByTagName('a');
		for (var i=0; i<links.length; ++i) {
			links[i].newId = item;
			links[i].onclick = function() {
				this.blur();
				New.viewer.display(this.newId);
				return false;
			}
		}
	}
});

var New = Class.create();

New.prototype = {

	id: null,
	title: null,
	description: null,
	date: null,
	photo: null,
	link: null,

	initialize: function(id) {
		var newinfo = newslist[id];
		if (!newinfo) return null;
		
		this.id = id;
		this.title = newinfo['title'];
		this.description = newinfo['description'];
		this.date = newinfo['date'];
		this.photo = newinfo['photo'];
		this.link = newinfo['link'];
	},

	setSelected: function(selected) {
		if (!this.id) return false;
		if (selected) {
			if (!this.isSelected()) {
				if (New.selectedNew != null) New.selectedNew.setSelected(false);
				$('new'+this.id).addClassName('selected');
				New.selectedNew = this;
			}
		} else {
			$('new'+this.id).removeClassName('selected');
			New.selectedNew = null;
		}
		return true;
	},

	isSelected: function() {
		if (!this.id) return false;
		return $('new'+this.id).hasClassName('selected');
	},

	fillTemplate: function(template) {
		template = template.replace(/\$id\b/i, this.id, template);
		template = template.replace(/\$title\b/i, this.title, template);
		template = template.replace(/\$description\b/i, this.description, template);
		template = template.replace(/\$date\b/i, this.date, template);
		template = template.replace(/\$photo\b/i, this.photo, template);
		template = template.replace(/\$link\b/i, this.link, template);
		return template;
	}
}

New.viewer = null;
New.selectedNew = null;


var NewsViewer = Class.create();

NewsViewer.prototype = {

	adjusting: false,
	displaying: null,

	initialize: function(id) {
		this.element = $(id);
		if (!this.element) return null;

		var highlighted = this.element.getElementsByClassName('new')[0].id;
		if (!highlighted || !highlighted.startsWith('new')) return;
		this.displaying = new New(highlighted.substring(3));
	},

	isDisplaying: function(id) {
		return this.displaying.id == id;
	},

	getNew: function() {
		return this.displaying;
	},

	display: function(id) {
		var el = this;
		if (this.adjusting || this.isDisplaying(id)) return false;

		var _new = new New(id);
		this.adjusting = true;
		this.element.innerHTML += _new.fillTemplate(New.Templates.detail);
		new Effect.Move(this.element, {
			x: -$('new'+id).getWidth(), y: 0, mode: 'relative',
			transition: Effect.Transitions.sinoidal,
			duration: .5,
			fps: 15,
			afterFinish: function() { // Borramos la primera noticia y reseteamos la posicion
				el.element.getElementsByClassName('new')[0].remove();
				el.element.setStyle({ right: 0, left: 0 })
				_new.setSelected(true);
				el.displaying = _new;
				el.adjusting = false;
			}
		});
	}
}