(function($) {
	$.extend({
		rotator: function(rotationId, contents, buttons, delay, effect) {
			if (contents.length != contents.length) {
				return;
			}

			$(buttons).each(function(index) {
				$(this).click(function() {
					pauseRotation(rotationId);
					showContent(contents, buttons, index, effect);
				});
				$(this).css({ cursor: "pointer" });
			});

			showContent(contents, buttons, 0, effect);

			resumeRotation(rotationId, contents, buttons, delay, effect);
		}
	});

	// private functions
	function showContent(contents, buttons, index, effect) {
		contents.filter(":visible").hide();
		buttons.filter(".active-content").removeClass("active-content");
		
		if (effect == null) {
			$(contents[index]).show();
		} else {
			$(contents[index]).show(effect);
		}
		$(buttons[index]).addClass("active-content")
	}

	function pauseRotation(rotationId) {
		$(window).stopTime(rotationId);
	}

	function resumeRotation(rotationId, contents, buttons, delay, effect) {
		$(window).stopTime(rotationId);
		$(window).everyTime(delay, rotationId, function() {
			showContent(contents, buttons, (indexOfSelected(contents) + 1) % contents.length, effect);
		});
	}

	function indexOfSelected(contents) {
		for (var i = 0; i < contents.length; ++i) {
			if ($(contents[i]).is(":visible")) {
				return i;
			}
		}

		return -1;
	}
})(jQuery);

