﻿var rotatorTimeout;

function enableRotator(rotatorDivSelector,delay, leftSelector, rightSelector) {
	$(function () {
		$(rotatorDivSelector).children().hide();
		$(rotatorDivSelector).children().first().show();
		$(leftSelector).click(function() {
			rotate(rotatorDivSelector,0, delay)
		});
		$(rightSelector).click(function() {
			rotate(rotatorDivSelector,1, delay)
		});
		rotatorTimeout = setTimeout( function() {
			rotate(rotatorDivSelector,1, delay);
		}, delay);
	});
}

function rotate(rotatorDivSelector,rotateWay, delay) {
	clearTimeout(rotatorTimeout);
	if($("div#mce_fullscreen_container").length == 0) {

	var toBeDisplayed;
	$(rotatorDivSelector).children().each(function() {
		if($(this).css('display') == "block") {
			$(rotatorDivSelector).children().hide();
			if(rotateWay > 0) {
				toBeDisplayed = $(this).next();
				if(toBeDisplayed.length == 0) {
					toBeDisplayed = $(this).parent().children().first();
				}
			} else {
				toBeDisplayed = $(this).prev();
				if(toBeDisplayed.length == 0) {
					toBeDisplayed = $(this).parent().children().last();
				}
			}
		}
	});
	toBeDisplayed.show();
	}
	rotatorTimeout = setTimeout( function() {
			rotate(rotatorDivSelector,1,delay);
		}, delay);
}
