 /**
  * JavaScript for rotating through images
  *
  * @author Steffen Friedrich, steffen.friedrich@pingping.ag
  * @copy pingping.ag
  ***/

function PP_References(_id, _path, _name, _images, _x, _y)
{	this.x = _x ? _x : 87;
	this.y = _y ? _y : 55;
	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);

		this.reposition(image);

		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.reposition = function( image )
	{		image.style.top = Math.round(this.y-image.height/2) + "px";
			image.style.left = Math.round(this.x-image.width/2) + "px";

			image.reposition = !image.height;
	}

	this.animate = function( image_nr )
	{	if(this.queue[image_nr].reposition)
			this.reposition(this.queue[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 )
	{	if(this.queue[image_nr].reposition)
			this.reposition(this.queue[image_nr]);
		
		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);
	}

	for(var i=0; i<_images.length; i++)
	{	this.images[_images[i]] = new Image();
		this.images[_images[i]].changer = this;
		this.images[_images[i]].onload = function () {this.changer.complete(this);}
		this.images[_images[i]].src = this.path + _images[i];
	}
}

function PP_Quotes(_id, _path, _name, data)
{	this.name = _name;
	this.path = _path;
	
	this.queue = new Array();
	this.act_image_nr = 0;
	this.isCool = !document.all;

	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].element.style.visibility = "visible";
		window.setTimeout(this.name + ".animate("+ ((image_nr+1)%this.queue.length) +")", 10000);
	}

	this.blend = function( image_nr, from_value, to_value )
	{	var step = (to_value - from_value) / 10;
	
		var elsN = this.name+'.queue['+image_nr+'].element.style.';

		if(!to_value) window.setTimeout(elsN+'visibility="hidden"',from_value*100);
		else this.queue[image_nr].element.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(elsN+'opacity='+sklave*.1,step>0?sklave*100:1000-sklave*100);
			else window.setTimeout(elsN+'filter="alpha(opacity='+sklave*10+')"',step>0?sklave*100:1000-sklave*100);
		}
	}

	for(var i=0; i<data.length; i++)
	{	data[i].element = get(data[i].id);
		if(this.isCool) data[i].element.style.opacity=0.0;
			else data[i].element.style.filter='alpha(opacity=0)';
		this.queue.push(data[i]);
	}
	
	this.animate(0);
	this.blend(0, 0, 10);	
}