/*
* ElementFader class
* @author: Marco Hoeck <marco.hoeck@tvi-services.de>
* @copyright	TV Information Services 2007
* @since		20.09.2007 - 10:00:00
*/

/*
* ElementFader
*/
function ElementFader(id)
{
	this.id = id;
	this.quantization = 0.1;
	this.selectedItem = 0;
	this.timeout = 0;
	this.nodes = new Array();
	this.Interval = window.setInterval("document.getElementById('" + id + "').ElementFader.updateAlpha(true)", 100);


	/*
	* addNode
	*/
	this.addNode = function(node)
	{
		if(!node)
			return;

		this.nodes.push(node);
	}

	/*
	* updateAlpha
	*/
	this.updateAlpha = function(fade)
	{
		if(!fade)
			fade = false;

		if(this.timeout > 0)
		{
			this.timeout--;
			return;
		}

		for(var i = 0; i < this.nodes.length; i++)
		{
			var newAlpha = this.getNodeAlpha(this.nodes[i]);
			var oldAlpha = newAlpha;

			if(i == this.selectedItem)
			{
				if(fade)
					newAlpha += this.quantization;
				else
					newAlpha = 100;
			}
			else
			{
				if(fade)
					newAlpha -= this.quantization;
				else
					newAlpha = 0;
			}

			if(newAlpha < 0)
				newAlpha = 0;
			else if(newAlpha > 1) newAlpha = 1;

			if(newAlpha != oldAlpha)
			{
				this.setNodeAlpha(this.nodes[i], newAlpha);
			}

			if(newAlpha == 0)
			{
				this.nodes[i].style.display = 'none';
			}
			else
			{
				this.nodes[i].style.display = 'block';
			}
		}
	}

	/*
	* getNodeAlpha
	*/
	this.getNodeAlpha = function(node)
	{
		if(!node)
			return;

		var alpha = 1;
		if(node.style.opacity)
			alpha = parseFloat(node.style.opacity);

		return alpha;
	}

	/*
	* setNodeAlpha
	*/
	this.setNodeAlpha = function(node, alpha)
	{
		if(!node)
			return;

		node.style.opacity = alpha + "";
		node.style.filter = "alpha(opacity=" + (alpha * 100) + ")";

		return alpha;
	}
}