var contentRotator = {};
contentRotator.modulesSelector = "form.depth0 .cAll .mi";
contentRotator.controlsSelector = "form.depth0 .cAll .controls";
contentRotator.listSelector = "form.depth0 .cAll .controls li";
contentRotator.linksSelector = "form.depth0 .cAll .controls li a";
contentRotator.playing = false;
contentRotator.timeout = 5000;
contentRotator.timeoutId = null;
contentRotator.currentItemIndex = 0;
contentRotator.playButton = "<img src=\"/_images/mladinska/ico-play.gif\" alt=\"play\" />";
contentRotator.stopButton = "<img src=\"/_images/mladinska/ico-pause.gif\" alt=\"pause\" />";

$(contentRotator.modulesSelector).hide();
$(contentRotator.listSelector).hide();

$(document).ready(function(){
	$("form.depth0 .cAll").css({width: 900, height: 358});
	var moduleCount = $(contentRotator.modulesSelector).size();
	createControls(moduleCount);
	if (moduleCount > 0)
	{
		$(contentRotator.listSelector).show();
		$(contentRotator.linksSelector).click(function (evt){
			if ($(this).parent().hasClass("sel")) return false;
			$(contentRotator.listSelector).removeClass("sel");
			$(contentRotator.modulesSelector + ":visible").hide();
			var linkIndex = $(contentRotator.linksSelector).index(this);
			contentRotator.currentItemIndex = linkIndex;
			$(contentRotator.modulesSelector + ":eq(" + linkIndex + ")").fadeIn(350);
			$(this).parent("li").addClass("sel");
			evt.preventDefault();
		});
	}
	$(contentRotator.linksSelector + ":first").click();
});

function createControls(moduleCount){
	$(contentRotator.controlsSelector).empty();
	for (var i=0; i<moduleCount; i++)
	{
		$("<li><a href=\"javascript:void(0)\">" + (i + 1) + "</a></li>").appendTo(contentRotator.controlsSelector);
	}
	var player = $("<span class=\"player\"><a href=\"javascript:void(0)\">" + contentRotator.playButton + "</a></span>").insertAfter($(contentRotator.controlsSelector));
	player.click(function (evt){
		contentRotator.playing = !contentRotator.playing;
		if (contentRotator.playing)
		{
			startPlayer();
			$("span.player a").html(contentRotator.stopButton);
		}
		else
		{
			stopPlayer();
			$("span.player a").html(contentRotator.playButton);
		}
		evt.preventDefault();
	});
	$(".player a").click();
}

function startPlayer(){
	contentRotator.currentItemIndex = $(contentRotator.listSelector).index($($(contentRotator.listSelector + ".sel")));
	play();
}

function play(){
	var itemCount = $(contentRotator.linksSelector).size();
	++contentRotator.currentItemIndex;
	contentRotator.currentItemIndex %= itemCount;
	$(contentRotator.linksSelector + ":eq(" + contentRotator.currentItemIndex + ")").click();
	contentRotator.timeoutId = setTimeout("play()", contentRotator.timeout);
}

function stopPlayer(){
	clearTimeout(contentRotator.timeoutId);
}