var currActive = new Array;
var timers = new Array;

$(document).ready(function() {
	$("a.tilt").click(function(event) {
		event.preventDefault();
	});
	
	$("h3").each(function(i, item) {
		if ($(item).text().length == 0) {
			$(item).hide();
		}
	});
	
	$("table.versenynaptar tr").hover(function() {
		$(this).addClass("hvr");
		
		var rel = $(this).attr('rel');
		
		$("div#tracks div").hide();
		$("div#track_" + rel).show();
	}, function(){
		$(this).removeClass("hvr");
	});
	
	$("table.versenynaptar tr").click(function() {
		var rel = $(this).attr('rel');
		
		location.href = '?verseny/2009-truck-race-championship/' + rel + '.html';
	});
	
	setBox('news');
	setBox('pilotakbox');
	setBox('versenynaptarbox');
	setBox('manbox');
	
	$(".hirlevelgomb").click(function(){
		$.post("newsletter.php", { email: $("input[name=textfield]").val() }, function(data) {
			alert(data);
		});
	});
	
	$(".lightbox").lightbox();
});

function setBox(box) {
	var cnt = $("." + box).length;
	
	currActive[box] = 1;
	
	for (i = 1; i <= cnt; i++) {
		if (i != currActive[box]) {
			$("#" + box + "_" + i).hide();
		}
	}
	
	timers[box] = setInterval(function(){boxInterval(box, cnt);}, 10000);
	
	$("." + box + " div ul li a").click(function(event){
		event.preventDefault();
		
		var rel = $(this).attr("rel");
		var relparts = rel.split("_");
		
		if (relparts[1] != currActive[box] && relparts[1] <= cnt) {
			clearTimeout(timers[box]);
			
			for (i = 1; i <= cnt; i++) {
				if (i != currActive[box]) {
					$("#" + box + "_" + i).hide();
				}
			}
			
			var rel = $(this).attr("rel");
			var relparts = rel.split("_");
			
			$("#" + box + "_" + currActive[box]).css("z-index", "100");
			$("#" + rel).css("z-index", "99").show();
			
			$("#" + box + "_" + currActive[box]).fadeOut();
			
			currActive[box] = parseInt(relparts[1]);
			timers[box] = setInterval(function(){boxInterval(box, cnt);}, 10000);
		}
	});
}

function boxInterval(box, cnt) {
	var next = currActive[box] + 1;
	
	if (next > cnt) {
		next = 1;
	}
	
	$("." + box + " div ul li a[rel=" + box + "_" + next + "]").trigger("click");
}


