CrossFade = function(cnt, img)
{
	this.cnt = $('#' + cnt);
	this.imglist = img;
	this.suspend = false;

	this.interval = 5000;	// tempo fra un fade e l'altro
	this.speed = 2000;		// velocit crossfading

	this.attach();
};

CrossFade.prototype.attach = function()
{
	this.cnt.css({position: 'relative'});

	this.cur = -1;

		// siccome le immagini ci mettono del tempo per
		// essere caricate, tratta la prima immagine in
		// modo particolare, caricandola immediatamente e facendo partire
		// l'animazione non appena questa  caricata
		// il caricamento delle altre avviene solo dopo, in modo da non
		// occupare banda inutilmente con il loro caricamento
		// ma effettuando comunque un preload

	this.img = [];
	this.loadImage(this.imglist[0]);

	var me = this;
	this.img[0].load(function() {

		me.animate();

		for (var i = 1; i < me.imglist.length; i++)
			me.loadImage(me.imglist[i]);
	});
};

CrossFade.prototype.loadImage = function(src)
{
	this.img.push($('<img />')
		.attr('src', src)
		.css({
			opacity: 0,
			position: 'absolute'
		})
		.appendTo(this.cnt)
	);
};

CrossFade.prototype.animate = function()
{
	if (this.suspend)
		return;

	if (this.cur >= 0)
		this.img[this.cur].animate({opacity: 0}, this.speed);

	if (++this.cur == this.imglist.length)
		this.cur = 0;

	this.img[this.cur].animate({opacity: 1}, this.speed);

	var me = this;
	setTimeout(function() { me.animate(); }, this.interval);
};

CrossFade.prototype.pause = function()
{
	this.suspend = true;
};

CrossFade.prototype.resume = function()
{
	this.suspend = false;
	this.animate();
};
