BLCMS.Widgets.Slideshow=new Class({
	Implements: [Options],

	options: {
		width:		400,
		height:		100,
		
		pause:		5000,
		transform:	1000,
		transfirst:	false
	},

	count: null,
	current: 0,
	
	images: null,
	selectors: null,
	
	timer: null,
	
	initialize: function(slideshow, options) {
		this.setOptions(options);

		this.slideshow=slideshow;
		this.images=slideshow.getElements('.images li');

		this.count=this.images.length;
		
		this.images.each(function(el, i) {
			if (this.options.transfirst || i>0) el.set('opacity', 0);
			el.set('tween', { duration: this.options.transform });
		}.bind(this));

		if (this.options.selector) this.createSelector();
		
		this.selectImage(0);
		this.resetTimer();
	},
	
	createSelector: function() {
		this.selector=new Element('ul', { 'class': 'selector' });
		this.selectors=[];
		
		this.images.each(function(image, i) {
			var el=new Element('li').inject(this.selector);
			this.selectors.push(el);
			
			el.addEvent('click', function() {
				this.selectImage(i);
			}.bind(this));
			
		}, this);
		
		this.selector.inject(this.slideshow);
	},
	
	resetTimer: function() {
		if (this.timer!=null) clearInterval(this.timer);
		this.timer=this.next.periodical(this.options.pause+this.options.transform, this);
	},
	
	selectImage: function(index) {
		this.current=index;
		
		this.resetTimer();
		
		if (this.current>=0 & this.current<this.count) {
			this.images.each(function(el, i) {
				if (index==i) {
					if (this.selectors) this.selectors[i].addClass('selected');
					el.tween('opacity', 1);
				} else {
					if (this.selectors) this.selectors[i].removeClass('selected');
					el.tween('opacity', 0);
				}
			}, this);
		}
	},
	
	prev: function() {
		var index=this.current;
			
		if (index<=0) index=this.count;

		index=(index-1)%this.count;
		this.selectImage(index);
	},
	
	next: function() {
		this.selectImage((this.current+1)%this.count);
	}
});
