var animatedIems = Array();

function animation() {
    if ((HOME_FADE_IN == null) || (typeof (HOME_FADE_IN) == 'undefined') || !HOME_FADE_IN)
        return false;

    var animatedTeasers = Array();
    var animatedContent = Array();
    for (var i = 0; i < animatedIems.length; i++) {
        if (animatedIems[i].toString().indexOf("animatedteaser_") >= 0)
            animatedTeasers.push(animatedIems[i]);
        if (animatedIems[i].toString().indexOf("animatedcontent_") >= 0)
            animatedContent.push(animatedIems[i]);
    }

    var animatedElements = Array();
    var j = 1;
    var k = 1;
    while (j <= animatedTeasers.length || k <= animatedContent.length) {
        if ((j <= k * 3 || k > animatedContent.length) && j <= animatedTeasers.length) {
            animatedElements.push(animatedTeasers[j - 1]);
            j++;
            continue;
        }
        animatedElements.push(animatedContent[k - 1]);
        k++;
    }

    for (var i = 0; i < animatedElements.length; i++) {
        $("#" + animatedElements[i]).css({ "visibility": "visible" }, { queue: "global" }),
		$("#" + animatedElements[i]).animate({ opacity: 1.0 }, { queue: "global" }, 1000);
    }
}

function setAnimationVisibility(itemID) {
    if ((HOME_FADE_IN == null) || (typeof (HOME_FADE_IN) == 'undefined') || !HOME_FADE_IN)
        return false;
    animatedIems.push(itemID);
    addCSSClass('teaserboxInitOpacity_0', getEl(itemID));
}

function startContentFadeIn() {
    ExtendedStartContentFadeIn(true);
}

function ExtendedStartContentFadeIn(allFlashLayers) {
    //	flashObject = getFlashMovieObject('flash_home_wuerfel');
	flashObjectWrapper = getEl('flash_home_wuerfel_wrapper');
	addCSSClass('flash_home_wuerfel_wrapper_small', flashObjectWrapper);
	showInitLayer(allFlashLayers);
	animation();
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName])
		return window.document[movieName];

	if (navigator.appName.indexOf("Microsoft Internet")==-1)
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];

	else
		return document.getElementById(movieName);
}

function showInitLayer(allFlashLayers) {
    if (allFlashLayers) {
        for (var i = 0; i < popupLayer_flashContainer_arr.length; i++)
            toggleFlashLayer(popupLayer_flashContainer_arr[i]);
    }
    else if (popupLayer_flashContainer_arr.length > 0)
        toggleFlashLayer(popupLayer_flashContainer_arr[0]);

	for (var i=0; i<popupLayer_bildContainer_arr.length; i++)
		toggleLayer(popupLayer_bildContainer_arr[i]);

	for (var i=0; i<popupLayer_spielplan_arr.length; i++)
		toggleSpielplanLayer(popupLayer_spielplan_arr[i]);
}


/*
$(document).ready(function(){
//	$.fxqueue("global").start();
	animation();
});
*/

function printWindow(obj) {
	obj.blur();
	window.print();
	return false;
}

function JSLink(link, target) {
	if (target=='_blank')
		window.open(Adresse, target);
	else
		document.location.href = link;
}

var bildergalerieIsBusy = false;
var FLASH_POPUP_ACTIVE = false;
var LAYER_COUNT = 0;
var LAYER_TOTAL = 0;
var CURRENT_LAYER = 0;

var BILDERGALERIE_AUTO_DELAY = 4000;

