/**
 *	Prototype FeatureList
 *	Author:		Ulises Tirado Zatarain
 *	Version:	1.0.0 (2010.02.05)
 *	Requires:	Prototype v1.5+ and Script.aculo.us
 *	Copyright (c) Miguel Hidalgo
 *	Licensed under BSD License.
 */

Event.observe(window,'load',function(){
	var lists = $$('.features');
	lists.each(function(list){
		eval('window.'+list.getAttribute('id')+' =  new FeatureList(list);');
	});
});
var FeatureList = Class.create();
FeatureList.prototype = {
	initialize:function(list){
		var news = list.getElementsBySelector('ul.news');
		if(news.size() > 0){
			this.news = news[0].getElementsBySelector('li');
		}else{
			throw 'No se encontró la lista de noticias.';
		}
		var images = list.getElementsBySelector('ul.images');
		if(images.size() > 0){
			this.images = images[0].getElementsBySelector('li');
		}else{
			throw 'No se encontró la lista de imagenes.';
		}
		if(this.news.size() != this.images.size()){
			throw 'La lista de noticias no corresponde con la lista de imagenes.';
		}
		this.element = list;
		this.current = -1;
		this.play();
	},
	play:function(){
		var old = this.current;
		this.current = (this.current + 1) % this.news.size();
		var $this = this;
		if(old >= 0){
			new Effect.Fade($this.images[old],{
				afterFinish:function(){
					$this.news[old].removeClassName('current');
					$this.news[$this.current].addClassName('current');
					new Effect.Appear($this.images[$this.current],{
						afterFinish:function(){
							setTimeout('window.'+$this.element.getAttribute('id')+'.play();',5000);
						}
					});
				}
			});
		}else{
			this.news[this.current].addClassName('current');
			for(var k = 1; k < this.images.size(); k++){
				this.images[k].style.display = 'none';
			}
			new Effect.Appear(this.images[this.current],{
				afterFinish:function(){
					setTimeout('window.'+$this.element.getAttribute('id')+'.play();',5000);
				}
			});
		}
	}
};
