﻿
function rotateChildren(container, delay) {
    if ($("#" + container).children().length > 1) {
        $("#" + container).children(":visible").hide();
        
        $("#" + container).children().eq(0).fadeIn('slow', function() { 
            rotateChildrenTimer(container, delay);
        });
    }
}

function rotateChildrenTimer(container, delay) {
    window.setTimeout("rotateDiv('" + container + "', " + delay + ")", delay);
}

function rotateDiv(container, delay) {
	var currentVisible = getCurrentVisibleChild(container);
	var nextVisible = currentVisible + 1;
	if (nextVisible >= $("#" + container).children().length) { nextVisible = 0; }
	
    $("#" + container).children().eq(currentVisible).fadeOut('slow', function() {
        $("#" + container).children().eq(nextVisible).fadeIn('slow', function() {
            rotateChildrenTimer(container, delay);
        });
    });
}

function getCurrentVisibleChild(container) {
	for (var i = 0; i < $("#" + container).children().length; i++) {
		var el = $("#" + container).children().eq(i);
		var disp = el.css("display");
		if (disp != "none") { return i; }
	}
	
	return 0;
}