$(document).ready(function() {
    if ((typeof (IS_HOME) != 'undefined') && (!IS_HOME)) showInitLayer(true);

    checkLayerCount();
    //	initGlobalLayerSwitch();

    if (LAYER_TOTAL == 0) {
        $("#globalLayerSwitch a.EinAusBlenden").css('display', 'none');
        if (document.getElementById('btnWarenkorb') == null) $("#globalLayerSwitch span#EinAusBlendenSpacer").css('display', 'none');
    }

    $("#globalLayerSwitch a.EinAusBlenden").click(function() {
        $(this).blur();
        checkLayerCount();
        if (LAYER_COUNT == 0) {
            if (CURRENT_LAYER > 0)
                toggleFlashLayer(CURRENT_LAYER);
            $("div.popupLayer").css('display', 'block');
            $("div.stdEl_bildContainer div.openPopupLink_titel").addClass('openPopupLinkTitel_active');
            $("div.spielplan_stueck p.spielplan_stueck_datum a.hinweis").addClass('hinweis_eingeblendet');
            $("div#globalLayerSwitch a.EinAusBlenden").removeClass('alleEinblenden');
            $("div#globalLayerSwitch a.EinAusBlenden").addClass('alleAusblenden');
        }
        else {
            if (CURRENT_LAYER > 0)
                closeLayer(CURRENT_LAYER);
            $("div.popupLayer").css('display', 'none');
            $("div.popupLayer_sml").css('display', 'none');
            $("div.stdEl_bildContainer div.openPopupLink_titel").removeClass('openPopupLinkTitel_active');
            $("div.spielplan_stueck p.spielplan_stueck_datum a.hinweis").removeClass('hinweis_eingeblendet');
            $("div#globalLayerSwitch a.EinAusBlenden").removeClass('alleAusblenden');
            $("div#globalLayerSwitch a.EinAusBlenden").addClass('alleEinblenden');
        }
        return false;
    });

    $("#bildergaleriePopup_fade_links, .bildergaleriePopup_buttonLeft").click(function() {
        $(this).get(0).blur();
        if ((!bildergalerieIsBusy) && ((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left')) / 600)) + 1) > 1)) {
            bildergalerieIsBusy = true;
            $("#bildergaleriePopup_fade_rechts").removeClass('fade_noCursor');
            $("#bildergaleriePopup_buttonRight").removeClass('bildergaleriePopup_buttonRight_disabled');
            $(".bildergalerieEntry_fading").animate({ opacity: "0" }, { duration: 250, queue: "bildergalerie" });
            $("#bildergaleriePopup_contentWrapper").animate({ left: "+=600px" }, { duration: 600, queue: "bildergalerie" });
            // next line: "0" = count of html-elements in wrapper before entries
			$(".bildergalerieEntry").eq(0+Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left'))/600))-1).find(".bildergalerieEntry_fading").animate({opacity: "1"}, {duration: 250, queue: "bildergalerie"}, "", function() {
                if ((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left')) / 600)) + 1) == 1) {
                    $("#bildergaleriePopup_fade_links").addClass('fade_noCursor');
                    $("#bildergaleriePopup_buttonLeft").addClass('bildergaleriePopup_buttonLeft_disabled');
                }
                else {
                    $("#bildergaleriePopup_fade_links").removeClass('fade_noCursor');
                    $("#bildergaleriePopup_buttonLeft").removeClass('bildergaleriePopup_buttonLeft_disabled');
                }
                bildergalerieIsBusy = false;
            });
        }
    });

    $("#bildergaleriePopup_fade_rechts, .bildergalerieEntry_image, .bildergaleriePopup_buttonRight").click(function() {
        $(this).get(0).blur();
        if ((!bildergalerieIsBusy) && ((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left')) / 600)) + 1) < Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('width')) / 600))) {
            bildergalerieIsBusy = true;
            $("#bildergaleriePopup_fade_links").removeClass('fade_noCursor');
            $("#bildergaleriePopup_buttonLeft").removeClass('bildergaleriePopup_buttonLeft_disabled');
            $(".bildergalerieEntry_fading").animate({ opacity: "0" }, { duration: 250, queue: "bildergalerie" });
            $("#bildergaleriePopup_contentWrapper").animate({ left: "-=600px" }, { duration: 600, queue: "bildergalerie" });
            // next line: "0" = count of html-elements in wrapper before entries
			$(".bildergalerieEntry").eq(0+Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left'))/600))+1).find(".bildergalerieEntry_fading").animate({opacity: "1"}, {duration: 250, queue: "bildergalerie"}, "", function() {
                if ((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left')) / 600)) + 1) == Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('width')) / 600)) {
                    $("#bildergaleriePopup_fade_rechts").addClass('fade_noCursor');
                    $("#bildergaleriePopup_buttonRight").addClass('bildergaleriePopup_buttonRight_disabled');
                    if ((typeof (bildergalerieAuto) != 'undefined') && (bildergalerieAuto != null))
                        window.clearInterval(bildergalerieAuto);
                    $("#bildergaleriePopup_buttonMiddle").removeClass("bildergaleriePopup_buttonMiddle_isPlaying");
                }
                else {
                    $("#bildergaleriePopup_fade_rechts").removeClass('fade_noCursor');
                    $("#bildergaleriePopup_buttonRight").removeClass('bildergaleriePopup_buttonRight_disabled');
                }
                bildergalerieIsBusy = false;
            });
        }
    });

    $("#bildergaleriePopup_buttonMiddle").click(function() {
        $(this).get(0).blur();
        if ($(this).hasClass("bildergaleriePopup_buttonMiddle_isPlaying")) {
            if ((typeof (bildergalerieAuto) != 'undefined') && (bildergalerieAuto != null))
                window.clearInterval(bildergalerieAuto);
            $(this).removeClass("bildergaleriePopup_buttonMiddle_isPlaying");
        }
        else {
            $(this).addClass("bildergaleriePopup_buttonMiddle_isPlaying");
            bildergalerieAutoNext();
            bildergalerieAuto = window.setInterval("bildergalerieAutoNext()", BILDERGALERIE_AUTO_DELAY);
        }
    });

});

function bildergalerieAutoNext() {
	if ((!bildergalerieIsBusy)&&((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left'))/600))+1)<Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('width'))/600))) {
		bildergalerieIsBusy = true;
		$("#bildergaleriePopup_fade_links").removeClass('fade_noCursor');
		$("#bildergaleriePopup_buttonLeft").removeClass('bildergaleriePopup_buttonLeft_disabled');
		$(".bildergalerieEntry_fading").animate({opacity: "0"}, {duration: 250, queue: "bildergalerie"});
		$("#bildergaleriePopup_contentWrapper").animate({left: "-=600px"}, {duration: 600, queue: "bildergalerie"});
	// next line: "0" = count of html-elements in wrapper before entries
		$(".bildergalerieEntry:nth-child("+(0+Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left'))/600))+2)+") .bildergalerieEntry_fading").animate({opacity: "1"}, {duration: 250, queue: "bildergalerie"}, "", function() {
			if ((Math.abs(Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('left'))/600))+1)==Math.floor(parseInt($("#bildergaleriePopup_contentWrapper").css('width'))/600)) {
				$("#bildergaleriePopup_fade_rechts").addClass('fade_noCursor');
				$("#bildergaleriePopup_buttonRight").addClass('bildergaleriePopup_buttonRight_disabled');
				if ((typeof (bildergalerieAuto) != 'undefined') && (bildergalerieAuto != null))
				    window.clearInterval(bildergalerieAuto);
				$("#bildergaleriePopup_buttonMiddle").removeClass("bildergaleriePopup_buttonMiddle_isPlaying");
			}
			else {
				$("#bildergaleriePopup_fade_rechts").removeClass('fade_noCursor');
				$("#bildergaleriePopup_buttonRight").removeClass('bildergaleriePopup_buttonRight_disabled');
			}
			bildergalerieIsBusy = false;
		});
	}
}

function unsetCurrentPicture() {
	getEl('popupTitel'+CURRENT_LAYER).style.display = 'none';
	removeCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+CURRENT_LAYER));
	if (FLASH_POPUP_ACTIVE) getEl('popupLayer'+CURRENT_LAYER).style.display = 'none';
	checkLayerCount();
}

function setCurrentPicture(Nr) {
	$("div.flashAnimationPopupContainer div.popupLayer").css("display", "none");
	$("div.flashAnimationTitelContainer div.openPopupLink_titel").css("display", "none");
	$("div.flashAnimationTitelContainer div.openPopupLink_titel").removeClass("openPopupLinkTitel_active");
	
	CURRENT_LAYER = Nr;
	getEl('popupTitel'+CURRENT_LAYER).style.display = 'block';
	if (FLASH_POPUP_ACTIVE) {
		getEl('popupLayer'+CURRENT_LAYER).style.display = 'block';
		addCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+CURRENT_LAYER));
	}
	checkLayerCount();
}

