var teaserReload = false;

startTeaserAnimation();

$(function() {

	$(".navigation li:has('ul')").hover(function() {
		$(this).addClass('current');
	}, function() {
		$(this).removeClass('current');
	});

	$(".navigation li ul li").hover(function() {
		$(this).addClass('hover');
		$(this).click(function() {
			var url = $("a", this).attr('href');
			window.location.href = url;
			return false;
		});
	}, function() {
		$(this).removeClass('hover');
	});

	$(".navigation li ul li:first-child").addClass('first');
	$(".navigation li ul li:last-child").addClass('last');

	$("#teaser div.teaserNavi .slideIcon").click(function() {
		if (teaserReload) {
			window.clearTimeout(teaserReload);
		}
		displayNextTeaser($(this).parent().attr('id'), 'off');
	});

});

function startTeaserAnimation() {
	teaserReload = window.setTimeout("displayNextTeaser('','on')", 6000);
}

function displayNextTeaser(idToOpen, autoplay) {
	var idToClose = $("#teaser div.teaserNavi ul li[class='active']")
			.attr('id');
	if (!idToOpen) {
		var idToOpen = $("#teaser div.teaserNavi ul li[class='active']").next()
				.attr('id');
		if (!idToOpen) {
			var idToOpen = "tn1";
		}
	}
	$("#" + idToClose + " a").animate( {
		opacity : 0
	}, 500, function() {
		$("#" + idToClose).animate( {
			width : '32px'
		}, 250, function() {
			$("#" + idToClose).addClass('inactive');
			$("#" + idToClose).removeClass('active');
			$("#" + idToOpen).animate( {
				width : '160px'
			}, 500, function() {
				$("#" + idToOpen).removeClass('inactive');
				$("#" + idToOpen).addClass('active');
				$("#" + idToOpen + " a").animate( {
					opacity : 1
				}, 0, function() {
					$("li[lang='" + idToClose + "']").fadeOut(function() {
						$("li[lang='" + idToOpen + "']").fadeIn(function() {
							if (autoplay == "on") {
								startTeaserAnimation();
							}
						});
					});
				});
			});
		});
	});
}
