//
// faders.js
//
// by Chris Klimas <klimas@gmail.com>
//
// A quick and mostly simple way of fading text without losing
// its anti-aliasing in Internet Explorer. This code is released
// under the terms of the Creative Commons Attribution-Share Alike
// 3.0 License: http://creativecommons.org/licenses/by-sa/3.0/
//

function fade (el, options)
{
	// el - the DOM element to fade
	//
	// options - an object with two properties:
	//
	//           fade - either 'in' or 'out', specifying the direction
	//           of the fade (required)
	//
	//           onComplete - a function to call after the fade
	//                        is done (optional)

	var current;
	var proxy = el.cloneNode(true);
	var direction = (options.fade == 'in') ? 1 : -1;
	
	el.parentNode.replaceChild(proxy, el);
	
	if (options.fade == 'in')
	{
		current = 0;
		proxy.style.visibility = 'visible';
	}
	else
		current = 1;

	setOpacity(proxy, current);	
	var interval = window.setInterval(tick, 25);
	
	function tick()
	{
		current += 0.05 * direction;
		
		setOpacity(proxy, current);
		
		if (((direction == 1) && (current >= 1))
				|| ((direction == -1) && (current <= 0)))
		{
			el.style.visibility = (options.fade == 'in') ? 'visible' : 'hidden';
			proxy.parentNode.replaceChild(el, proxy);
			delete proxy;
			window.clearInterval(interval);	
			
			if (options.onComplete)
				options.onComplete();
		}
	};
	
	function setOpacity (el, opacity)
	{						
		var percent = Math.floor(opacity * 100);
			
		// IE
		el.style.zoom = 1;
		el.style.filter = 'alpha(opacity=' + percent + ')';
					
		// CSS 3
		el.style.opacity = opacity;
	};
};