Slideshow = function(ul_id, start_frame, delay)
{
	this.start_frame = start_frame;
	this.end_frame = start_frame;
	this.current_frame = start_frame;
	this.next_frame;
	this.delay = delay;
	this.running = true;
	this.lis = false;
	this.ul_id = ul_id;
	this.fade_listeners = Array();
	this.init();
}

Slideshow.prototype.init = function()
{
	this.lis = $(this.ul_id).getElementsByTagName('li');
	if(this.lis.length > 0) {
		for(i=0;i<this.lis.length; i++) {
			if(i!=0) {
				this.lis[i].style.display = 'none';
			}
		}
		this.end_frame = this.lis.length-1;
		this.next_frame = 1;
	}

	this.start();
}

Slideshow.prototype.start = function()
{
	var tmp = this;
	tmp.running = true;
	new PeriodicalExecuter(function(pe) {
		if(tmp.running) {
			tmp.fadeInOut();
		}
		else {
			pe.stop();
		}
	}, tmp.delay);
}

Slideshow.prototype.stop = function()
{
	this.running = false;
}

Slideshow.prototype.restart = function()
{
	this.fadeInOut();
	this.start();
}

Slideshow.prototype.fadeInOut = function()
{
	if(this.next_frame > this.end_frame) {
		this.next_frame = this.start_frame;
	}

	var tmp_ul_id = this.ul_id;

	Effect.Fade(this.lis[this.current_frame], {queue:{position:'end',scope:tmp_ul_id}});
	this.current_frame = this.next_frame;
	this.callFadeListeners();
	Effect.Appear(this.lis[this.current_frame], {queue:{position:'end',scope:tmp_ul_id}});

	this.next_frame++;
}

Slideshow.prototype.attachFadeListener = function(listener)
{
	this.fade_listeners[this.fade_listeners.length] = listener;
}

Slideshow.prototype.callFadeListeners = function()
{
	for(i=0;i<this.fade_listeners.length;i++) {
		this.fade_listeners[i](this);
	}
}

Slideshow.prototype.goto = function(frame_index)
{
	if(frame_index >= this.start_frame && frame_index <= this.end_frame) {
		this.running = false;
		this.next_frame = frame_index;
		this.fadeInOut();
	}
}
