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: 3000 });

		this.matches[0].set('opacity', 1);
		if(this.matches.length > 1) this.tween(0);
	},
	addEvents: function() {
       if($('next')) {
           $('next').addEvent('click', function(ev) {
           this.tween(this.next_id);
           new Event(ev).stop()
       }.bind(this));
       }
       if($('previous')){
       $('previous').addEvent('click', function(ev) {
           this.tween(this.prev_id);
           new Event(ev).stop()
       }.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(5000);

		
	}
}


window.addEvent('domready', function() {
	slideshow.init('#promotion .image', 'custom_function');
	$$('.slideBar').set('opacity', 0.8);
});
