var headlineCount;
var headlineInterval;
var oldHeadline = 0;
var currentHeadline = 0;

function closeText(elem) {
	$(elem).parents(".toggle").slideUp(200, function() {
		$(this).prev().find(".fwd").show();
	});
}

function closeAllTexts() {
	$(".toggle").slideUp(200, function() {
		$("#left .fwd").show();
	});
}

function openAllTexts() {
	$(".toggle").slideDown(200);
	$("#left .fwd").hide();
}

function setContainerHeight() {
	$("#container").css("min-height", $(window).height() + "px");
}

function fadeInStatementHeader() {
	var img = $("#header-statement-2");
	img.css("opacity", 0);
	img.animate({opacity: 1}, {duration: 2500});
	//img.fadeIn(2500);
}

function toggleContent(h1, scroll) {
	var div = $(h1).next();
	var fwd = $(h1).find(".fwd");
	var showFwd = div.is(":visible");
	if (!showFwd) fwd.hide(); else fwd.show();
	div.toggle("fast", function() {
		var hl = $(this).prev();
		hl.find("span.ul").css("border-bottom-color", $(this).is(":visible") ? "black" : "rgb(196, 9, 29)");
		if (scroll) {
			$(window).scrollTo($(h1), "slow");
		}
	});
}

function gotoAnchor(anchor) {
	toggleContent(anchor, true);
}

function headlineRotate() {
	currentHeadline = (oldHeadline + 1) % headlineCount;
	$("#top li:eq(" + oldHeadline + ")")
	.animate({
		top: -50
	},1000, function() {
		$(this).css('top', '20px');
	});
	$("#top li:eq(" + currentHeadline + ")")
	.animate({
		top: 1
	},1000);
	oldHeadline = currentHeadline;
}

$(document).ready(function() {
	setContainerHeight();

	$("#left div.toggle").hide();
	$("#left .toggle-hl").click(function() {
		toggleContent(this, false);
	});
	
	$("#logo").hover(
		function() {
			$("#claim h1").animate({
				color: "#ffef38"
			});
			$("#claim ul").slideDown(200);
		},
		function() {
			$("#claim h1").animate({
				color: "#000000"
			});
			$("#claim ul").slideUp(200);
		}
		);

	$(".close-text").click(function() {
		$(this).parents(".toggle").slideUp(200, function() {
			var hl = $(this).prev();
			hl.find(".fwd").show();
			hl.find("span.ul").css("border-bottom-color", "rgb(196, 9, 29)");
		});
	});

	$("#statement #header").append("<img src='" + base + "img/header_statement_2.jpg' alt='' id='header-statement-2'/>");
	window.setTimeout(fadeInStatementHeader, 5000);

	setContainerHeight();

	// News
	headlineCount = $("#top li").size();
	$("#top li:eq(" + currentHeadline + ")").css('top', '0px');

	if (headlineCount > 1) {
		headlineInterval = setInterval(headlineRotate, 5000);
		$("#top").hover(function() {
			clearInterval(headlineInterval);
		}, function() {
			headlineInterval = setInterval(headlineRotate,5000);
			headlineRotate();
		});
	}

	//Kontaktformular
	$("input[name='Typ']").click(function() {
		$("fieldset:visible").hide("slow");
		$("#Options" + $(this).val()).show("slow");
	});

	//Anchors
	if (location.hash == "#imprint") {
		toggleContent($("#imprint"), true);
		toggleContent($("#disclaimer"), false);
	}
	
	//E-Mails
	$("a[href^=\"mailto\"]").each(function(){
		var elem = $(this);
		elem.attr("href", elem.attr("href").replace(/\-at\-Mittelpunkt/, "@Mittelpunkt"));
		elem.text(elem.text().replace(/\-at\-Mittelpunkt/, "@Mittelpunkt"));
	});

});

$(window).bind("resize", setContainerHeight);
