function Slideshow()
{
	this.data = new Array;
	this.loader = new Image();
	this.delay = 3000;
	this.animTime = 1000;
	this.indexPrev = 0;
	this.indexCurr = 0;
	this.activeSlide = 1;
	this.startTicks;
	this.oncomplete = createCallback(this, "run");
}

Slideshow.prototype.initialize = function ()
{
	var data = document.getElementById("gallery_data");
	var target = document.getElementById("gallery_target");
	if (data != null && target != null)
	{
		this.data = new Array;
		var imgs = data.value.split(recordSeparator);
		for (var i = 0; i < imgs.length; i++)
		{
			var values = imgs[i].split(fieldSeparator);
			this.data.push({ href: values[0], src: values[1], title: values[2] });
		}

		this.loader.onload = createCallback(this, "image_loaded");
		this.run();
	}
}

Slideshow.prototype.show_next = function ()
{
	this.startTicks = new Date().getTime();
	this.indexPrev = this.indexCurr;
	this.indexCurr = this.indexCurr < this.data.length - 2 ? this.indexCurr + 1 : 0;
	this.loader.src = this.data[this.indexCurr].src;
}

Slideshow.prototype.image_loaded = function ()
{
	var ticks = this.getTicks();
	var diff = this.delay - ticks;
	if (diff <= 0)
		this.playTransition();
	else
		setTimeout(createCallback(this, "playTransition"), diff);
}

Slideshow.prototype.playTransition = function ()
{
	var slideIn, slideOut;
	if (this.activeSlide == 1)
	{
		slideIn = document.getElementById("slide2");
		slideOut = document.getElementById("slide1");

		this.activeSlide = 2;
	}
	else
	{
		slideIn = document.getElementById("slide1");
		slideOut = document.getElementById("slide2");

		this.activeSlide = 1;
	}

	var img = this.data[this.indexCurr];
	slideIn.src = img.src;
	slideIn.parentNode.href = img.href;
	slideIn.parentNode.title = img.title;

	this.alpha1 = new anim.Alpha(slideIn, 100, this.animTime);
	this.alpha1.setAlpha(0);
	this.alpha2 = new anim.Alpha(slideOut, 0, this.animTime);
	this.alpha2.setAlpha(100);

	this.trans = new anim.Sequence(anim.Sequence.TypeSimultaneous, this.oncomplete);
	this.trans.addChild(this.alpha1);
	this.trans.addChild(this.alpha2);
	this.trans.run();
}

Slideshow.prototype.run = function ()
{
	this.cleanup();
	this.show_next();
}

Slideshow.prototype.getTicks = function ()
{
	return (new Date().getTime() - this.startTicks);
}

Slideshow.prototype.cleanup = function ()
{
	this.alpha1 = null;
	this.alpha2 = null;
	this.trans = null;
}

Slideshow.current = null;
Slideshow.setup = function ()
{
	Slideshow.current = new Slideshow;
	Slideshow.current.initialize();
}

Slideshow.cleanup = function ()
{
	if (Slideshow.current)
	{
		Slideshow.current.cleanup();
		Slideshow.current = null;
	}
}

evt.addHandler(window, "onload", Slideshow.setup);
evt.addHandler(window, "onunload", Slideshow.cleanup);
