var HOME_SLIDESHOW_INTERVAL = 6000;
var slideshow = null;

$(document).ready(function() {

	// code für display bock_right block und none
	$('a.toggleBtn').click(function() {
		$('div.newsItem').removeClass('active');
		$('a.toggleBtn').show();
		$(this).closest('div.newsItem').addClass('active');
		$(this).hide();
	});

	$('div.home_bildContainerWrapper a.btnLeft').click(function() {
		if (!$('#home_bildContainer').hasClass('moving')) {
			slideshowPrev();
		}

	});

	$('div.home_bildContainerWrapper a.btnRight').click(function() {
		if (!$('#home_bildContainer').hasClass('moving')) {
			slideshowNext();
		}
	});

	$('div.home_bildContainerWrapper').mouseenter(function() {
		$(this).addClass('mouseOver');
		clearSlideshow();
	});

	$('div.home_bildContainerWrapper').mouseleave(function() {
		$(this).removeClass('mouseOver');
		activateSlideshow();
	});

	$('a.stueck_metaTools_info').click(function(e) {
		e.stopPropagation();
	});

	$('div.infoLayer').click(function(e) {
		e.stopPropagation();
	});

	$('div.standardformular_row div.datum_kalender').click(function(e) {
		e.stopPropagation();
	});

	$('div.socialPopup').click(function(e) {
		e.stopPropagation();
	});

	$('div.socialMedialinks a').click(function(e) {
		e.stopPropagation();
	});

	$('a.stueck_metaTools_info').click(function(e) {
		e.stopPropagation();
	});

	var searchtextInput = $('div.searchForm input.text');
	var searchtextInput_default = searchtextInput.val();


	searchtextInput.focus(function() {
		if (searchtextInput.val() == searchtextInput_default) searchtextInput.val("");
	}).blur(function() {
		if (searchtextInput.val().length == 0) searchtextInput.val(searchtextInput_default);
	});

	checkSlideshowPosition();
	activateSlideshow();

	ReplaceAllHeadlines();
});

function showCalenderDayInfo(CalenderInfoHtmlId) {
    $('div.eventBox').removeClass('eventBox_active');
    $('#' + CalenderInfoHtmlId).addClass('eventBox_active');
}

function activateSlideshow() {
    clearSlideshow();
    slideshow = window.setInterval('slideshowAutoNext();', HOME_SLIDESHOW_INTERVAL);
}

function clearSlideshow() {
    window.clearInterval(slideshow);
}

function slideshowAutoNext() {
    if (!$('div.home_bildContainerWrapper').hasClass('mouseover') && !isRightEndOfSlideshow()) {
        slideshowNext();
    }
}

function slideshowNext() {
    if (!$('#home_bildContainer').hasClass('moving')) {
        $('#home_bildContainer').addClass('moving');
        $('#home_bildContainer').animate({ marginLeft: '-=698px' }, 1000, function() {
            checkSlideshowPosition();
            $('#home_bildContainer').removeClass('moving');
        });
    }
}

function slideshowPrev() {
    if (!$('#home_bildContainer').hasClass('moving')) {
        $('#home_bildContainer').addClass('moving');
        $('#home_bildContainer').animate({ marginLeft: '+=698px' }, 1000, function() {
            checkSlideshowPosition();
            $('#home_bildContainer').removeClass('moving');
        });
    }
}

function checkSlideshowPosition() {
    if (isLeftEndOfSlideshow())
        $('#home_bildContainer').css('marginLeft', (2 * 698 - parseInt($('#home_bildContainer').css('width'))) + 'px');

    else if (isRightEndOfSlideshow())
        $('#home_bildContainer').css('marginLeft', '-698px');
}

function isRightEndOfSlideshow() {
    if (parseInt($('#home_bildContainer').css('marginLeft')) + parseInt($('#home_bildContainer').css('width')) <= 698)
        return true

    return false;
}

function isLeftEndOfSlideshow() {
    if (parseInt($('#home_bildContainer').css('marginLeft')) >= 0)
        return true

    return false;
}

/***/
function openPopupLayer(popupHtmlID) {
    $('div#site_overlay_scrollbugfix').show();
    $('div#site_overlay').show();
    $('div#site_overlay').attr('onclick', "closePopupLayerById('" + popupHtmlID + "'); return false;"); /***/
    $('div#site_overlay_scrollbugfix').attr('onclick', "closePopupLayerById('" + popupHtmlID + "'); return false;"); /***/
}

function closePopupLayerByIcon(obj) {
    closePopupLayer($(obj).closest('div.popupLayer'));
}

function closePopupLayerById(popupHtmlID) {
    closePopupLayer($('#' + popupHtmlID));
}

function closePopupLayer(obj) {
    $(obj).hide();
    $('div#site_overlay').hide();
    $('div#site_overlay_scrollbugfix').hide();
    $('div#site_overlay').attr('onclick', '');
    $('div#site_overlay_scrollbugfix').attr('onclick', '');
}

function showInfoLayer(obj, layerHtmlId) {
    $('div.infoLayer').hide();
    $(obj).blur();
    $('div#' + layerHtmlId).show();
    $(obj).click(function(event) { event.stopPropagation(); });
    
    $('html').click(function() {
        $('div.infoLayer').hide();
        $('html').unbind('click');
    });
}

function closeInfoLayer(obj) {
    $('div.infoLayer').hide();
    $('html').unbind('click');
}

function getYScrollPos() {
    var yScrollPos = 0;
    if (window.pageYOffset) yScrollPos = window.pageYOffset;
    else if (document.documentElement.scrollTop) yScrollPos = document.documentElement.scrollTop;
    return parseInt(yScrollPos);
}

function toggleSocialPopup(name) {
    var divname = "socialPopup_" + name;
    if ($('div#' + divname).hasClass('socialPopup_visible')) {
        $('div#' + divname).removeClass('socialPopup_visible');
    } else {
        $('div.socialPopup').removeClass('socialPopup_visible');
        $('div#' + divname).addClass('socialPopup_visible');
        $('html').click(function() {
            $('div.socialPopup').removeClass('socialPopup_visible');
            $('html').unbind('click');
        });
    }
}

function closeSocialPopup(obj) {
    $(obj).parent().removeClass('socialPopup_visible');
}

function showKalender(obj) {
	$(obj).closest('div.datum_kalender').addClass('datum_kalender_visible');
	$(obj).click(function(event) { event.stopPropagation(); });
	$('html').click(function() {
		$('div.datum_kalender').removeClass('datum_kalender_visible');
		$('html').unbind('click');
	});
}

function closeKalender(obj) {
	$(obj).closest('div.datum_kalender').removeClass('datum_kalender_visible');
	$('html').unbind('click');
}

function setKalenderDate(obj, date) {
	$(obj).closest('div.datum_kalender').find('input.datum_kalender').attr('value', date);
	closeKalender(obj);
}
