//************************
// NodeFader Constructor *
//************************
function NodeFader(node_wrapper, node_name, interval, speed)
{	
	// get id's
	this.node_wrapper	= document.getElementById(node_wrapper);
	
	if (this.node_wrapper)
	{
		this.nodes			= this.node_wrapper.getElementsByTagName(node_name);
		
		// set properties
		this.interval	= !interval ? 1000 : interval;
		this.speed		= !speed ? 50 : speed;
		this.current	= 0;
		
		// initialize fader
		this.init();
	}
}

//**********************************
// Function initialize image fader *
//**********************************
NodeFader.prototype.init = function()
{
	if (this.nodes[0])
	{
		// set object as var
		var _this = this;
		
		// hide all images
		for (var i=1; i<this.nodes.length; i++)
		{
			this.nodes[i].xOpacity = 0;
		}
		
		// show first image
		this.nodes[0].style.display	= 'block';
		this.nodes[0].xOpacity			= .99;
		
		// do the fader
		var newTimer = setTimeout(
			function ()
			{
				_this.set();
			}
			, _this.interval
			);
	}
}

//**********************
// Function fade image *
//**********************
NodeFader.prototype.set = function()
{
	// set object as var
	var _this = this;
	
	var cOpacity	= this.nodes[this.current].xOpacity;
	var nIndex		= this.nodes[this.current + 1] ? (this.current + 1) : 0;
	var nOpacity	= this.nodes[nIndex].xOpacity;
	
	cOpacity	-= .05; 
	nOpacity	+= .05;
	
	this.nodes[nIndex].style.display	= 'block';
	this.nodes[this.current].xOpacity	= cOpacity;
	this.nodes[nIndex].xOpacity		= nOpacity;
	
	this.opacity(this.nodes[this.current]); 
	this.opacity(this.nodes[nIndex]);
	
	if (cOpacity<=0)
	{
		this.nodes[this.current].style.display = 'none';
		this.current = nIndex;
		
		// do the fader
		var newTimer = setTimeout(
			function ()
			{
				_this.set();
			}
			, _this.interval
			)
	}
	else
	{
		// do the fader
		var newTimer = setTimeout(
			function ()
			{
				_this.set();
			}
			, _this.speed
			)		
	}
}

//***********************
// Function set opacity *
//***********************
NodeFader.prototype.opacity = function(obj)
{
	if(obj.xOpacity > .99)
	{
		obj.xOpacity = .99;
		
		return;
	}
	
	obj.style.opacity		= obj.xOpacity;
	obj.style.MozOpacity	= obj.xOpacity;
	
	obj.style.filter		= 'alpha(opacity=' + (obj.xOpacity * 100) + ')';
}