function toggleFlashLayer(id) {
	if (getEl('popupTitel'+id)!=null) {
		if (hasCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id))) {
			getEl('popupLayer'+id).style.display = 'none';
			removeCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id));
			FLASH_POPUP_ACTIVE = false;
		}
		else {
			getEl('popupLayer'+id).style.display = 'block';
			addCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id));
			FLASH_POPUP_ACTIVE = true;
		}
	}
	checkLayerCount();
	return false;
}

function toggleLayer(id) {
	if (getEl('popupTitel'+id)!=null) {
		if (hasCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id))) {
			getEl('popupLayer'+id).style.display = 'none';
			removeCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id));
		}
		else {
			getEl('popupLayer'+id).style.display = 'block';
			addCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id));
		}
	}
	checkLayerCount();
	return false;
}

function toggleSpielplanLayer(id, obj) {
	if (typeof(obj)!='undefined') obj.blur();
	if (getEl('popupSpielplanTitel'+id)!=null) {
		if (hasCSSClass('hinweis_eingeblendet', getEl('popupSpielplanTitel'+id))) {
			getEl('popupLayer'+id).style.display = 'none';
			removeCSSClass('hinweis_eingeblendet', getEl('popupSpielplanTitel'+id));
		}
		else {
			getEl('popupLayer'+id).style.display = 'block';
			addCSSClass('hinweis_eingeblendet', getEl('popupSpielplanTitel'+id));
		}
	}
	checkLayerCount();
	return false;
}

