Slideshow = function(ul_id, start_frame, delay, caption_id)
{
	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.caption = false;
	this.lis = false;
	this.init(ul_id, caption_id);
}

Slideshow.prototype.init = function(ul_id, caption_id)
{
	this.lis = $(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-2;
		this.next_frame = 1;
	}

	if(typeof caption_id != 'undefined') {
		this.caption = $(caption_id);
	}

	this.start(this.start_frame);
}

Slideshow.prototype.start = function()
{
	var tmp = this;
	new PeriodicalExecuter(function(pe) {
		if(tmp.running) {
			tmp.fadeInOut();
		}
	}, tmp.delay);
}

Slideshow.prototype.fadeInOut = function()
{
	if(this.next_frame > this.end_frame) {
		this.next_frame = this.start_frame;
	}

	Effect.Fade(this.lis[this.current_frame], {queue:{position:'end',scope:'image'}});
	this.current_frame = this.next_frame;
	Effect.Appear(this.lis[this.current_frame], {queue:{position:'end',scope:'image'}});

	if(this.caption) {
		var tmp = this;
		Effect.Fade(this.caption, {queue:{position:'end', scope:'caption'}, afterFinish:function() {
			tmp.caption.innerHTML = tmp.lis[tmp.current_frame].getElementsByTagName('img')[0].title;
			Effect.Appear(tmp.caption, {queue:{position:'end', scope:'caption'}});
		}});
	}
	else {
	}

	this.next_frame++;
}

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();
	}
}
