var xfade = {
	path:'',
	images:new Array(),
	running:false,
	active:null,
	clear:false,
	addImage:function(path,width,height,alt){
		this.images[this.images.length] = {
			path:path,
			width:width,
			height:height,
			alt:alt
		};
	},
	showNext:function(){
		if(this.running){
			var visible =  $('div.xfade div img:visible');
			var next = $('div.xfade div img:visible').next();
			if(next.length < 1){
				next = $('div.xfade div img:first');
			}
			
			visible.fadeOut(2000,function(){
				if(xfade.clear){
					$(this).remove();
					xfade.clear = false;
				}
			});
				
			next.fadeIn(2000,function(){
				xfade.active = $(this);
				xfade.loop();
			});
		}
	},
	loop:function(){
		if(xfade.running){
			$('div.xfade').animate({opacity: 1}, 3000, function(){
				xfade.showNext();
			});
		}
	},
	play:function(){
		this.running = true;
		this.loop();
	},
	stop:function(){
		this.running = false;
	},
	activate:function(){
		for(var i=1;i<this.images.length;i++){
			$('div.xfade div').append('<img src="'+this.images[i].path+'" width="'+this.images[i].width+'" height="'+this.images[i].height+'" alt="'+this.images[i].alt+'" style="display:none;" />');
		}
		this.path = this.images[0].path;
		this.active = $('div.xfade div img:first');
		this.play();
	},
	replace:function(images){
		if(images[0].path != this.path){
			this.stop();
			this.clear = true;
			$('div.xfade div img:hidden').remove();
			for(var i=0;i<images.length;i++){
				$('div.xfade div').append('<img src="'+images[i].path+'" width="'+images[i].width+'" height="'+images[i].height+'" alt="'+images[i].alt+'" style="display:none;" />');
			}
			this.play();
			this.path = images[0].path;
		}
	}
}