function toggleH2Layer(id, obj) {
	if (typeof(obj)=='undefined') return false;
	obj.blur();
	if (getEl('popupTitel'+id)!=null) {
		if (hasCSSClass('stdEl_openPopup_active', getEl('popupTitel'+id))) {
			getEl('popupLayer'+id).style.display = 'none';
			removeCSSClass('stdEl_openPopup_active', getEl('popupTitel'+id));
		}
		else {
			getEl('popupLayer'+id).style.top = getYPos(obj)+19+'px';
			getEl('popupLayer'+id).style.display = 'block';
			addCSSClass('stdEl_openPopup_active', getEl('popupTitel'+id));
		}
	}
	checkLayerCount();
	return false;
}

function closeLayer(id) {
	getEl('popupLayer'+id).style.display = 'none';
	removeCSSClass('openPopupLinkTitel_active', getEl('popupTitel'+id));
	if(hasCSSClass('popupLayerFlashbox', getEl('popupLayer'+id))) FLASH_POPUP_ACTIVE = false;
	checkLayerCount();
	return false;
}

function closeSpielplanLayer(id) {
	getEl('popupLayer'+id).style.display = 'none';
	removeCSSClass('hinweis_eingeblendet', getEl('popupSpielplanTitel'+id));
	checkLayerCount();
	return false;
}

function closeH2Layer(id) {
	getEl('popupLayer'+id).style.display = 'none';
	removeCSSClass('stdEl_openPopup_active', getEl('popupTitel'+id));
	checkLayerCount();
	return false;
}


function checkLayerCount() {
	LAYER_COUNT = $("div.popupLayer:visible").size();
	LAYER_TOTAL = $("div.popupLayer").size();
	
	if (LAYER_COUNT == 0) {
	    $("div#globalLayerSwitch a.EinAusBlenden").removeClass('alleAusblenden');
	    $("div#globalLayerSwitch a.EinAusBlenden").addClass('alleEinblenden');
	}
	else {
	    $("div#globalLayerSwitch a.EinAusBlenden").removeClass('alleEinblenden');
	    $("div#globalLayerSwitch a.EinAusBlenden").addClass('alleAusblenden');
	}
}

function closeBildergalerie() {
	getEl('bildergaleriePopup_container').style.display = 'none';
	getEl('darken_site').style.display = 'none';
	getEl('scrollbugfix').style.display = 'none';
	return false;
}

function openBildergalerie() {
	getEl('darken_site').style.display = 'block';
	getEl('scrollbugfix').style.display = 'block';
	getEl('bildergaleriePopup_container').style.display = 'block';
}

function closeVideoPlayer() {
	getEl('videoPlayerPopup_container').style.display = 'none';
	getEl('darken_site').style.display = 'none';
	getEl('scrollbugfix').style.display = 'none';
	return false;
}

function openVideoPlayer() {
	getEl('darken_site').style.display = 'block';
	getEl('scrollbugfix').style.display = 'block';
	getEl('videoPlayerPopup_container').style.display = 'block';
}

function closeLivesuche() {
	getEl('livesuche_wrapper').style.display = 'none';
	return false;
}

