
function initFoldingContent() {
	if ($("#twoColcontent .floatright a.menu").length) {

		$("#twoColcontent .floatright").children().hide();
		$("#twoColcontent .floatright a.menu").parents().show();
		$("#twoColcontent .floatright a.menu").show();
		$("#twoColcontent .floatright a.menu").addClass("closed");

		$("#twoColcontent .floatright a.menu").click(function() {
			if ($("#twoColcontent .floatright div").is(":visible")) {
				$("#twoColcontent .floatright").children().hide();
				$("#twoColcontent .floatright a.menu").parents().show();
				$("#twoColcontent .floatright a.menu").show();
				$("#twoColcontent .floatright a.menu").addClass("closed");
			} else {
				$("#twoColcontent .floatright").children().show();
				$("#twoColcontent .floatright a.menu").removeClass("closed");
			}
		});
	}
}

function showNextNews() {
    var classes = $(".newsNav.aktuell").attr("class");
    if (classes) {
        var current = parseInt(classes.replace(/[^0-9]/g, ""));
        var totalNews = $(".newsNav").size();
        if (current < totalNews) {
            showNews(current+1);
        } else {
            //showNews(1);
        }
    }
    return false;
}

function showPrevNews() {
    var classes = $(".newsNav.aktuell").attr("class");
    if (classes) {
        var current = parseInt(classes.replace(/[^0-9]/g, ""));
        var totalNews = $(".newsNav").size();
        if (current > 1) {
            showNews(current-1);
        } else {
            //showNews(totalNews);
        }
    }
    return false;
}

function showNews(num) {
    // check where we are
    var current = 0;
    var total = $(".newsNav").size();
    var classes = $(".newsNav.aktuell").attr("class");
    if (classes) {
        current = parseInt(classes.replace(/[^0-9]/g, ""));
    }
    if (!total || current == 0 || current == num) {
        return false;
    }

    // hide next/prev buttons
    if (total == num) {
        $("#next_button").hide();
    } else {
        $("#next_button").show();
    }
    if (num == 1) {
        $("#previous_button").hide();
    } else {
        $("#previous_button").show();
    }

    // toggle text and adjust dimensions afterwards
    $(".newsHdl").hide();
    $(".newsText").hide();
    $(".newsHdl.news"+num).show();
    $(".newsText.news"+num).show();
    adjustNewsImageDim();

    // move images
    var $curImgDiv = $(".bgimg.news"+current);
    var $nextImgDiv = $(".bgimg.news"+num);
    $curNewLeft = $curImgDiv.outerWidth();
    if (current < num) {
        $curNewLeft *= -1;
    }
    $nextImgDiv.css('left', $curNewLeft*-1);
    $nextImgDiv.show();
    $curImgDiv.stop(true, true).animate(
        {left: $curNewLeft},
        1000,
        function() {
            $curImgDiv.hide();
            adjustNewsImageDim();
        }
    );
    $nextImgDiv.stop(true).animate(
        {left: 0},
        1000
    );

    // set active navi
    $(".newsNav").removeClass('aktuell');
    $(".newsNav.news"+num).addClass('aktuell');

    return false;
}

function adjustNewsImageDim(recursion) {
    // vars
    var $width = $(window).width();
    var $height = $("#wrap").outerHeight();

    //$("#debug").html($(window).width() +"<br/>"+ $("#fullscreen").outerWidth());
    //$("#debug").html($(window).height() +"<br/>"+ $("#wrap").outerHeight() +"<br/>"+ $(".bgimg").height());

    // div width and height must not change
    $(".bgimg").css('width', $width);
    $(".bgimg").css('height', $height);
    $("#fullscreenleft").css('height', $height);

    // image width may change depending on it's height
    $(".bgimg img").css('width', $width);
    for (var i=0; i<$(".bgimg img").size(); i++ ) {
        var $hide = false;
        if ($(".bgimg:eq("+i+") img").height() == 0) {
            // div with image is not visible, move it ouside of the visible area and show it
            $(".bgimg:eq("+i+")").css('left', -$width);
            $(".bgimg:eq("+i+")").show();
            var $hide = true;
        }
        if ($(".bgimg:eq("+i+") img").height() < $height) {
            // increase width to match window height
            var $ratio = $(".bgimg:eq("+i+") img").height() / $width;
            $(".bgimg:eq("+i+") img").css('width', $height/$ratio);
        }
        if ($hide) {
            // hide div again, so that it does not become visible on resize
            $(".bgimg:eq("+i+")").hide();
        }
    }

    if (!recursion && $height < $("#wrap").outerHeight()) { // occurs sometimes
    	adjustNewsImageDim(true);
    }
    if (!recursion && $width < $(window).width()) { // if there was a scrollbar before the animation starts
    	adjustNewsImageDim(true);
    }
}

/**
 * scrolls down so that the video is fully visible
 */
function workDetailScrollDown() {
	if (!$("#endOfVideo").length) {
		return;
	}
	var $pos = $("#endOfVideo").offset().top - $('html, body').height();
	if ($pos > 0) {
		$('html, body').animate({ scrollTop: $pos}, 'fast');
	}
}

