//This slideshow is version 0.1 and uses MooTools 1.2.1
var slideshow = {
	init: function(match, callback_func) {
		this.matches = $$(match);
		this.next_id;
		this.prev_id;
		this.delayed_event = false;
		this.addEvents();
		this.callback = callback_func;
		
		this.matches.set('opacity', 0);
		this.matches.set('tween', { duration: 2000 });
		
		if(this.matches.length > 0) {
			this.matches[0].set('opacity', 1);
		}
		if(this.matches.length > 1) this.tween(0);
	},
	addEvents: function() {
		if($('slideshowNext')) {
			$('slideshowNext').addEvent('click', function() {
				this.tween(this.next_id);
				return false;
			}.bind(this));
		}
		
		if($('slideshowPrevious')) {
			$('slideshowPrevious').addEvent('click', function() {
				this.tween(this.prev_id);
				return false;
			}.bind(this));
		}
	},
	
	tween: function(ele_id) {
		this.matches.tween('opacity', 0);
		this.matches[ele_id].tween('opacity', 1);
		
		// calc next
		var next_int = ele_id + 1;
		if(next_int >= this.matches.length) next_int = 0;
		
		// calc previous
		var prev_int = ele_id - 1;
		if(prev_int < 0) prev_int = this.matches.length - 1;
		
		this.next_id = next_int;
		this.prev_id = prev_int;
		
		if(this.delayed_event) $clear(this.delayed_event);
		
		this.delayed_event = (function() { this.tween(next_int) }.bind(this)).delay(6000);
		
		if(this.callback) {
			eval(this.callback)(this.matches[ele_id]);
		}
	}
}

//This needs to be on client's page
/*
	function custom_function(ele) {
		(function() { $('text').innerHTML = ele.title; }).delay(500);
	}
	window.addEvent('domready', function() {
		slideshow.init('#slideshow .image', 'custom_function');
	});
*/
	

