//*************************
// ImageFader Constructor *
//*************************
function ImageFader(image_wrapper, interval, speed)
{	
	// get id's
	this.image_wrapper	= document.getElementById(image_wrapper);
	this.images			= this.image_wrapper.getElementsByTagName('img');
	
	// set properties
	this.interval	= !interval ? 1000 : interval;
	this.speed		= !speed ? 50 : speed;
	this.current	= 0;
}

//**********************************
// Function initialize image fader *
//**********************************
ImageFader.prototype.init = function()
{
	if (this.images.length > 0)
	{
		// set object as var
		var _this = this;
		
		// hide all images
		for (var i=1; i<this.images.length; i++)
		{
			this.images[i].xOpacity = 0;
		}
		
		// show first image
		this.images[0].style.display	= 'block';
		this.images[0].xOpacity			= .99;
		
		// do the fader
		var newTimer = setTimeout(
			function ()
			{
				_this.set();
			}
			, _this.interval
			)
	}
}

//**********************
// Function fade image *
//**********************
ImageFader.prototype.set = function()
{
	// set object as var
	var _this = this;
	
	var cOpacity	= this.images[this.current].xOpacity;
	var nIndex		= this.images[this.current + 1] ? (this.current + 1) : 0;
	var nOpacity	= this.images[nIndex].xOpacity;
	
	cOpacity	-= .05; 
	nOpacity	+= .05;
	
	this.images[nIndex].style.display	= 'block';
	this.images[this.current].xOpacity	= cOpacity;
	this.images[nIndex].xOpacity		= nOpacity;
	
	this.opacity(this.images[this.current]); 
	this.opacity(this.images[nIndex]);
	
	if (cOpacity<=0)
	{
		this.images[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 *
//***********************
ImageFader.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) + ')';
}
