/**
 * banner.js
 *
 * @author Iván Rodríguez
 * @date 2009-06-30
 * @brief Clase que controla los banners de la home
 * @version 0.1
 */
var Banner = new Class({
	Implements 	: [Options],
	
	previousButton 	: 'previousButton',
	nextButton	: 'nextButton',
	container	: 'container',
	items		: 'Item',
	index 		: 0,
	currentIndex	: this.index,
	previousIndex 	: this.index,
	transitionTime	: 1100,
	transitionType	: 'cubic:out',	
		
	initialize : function(options) {
		this.setOptions(options);
		
		this.previousButton  = (options.previousButton) ? 
		$(options.previousButton) : $(this.previousButton);
		
		this.nextButton = (options.nextButton) ? 
		$(options.nextButton) : $(this.nextButton);
		
		this.container = (options.container) ? 
		$(options.container) : $(this.container);
		
		this.previousButton.addEvent('click', this.onPrevious.bindWithEvent(this));
		this.nextButton.addEvent('click', this.onNext.bindWithEvent(this));
		
		this.items = $$('.' + this.items);
		
		this.move();
	},
	
	onPrevious : function(e) {
		e.stop();
		
		if(this.index > 0) {
			this.previousIndex = this.index;
			this.index = this.index - 1;
			this.currentIndex = this.index;
	
			this.move();
		}
	},
	
	onNext : function(e) {
		e.stop();
		
		//console.log(this.items.length);
				
		if(this.index < (this.items.length - 1) && this.index >= 0) {
			this.previousIndex = this.index;
			this.index = this.index + 1;
			this.currentIndex = this.index;
			
			this.move();
		}
	},
	
	move : function() {
		// Ocultamos la capa previa
		var itemOut = new Fx.Morph(this.items[this.previousIndex], {
			duration: this.transitionTime, 
			transition: this.transitionType,
			link: 'ignore'
		}).start({
			'opacity':[1, 0]
		});
		this.items[this.previousIndex].set({
			'styles' : {
				'display' : 'none'
			}
		});
		
		// Mostramos la capa actual
		var itemIn = new Fx.Morph(this.items[this.currentIndex], {
			duration: this.transitionTime, 
			transition: this.transitionType,
			link: 'ignore'
		}).start({
			'opacity':[0, 1]
		});
		this.items[this.currentIndex].set({
			'styles' : {
				'display' : 'block'
			}
		});
	}
});
