 /**
  * JavaScript for changing images
  *
  * @author Yan Dehner, yan.dehner@pingping.ag
  * @copy pingping.ag
  ***/

function imageChanger(_id, _path, _name, _images)
{
	this.name = _name;
	this.path = _path;
	this.element = get(_id);
	this.images = new Object();
	this.queue = null;
	this.act_image_nr = 0;
	this.isCool = !document.all;

	this.complete = function(_image)
	{
		var image;
		image = document.createElement("IMG");
		image.src = _image.src;
		this.element.appendChild(image);

		image.style.top = Math.round(55 - image.height/2) + "px";
		image.style.left = Math.round(87-image.width/2) + "px";

		if(this.isCool) image.style.opacity = 0.0;
			else image.style.filter='alpha(opacity=0)';
		if ( this.queue )
		{
			this.queue.push(image);
		} else {
			this.queue = new Array(image);
			this.animate(0);
			this.blend(0, 0, 10);
		}
	}

	this.animate = function( image_nr )
	{
		if ( this.act_image_nr != image_nr )
		{
			this.blend(this.act_image_nr, 10, 0);

		}

		if ( this.act_image_nr != image_nr )
		{
			this.blend(image_nr, 0, 10);
			this.act_image_nr = image_nr;
		}

		this.queue[image_nr].style.visibility = "visible";
		window.setTimeout(this.name + ".animate("+ ((image_nr+1)%this.queue.length) +")", 3000);
	}

	this.blend = function( image_nr, from_value, to_value )
	{
		var step = (to_value - from_value) / 10;

		if(!to_value)
			window.setTimeout(this.name+'.queue['+image_nr+'].style.visibility="hidden"',from_value*100);
		else this.queue[image_nr].style.visibility="visible";

		for(var sklave=from_value;sklave<=to_value&&step>0||sklave>=to_value&&step<0;sklave+=step)
		{
			if(this.isCool) window.setTimeout(this.name+'.queue['+image_nr+'].style.opacity='+sklave*.1,step>0?sklave*100:1000-sklave*100);
			else window.setTimeout(this.name+'.queue['+image_nr+'].style.filter="alpha(opacity='+sklave*10+')"',step>0?sklave*100:1000-sklave*100);

			// filter:alpha(opacity=50);
		}
	}

	for(var i=0; i<_images.length; i++)
	{
		this.images[_images[i]] = new Image();
		this.images[_images[i]].src = this.path + _images[i];
		this.images[_images[i]].changer = this;
		this.images[_images[i]].onload = function () {this.changer.complete(this);}
	}
}