UKISA.site.Fader = function(el, childNodeType) {
	this.canvas = document.getElementById(el);

	if (this.canvas) {
	this.childNodeType = childNodeType;

	YAHOO.util.Dom.addClass(this.canvas, "ukisa-fader-canvas");

	this.init();
	}
};

UKISA.site.Fader.prototype = {
	canvas: null,
	
	childNodeType: null,

	children: null,
	
	currentChild: 0,

	init: function() {
		var i, _this, Dom, ix;

		this.children = $(this.childNodeType, this.canvas);

		Dom = YAHOO.util.Dom;

		// There must be three items or this scroller will not work.
		if (this.children.length > 1) {

			for (i = 0, ix = this.children.length; i < ix; i++) {
				Dom.addClass(this.children[i], "ukisa-fader-item");
			}

			this.currentChild = this.children.length - 1; 

			_this = this;

			YAHOO.util.Dom.setStyle(this.children[0], "opacity", 0);

			function rotate() {

				setTimeout(function() {
					var toFadeNow, nextInLine, animation;	

					toFadeNow = _this.children[_this.currentChild];

					if (_this.currentChild === 0) {
						nextInLine = _this.children[_this.children.length - 1];
					} else {
						nextInLine = _this.children[_this.currentChild - 1];
					}

					Dom.setStyle(toFadeNow, "zIndex", 10);

					Dom.setStyle(nextInLine, "opacity", 1);

					Dom.setStyle(nextInLine, "zIndex", 0);

					animation = new YAHOO.util.Anim(toFadeNow, {
						opacity: {
								from: 1,
								to: 0 
							} 
						}, 
						1, 
						YAHOO.util.Easing.easeOut
					);
					animation.onComplete.subscribe(function() {				
						if (_this.currentChild === 0) {
							_this.currentChild = _this.children.length - 1;
						} else {
							_this.currentChild--;
						}
				
						rotate();
					});
					animation.animate();
			
						
				}, 8000);

			}

			rotate();
		}
	}

};
