var timoutId;
var curIndex = -1;
var nextIndex = 0;
var items;

$(document).ready(function() {
	$("#top-menu li").hover(
		function() {
			$(this).find("ul").show();
		},
		function() {
			$(this).find("ul").hide();
		}
	);
	items = $("#rotate-items .items .item");
	timoutId = setTimeout(function() {Rotation()}, 10000);
});

function Rotate(block1, block2, curIndex, nextIndex) {
	$(block1).fadeOut(500, function(){
		$(block2).fadeIn(500);
	});
}

function Rotation() {
	curIndex=nextIndex;
	nextIndex++;
	
	if (curIndex==items.length-1)
	{
		nextIndex=0;
	}
	Rotate(items[curIndex], items[nextIndex], curIndex, nextIndex);
	timoutId = window.setTimeout(function() {Rotation()}, 9000);
}

function setVisible(obj, itemIndex) {
	if (!$(obj).hasClass("act")) {
		items.stop();
		clearTimeout(timoutId);
		curIndex=nextIndex;
		nextIndex = itemIndex;
		Rotate(items[curIndex], items[nextIndex], curIndex, nextIndex);
		curIndex = itemIndex;	
	}
}
