/* ALPHA ANIMATE (C) 2005 Slawomir Pryczek */

function alphaAnimate()
{
	this.animStop = true;
	this.animStep = 0;
	this.callbackAnimFinished = false;
	this.alterOpac = 0;
	this.minOpac = 0;
	
	this.replaceNode = function(oldn, newn, deleteOld)
	{
		if (this.animStop == false)
		{
			if (this.oldn != newn && this.oldn != oldn && this.deleteOld)
				this.oldn.parentNode.removeChild(this.oldn);
			this.setOpacity(this.newn, 100);
		}
		
		if (oldn === false)
			deleteOld = false;
		this.deleteOld = deleteOld ? true : false;

		this.animStep = 1;
		this.oldn = oldn;
		this.newn = newn;
		if (this.newn !== false)
		{
			this.setOpacity(this.newn, 1);
			this.newn.style.display = 'block';
		}
		
		if (this.animStop == true)
		{
			this.animStop = false;
			this.anim();
		}
	}
	
	this.setOpacity = function(node, percentOpaque)
	{
		if (node === false)
			return;
		
		if (this.alterOpac != 0)
			percentOpaque = percentOpaque / this.alterOpac;
		if (this.minOpac != 0)
			percentOpaque = Math.max(this.minOpac, percentOpaque);
		
		if (percentOpaque == 100 && this.alterOpac == 0)
		{
			node.style.opacity = '';
			node.style.filter = ''; 
			return;
		}
		
		var _vopac = percentOpaque/100.0;	
		if (_vopac < 0.01)
		{
			_vopac = '0';
		}
		else
		{
			if (_vopac < 0.1)
				_vopac = '0.0'+percentOpaque;
		}

		node.style.opacity=_vopac;
		node.style.filter = "alpha(opacity=" + percentOpaque + ")"; 
	}
	
	this.anim = function()
	{		
		//this.animStep += (this.animStep+100)/30;
		
		var percopac = Math.ceil(Math.max(0, Math.min(100, this.animStep)));
		this.setOpacity(this.oldn, Math.max(0, 100-(percopac*2)));
		this.setOpacity(this.newn, (percopac > 80 ? 100 : percopac) );

		if (percopac >= 100)
			this.cleanup();
		
		if (!this.animStop)
		{
			var that = this;
			window.setTimeout(function() { that.anim(); }, 40);			
		}
		this.animStep += (100-this.animStep)/10 + 10;
		if (this.animStep>=100)
		{
			if (this.oldn!==false && this.minOpac == 0)
				this.oldn.style.display = 'none';
			
			if (this.callbackAnimFinished!=false)
			{
				this.callbackAnimFinished();
			}
		}
	}
	
	this.cleanup = function()
	{
		if (this.deleteOld)
			this.oldn.parentNode.removeChild(this.oldn);
		this.animStop = true;
	}

}
