dojo.require("dojo.fx");

var nodes;
var counter = 0;
var currentNode;
var nextNode;
var displayTime;
var transformTime;
var handle;
var combinedAnim;
var combinedAnim2;
var paused = false;

function fadeNodes(className, displayTimeParam, transformTimeParam) {
	dojo.ready(
		function(){ 
			nodes = dojo.query(className);
			for(var i = 1; i<nodes.length; i++){
				dojo.style(nodes[i], "display", "none");
				dojo.style(nodes[i], "opacity", "0");
			}
			displayTime = displayTimeParam;
			transformTime = transformTimeParam;
			currentNode = nodes[0];
			nextNode = nodes[1];
			combineIt();
		});
}

function combineIt() {
	currentNode = nodes[counter];
	counter = counter + 1;
	if (counter > nodes.length-1) {
		counter = 0;
	}
	nextNode = nodes[counter];

	//Fade the current node out
	var delay = dojo.animateProperty( {
			node: currentNode,
			delay: displayTime
		}
	)
	
	var hideAnim = dojo.fx.chain([
		delay,
		dojo.fadeOut({
			node: currentNode,
			duration: transformTime,
			onEnd: function() {
				dojo.style(currentNode, "display", "none");
			}
		})
	]);
	
	//Fade the next node in
	var showAnim = dojo.fx.chain([
		delay,
		dojo.fadeIn({
			onBegin: function() {
				dojo.style(nextNode, "display", "block");
			},
			node: nextNode,
			duration: transformTime
		})
	]);
	
	//Combine the two sets of animations into one that runs in parallel.
	combinedAnim = dojo.fx.combine([hideAnim, showAnim]);
	
	//Set it so that every time it ends, it runs again.
	handle = dojo.connect(showAnim, "onEnd", combineIt);
	
	//Run it!
	combinedAnim.play();
}
dojo.addOnLoad(setup);

function setup() {
	dojo.query(".promoComponentRullerendeContainer").onmouseenter(function(e){
		paused = true;
		combinedAnim.pause();
	});
	
	dojo.query(".promoComponentRullerendeContainer").onmouseleave(function(e){
		//Start immediately, false -> start from current position (If true, starts the animation from the beginning; otherwise, starts it from its current position.)
		combinedAnim.play(0, false);
	});	
}

