xChangeImage = function ()
{

	this.vSlideshowTimeout = 50;
	this.vTimeout = 6000;
	this.vStep = 0.05;
//	this.vStartOpacity = 0.1;
	this.vPrevImage = null;
	this.vImages = [];
	this.vTargetElement = arguments[0];

	for(var i=0; i<arguments.length; i++) 
	{
		if (i==0)
		{
			this.vImages[i] = this.vTargetElement;
			this.vImages[i].style.position = 'absolute';
		}
		else
		{
			var tmp = new Image();
			tmp.src = arguments[i];
			tmp.style.position = 'absolute';
//			tmp.style.left = this.vTargetElement.offsetLeft;
//			tmp.style.top = this.vTargetElement.offsetTop;
			tmp.style.opacity = tmp.style.MozOpacity = 0;
			tmp.style.filter = "alpha(opacity=0)";
			this.vTargetElement.parentNode.appendChild(tmp);
			this.vImages[i] = tmp;
		}
	}
	this.vCurrentImage = 0;
	this.vPrevImage = 0;
	this.vTimerId = 'slideshow'; //'change_image_timer' + Math.random();
	window[this.vTimerId] = this;

	setInterval(window[this.vTimerId].fOnTimeout, this.vTimeout);
	var p = this;
}

xChangeImage.prototype.fOnShow = function ()
{
	p = window['slideshow'];

	var tImg = p.vImages[p.vPrevImage];

	var tOpacityLevel = 1*(tImg.getAttribute('step')?tImg.getAttribute('step'):1) - p.vStep;

	tImg.style.opacity = tImg.style.MozOpacity = tOpacityLevel;
	tImg.style.filter = "alpha(opacity=" + tOpacityLevel*100 + ")";
//	console.debug('Down: ' + tOpacityLevel);
//	console.debug('Prev: ' + tOpacityLevel);
	if (tOpacityLevel == 0)
	{
		tImg.setAttribute('step', 1);
	}
	else
	{
		tImg.setAttribute('step', Math.round(100*(tOpacityLevel))/100);
	}
	

	
	var tImg = p.vImages[p.vCurrentImage];
	
	var tOpacityLevel = 1*(tImg.getAttribute('step')?tImg.getAttribute('step'):0) + p.vStep;
	tImg.style.opacity = tImg.style.MozOpacity = tOpacityLevel;
	tImg.style.filter = "alpha(opacity=" + tOpacityLevel*100 + ")";
//	console.debug('Up: ' + tOpacityLevel);
	if (tOpacityLevel == 1)
	{
		tImg.setAttribute('step', 1);
	}
	else
	{
		tImg.setAttribute('step', Math.round(100*(tOpacityLevel))/100);
		setTimeout(window[p.vTimerId].fOnShow, p.vSlideshowTimeout); 
	}
//	console.debug('Next: ' + tOpacityLevel);
	
	
//	p.fProcessHide();
//	p.fProcessShow();

}

//xChangeImage.prototype.fProcessHide = function ()
//{
//	p = window['slideshow'];
//	var tImg = p.vImages[p.vPrevImage];
//
//	var tOpacityLevel = 1*(tImg.getAttribute('step')?tImg.getAttribute('step'):1);
//
//	tImg.style.opacity = tImg.style.MozOpacity = tOpacityLevel;
//	tImg.style.filter = "alpha(opacity=" + tOpacityLevel*100 + ")";
////	console.debug(tOpacityLevel  - p.vStep);
//	if (tOpacityLevel - p.vStep >= 0)
//	{
//		console.debug(tOpacityLevel - p.vStep);
//		tImg.setAttribute('step', Math.round(100*(tOpacityLevel - p.vStep))/100);
//		setTimeout(window[p.vTimerId].fProcessHide, p.vSlideshowTimeout); 
//	}
//	else
//	{
//		tImg.setAttribute('step', 1);
//	}
//	
//}

//xChangeImage.prototype.fProcessShow = function ()
//{
//	p = window['slideshow'];
//	
//	var tImg = p.vImages[p.vCurrentImage];
//	
//	var tOpacityLevel = 1*(tImg.getAttribute('step')?tImg.getAttribute('step'):0);
//	tImg.style.opacity = tImg.style.MozOpacity = tOpacityLevel;
//	tImg.style.filter = "alpha(opacity=" + tOpacityLevel*100 + ")";
//	
//	if (tOpacityLevel + p.vStep <= 1)
//	{
//		tImg.setAttribute('step', Math.round(100*(tOpacityLevel + p.vStep))/100);
//		setTimeout(window[p.vTimerId].fProcessShow, p.vSlideshowTimeout); 
//	}
//	else
//	{
//		tImg.setAttribute('step', 0);
//	}
//}

xChangeImage.prototype.fOnTimeout = function ()
{
	p = window['slideshow'];

	var tmp = p.vCurrentImage + 1;

	if (tmp >= p.vImages.length)
	{
		tmp = 0;
	}
	if (p.vImages[tmp].complete)
	{
		p.vPrevImage = p.vCurrentImage;
		p.vCurrentImage = tmp;
		p.vImages[p.vCurrentImage].setAttribute('step', 0);
		p.vImages[p.vPrevImage].setAttribute('step', 1);
		p.fOnShow();
	}
}