// chage these "url" values below to modify where the tabs redirect
// and the "newPage" values to tell the script whether to open the links in the current page or a new page
var links = {
	"1": {"url":"InBus.html", 		"newPage":false},
	"2": {"url":"InBus.html", 		"newPage":false},
	"3": {"url":"cercetare.html", 	"newPage":false},
	"4": {"url":"cercetare.html", 	"newPage":false},
	"5": {"url":"portofoliu.html", "newPage":false}
}

var cycleTime = 4000; // (milliseconds) Change this value here to make the tabs cycle faster or slower

// returns the index of the next tab in rotation
// if the current tab is the last one than it returns the index of the first tab (0)
function nextTabIndex(tabs, currentTabIndex) {
	if(currentTabIndex >= (tabs.tabs("length") - 1))
		return 0;
	return currentTabIndex + 1;
}

function goToLink(index) {
	$("#tabs-handle-" + index  + ", #link-" + index).click(function(){
		if(links[index]["newPage"])
			window.open(links[index]["url"], "_blank");
		else
			window.location.href = links[index]["url"];
	});
}

$(document).ready(function(){
	// add the links
	for(var key in links) {
		if (links.hasOwnProperty(key)) {
			goToLink(key);
		}
	}

	// add the tabs logic
	var currentTabIndex = 0;
	var isRotating = true;
	var tabs = $("#tabs").tabs({
			event: "mouseover",
			show: function(event, ui) {
				currentTabIndex = ui.index;
			}/*,
			fx: {
				opacity: "toggle",
				duration: 100
			}*/
	});

	// start the tabs rotation
	tabs.tabs("rotate", cycleTime, true);
	isRotating = true;

	// mouse is over a tab handle
	$(".ui-tabs-nav li a").mouseover(function(){
		// stop the rotation
		tabs.tabs("rotate", 0);
		isRotating = false;
	})

	// the mouse leaves the tabs area
	$(".slider-container").mouseleave(function(){
		// if the tabs rotation is stopped immediately change to the next tab
		if(isRotating == false)
			tabs.tabs("select", nextTabIndex(tabs, currentTabIndex));
		// start the rotation again
		tabs.tabs("rotate", cycleTime, true);
		isRotating = true;
	});
});
