function private_img_mouseover (evn) {
	if (evn) {
		this.xfader.pause();
	} else {
		if (window.event.srcElement.parentNode.id=="imagecontainer") {
			window.event.srcElement.xfader.pause();
		}
	}
}

function private_img_mouseout (evn) {
	if (evn) {
		this.xfader.play();
	} else {
		if (window.event.srcElement.parentNode.id=="imagecontainer") {
			window.event.srcElement.xfader.play();
		}
	}
}


function private_setup () {
	if (this.host_handle) {
		if (this.host_handle.hasChildNodes()) {
			for (var i=0; i<this.host_handle.childNodes.length; i++) {
				var elem=this.host_handle.childNodes[i];
				
				if (elem.nodeName.toLowerCase()=="img") {
					this.images.push(elem);
					
					elem.xfader=this;
					
					elem.onmouseover=private_img_mouseover;
					elem.onmouseout=private_img_mouseout;
				}
			}
			
			if (this.images.length>1) {
				this.current_image=0;
				this.next_image=1;
			}
		}
	}
}

function private_do_xfade () {
	if (this.images.length>1) {
		var i_fadeout=this.images[this.current_image];
		var i_fadein=this.images[this.next_image];
		
		i_fadeout.style.opacity=1.0-this.fade_amount*0.01
		i_fadeout.style.filter="alpha(opacity="+(100-Math.round(this.fade_amount))+")";
		
		i_fadein.style.opacity=this.fade_amount*0.01
		i_fadein.style.filter="alpha(opacity="+(Math.round(this.fade_amount))+")";
		
		if (!this.paused) {
			this.fade_amount=this.fade_amount+this.fading_speed;
		}
		
		if (this.fade_amount>100.0) {
			i_fadeout.style.display="none";
			
			this.current_image=this.next_image;
			this.next_image=(this.current_image+1) % this.images.length;
			
			i_fadein=this.images[this.next_image];
			i_fadein.style.display="block";
			
			this.fade_amount=0.0;
		}
	}
}

function private_pause () {
	this.paused=true;
}

function private_play () {
	this.paused=false;
}

function xfader (img_host_id) {
	this.host_handle=document.getElementById(img_host_id);
	this.host_handle.xfader=this;
	
	this.images=new Array();
	this.current_image=0;
	this.next_image=0;
	this.fade_amount=100.0;
	this.fading_speed=1.0;
	
	this.paused=false;

	this.setup=private_setup;

	this.do_xfade=private_do_xfade;
	
	this.pause=private_pause
	
	this.play=private_play
	
	this.setup();
}

function xfade_step () {
	if (window.xf) {
		window.xf.do_xfade()
	}
}

function xfade_init () {
	window.xf=new xfader("imagecontainer");
	
	setInterval(xfade_step, 50);
}