function prepareVideoLayer(is43Format, titleID) {

    jQuery.fn.exists = function() { return jQuery(this).length > 0; }

    $("div.videoPlayerEntry_headerContent").css("display", "none");
    if (is43Format)
        $("div#videoPlayerPopup_container").addClass("videoPlayerPopup43_container");
    else
        $("div#videoPlayerPopup_container").removeClass("videoPlayerPopup43_container");

    var o = getEl('VideoTitle_' + titleID);  
    if (o != null && typeof(o) != 'undefined')
        getEl('VideoTitle_' + titleID).style.display = 'block';
    if (!($("div#flashContent_videoplayer_container div#flashContent_videoplayer").exists()))
        getEl('flashContent_videoplayer_container').innerHTML = '<div id="flashContent_videoplayer"></div>';
}

function openDateDetailLayer(id, obj) {
	if (typeof(obj)!='undefined') obj.blur();
	layerObj = getEl('popupLayer'+id);
	if ((layerObj!=null)&&(typeof(layerObj)!='undefined')) {
		layerObj.parentNode.style.left = getXPos(obj)+596+'px';
		layerObj.parentNode.style.top = getYPos(obj)+204+'px';
		$("div.popupLayerCalendar").css("display", "none");
		layerObj.parentNode.style.display = 'block';
		layerObj.style.display = 'block';
	}
	return false;
}

function closeDateDetailLayer(id) {
	getEl('popupLayer'+id).style.display = 'none';
	return false;
}

function openAnmeldeLayer(id, obj) {
	if (typeof(obj)!='undefined') obj.blur();
	layerObj = getEl('popupLayer'+id);
	if ((layerObj!=null)&&(typeof(layerObj)!='undefined')) {
		safari_x_korrektur = 0;
		safari_y_korrektur = 0;
		layerObj.style.left = 799 - obj.parentNode.offsetWidth - getXPos(obj) + safari_x_korrektur+'px';
		layerObj.style.top = getYPos(obj)+164+safari_y_korrektur+'px';
		layerObj.style.display = 'block';
	}
	return false;
}

function closeAnmeldeLayer(id) {
	getEl('popupLayer'+id).style.display = 'none';
	return false;
}


function OpenFacebookWindow(FacebookLink, WindowTitle) {
    var facebookWindow = window.open(FacebookLink, '', "width=500,height=400,left=100,top=200");
    facebookWindow.focus(); 
}

function ReplaceHeadline(ItemID, Width, Height, ImageUrl, AltText) {
    var item = getEl(ItemID);
    internalReplaceHeadline(item, Width, Height, ImageUrl, AltText);
}

function ReplaceHeadlineWithinLink(ItemID, Width, Height, ImageUrl, AltText) {
    var item = getEl(ItemID);
    if ((item != null) && (typeof (item) != 'undefined')) {
        var itemFound = false;
        for (child in item.childNodes) {
            var tagName = item.childNodes[child].tagName;
            if ((tagName != null) && (typeof (tagName) != 'undefined') && tagName.toUpperCase() == 'A') {
                internalReplaceHeadline(item.childNodes[child], Width, Height, ImageUrl, AltText);
                itemFound = true;
                break;
            }
        }
        if (!itemFound) {
            internalReplaceHeadline(item, Width, Height, ImageUrl, AltText);
        }
    }
}

function ReplaceHeadlineWithinSpan(ItemID, Width, Height, ImageUrl, AltText) {
    var item = getEl(ItemID);
    if ((item != null) && (typeof (item) != 'undefined')) {
        var itemFound = false;
        for (child in item.childNodes) {
            var tagName = item.childNodes[child].tagName;
            if ((tagName != null) && (typeof (tagName) != 'undefined') && tagName.toUpperCase() == 'SPAN') {
                internalReplaceHeadline(item.childNodes[child], Width, Height, ImageUrl, AltText);
                itemFound = true;
                break;
            }
        }
        if (!itemFound) {
            internalReplaceHeadline(item, Width, Height, ImageUrl, AltText);
        }
    }
}

function internalReplaceHeadline(Item, Width, Height, ImageUrl, AltText) {
    if ((Item != null) && (typeof (Item) != 'undefined')) {
        var width = '';
        var height = '';
        if (Width > 0)
            width = ' width="' + Width.toString() + '"';
        if (Height > 0)
            height = ' height="' + Height.toString() + '"';
        Item.innerHTML = '<img' + width + height + ' border="0" alt="' + AltText + '" src="' + ImageUrl + '">';
    }
}
