var DwApetito = {
    
    controlSeoBoxToggle : function()
    {
        var _oSeoInfoBox = $('.SeoInfoBox');
        var _oSeoInfoHeadline = _oSeoInfoBox.find('h3.SeoInfoHeadline');
        var _oSeoInfoText = _oSeoInfoBox.find('h3.SeoInfoText');
        
        _oSeoInfoBox.click(function()
        {
            if (_oSeoInfoBox.hasClass('Active')) {
                _oSeoInfoBox.removeClass('Active');
            } else {
                _oSeoInfoBox.addClass('Active');
            }
            
            return false;
        });
    },
    
    
    
    ///
    /// teaser box with tabs and carousel inside
    ///
    tabbedContentInit : function()
    {
    	var _oInfoBoxLeft = $('.InfoBoxLeft');
    	var _oTabs = $(_oInfoBoxLeft).find('ul.HeaderTabList');
    	var _iWidthOfScrollElement = 460;
    	
    	/////// initiate tab toggling
    	_oTabs.find("li>a").click(function(){
    		
    		_oInfoBoxLeft.find(".TabContent").removeClass("ActiveTab").addClass("InactiveTab");
    		
        	var _sThisLink = $(this).attr("rel");
        	
    		_oTabs.find("li").removeClass("Active");
    		$(this).closest("li").addClass("Active");
    		_oInfoBoxLeft.find(".TabContent" + _sThisLink).removeClass("InactiveTab").addClass("ActiveTab");
    		
    		return false;    		
    	});
    	
    	////set size, so that list can float
    	_oInfoBoxLeft.find(".ScrollItemsWrapper").each(function(){
    		
    		var _iLength = $(this).find(".ScrollerItem").length;
    		$(this).css("width", (_iLength * 460) + "px");
    		if (_iLength > 1) $(this).closest(".ScrollContent").find(".ScrollRight").addClass("ActiveArrow");
    	});
    
    	///// function to scroll content right
    	_oInfoBoxLeft.find(".ScrollContent .ScrollRight a").click(function(){
    		
    		// don't do anything if inactive
    		if (!$(this).closest('div').hasClass("ActiveArrow")) return false; 
    		
    		_oInfoBoxLeft.find(":animated").stop(true,true);
    		
    		var _oThisScrollerWrapper = $(this).closest(".ScrollContent").find(".ScrollItemsWrapper");
    		
    		/// hide button if no more elements to the right
    		var _iMrgLft = parseInt(_oThisScrollerWrapper.css("marginLeft"));
    		var _iLastPos = -( parseInt(_oThisScrollerWrapper.width()) - 2*_iWidthOfScrollElement ) ;
    		
    		if (_iMrgLft == _iLastPos) $(this).closest(".ScrollRight").removeClass("ActiveArrow");
    		
    		// show left button
    		$(this).closest(".ScrollContent").find(".ScrollLeft").addClass("ActiveArrow");
    		
    		//move content left
    		var _iThisMargin = parseInt(_oThisScrollerWrapper.css("margin-left"));
    		_oThisScrollerWrapper.animate({'margin-left':_iThisMargin - _iWidthOfScrollElement});
    		
    		return false;
    	});
    	
    	////// function to scroll content left
    	_oInfoBoxLeft.find(".ScrollContent .ScrollLeft a").click(function(){
    		
    		// don't do anything if inactive
    		if (!$(this).closest('div').hasClass("ActiveArrow")) return false; 
    		
    		_oInfoBoxLeft.find(":animated").stop(true,true);
    		
    		var _oThisScrollerWrapper = $(this).closest(".ScrollContent").find(".ScrollItemsWrapper");
    		
    		/// hide button if no more elements to the left
    		var _iMrgLft = parseInt(_oThisScrollerWrapper.css("marginLeft"));
    		var _iLastPos = - ( _iWidthOfScrollElement ) ;
    		
    		if (_iMrgLft == _iLastPos) $(this).closest(".ScrollLeft").removeClass("ActiveArrow");
    		
    		// show right button
    		$(this).closest(".ScrollContent").find(".ScrollRight").addClass("ActiveArrow");
    		
    		//move content right
    		var _iThisMargin = parseInt(_oThisScrollerWrapper.css("margin-left"));
    		_oThisScrollerWrapper.animate({'margin-left':_iThisMargin + _iWidthOfScrollElement});
    		
    		return false;
    	});
    },
    
    ///
    /// function to switch between images (via tabs) in related content module m6
    ///
    rcM6ImageTabsInit : function() 
    {
    	$('.RelatedContentPicTeaser .PicTeaserTabHeader>li:eq(0)').addClass("FirstElement");
    	
    	$('.RelatedContentPicTeaser .RelatedContentHead').attr("class","RelatedContentHeadFirstElement");
    	
    	$('.RelatedContentPicTeaser .PicTeaserTabHeader>li>a').click(function(){
    		
    		var _oThisWrapper = $(this).closest(".RelatedContentPicTeaser");
    		var _iThisElementIndex = $(this).closest('li').index();
    		//var _iThisElementIndex =  _oThisWrapper.find("ul>li>a").index(this);
    		
    		if (_iThisElementIndex == 0) _oThisWrapper.find('.RelatedContentHead').attr("class","RelatedContentHeadFirstElement");
    		else _oThisWrapper.find('.RelatedContentHeadFirstElement').attr("class","RelatedContentHead");
    		
    		_oThisWrapper.find("li.Active").removeClass("Active");
    		$(this).closest("li").addClass("Active");
    		
    		_oThisWrapper.find(".PicTeaserImageContent .Img").removeClass("Active");
    		
    		_oThisWrapper.find(".PicTeaserImageContent .Img:eq("+ _iThisElementIndex +")").addClass("Active");
    		
    		return false;
    	});
    },
    
    ///
    /// function to scroll items of content scroller
    ///
    contentSliderInit : function()
    {
    	
    	var slideDirection = 1; // 1 for right, -1 for left; this is just for initiating the variable
    	
    	// return if no such element on this page
    	if($('.ContentSliderElement').length < 0 ) return false;
    	
    	// initiate for each instance of slider on page
    	$('.ContentSliderElement .MainSliderContentWrapper .SliderContentItemsWrapper').each(function(){
    		
    		var oThisMainEl = $(this).closest('.ContentSliderElement');
    		var bHasInContentNumbering = ( $(oThisMainEl).find('.InContentNumbering').length > 0 );
    		
    		// add inline numbering
    		if (bHasInContentNumbering) {
    			
    			iContItemsLength = oThisMainEl.find(".ContentItem").length;
    			oThisMainEl.find(".ContentItem:eq(0) .InContentNumbering").text( "1/" + iContItemsLength + " " );
    		}
    		
    		// set width of wrapper to enable sliding
    		var iNewWidth = 0;
    		$(this).find('.ContentItem').each(function(){
    			
    			iNewWidth += $(this).width();
    			if ($(oThisMainEl).hasClass("HasMarginBetweenElements")) iNewWidth += DwApetito.getMarginFromMainClass(oThisMainEl);
    		});
    		//iNewWidth = ( $(this).find('.ContentItem').length ) * 463;
    		$(this).css("width", iNewWidth + "px");
    	
	    	// set first element active
	    	$(this).find(".ContentItem:eq(0)").addClass("Active");
	    	
	    	// disable unused arrows
	    	oThisMainEl.find('.LeftArrow').addClass("Inactive");
	    	if ( $(this).find('.ContentItem').length < 2 )
	        	$(this).closest('.ContentSliderElement').find('.RightArrow').addClass('Inactive');
	    	
	    	// build pagination
	    	oThisMainEl.find('.ContentItem').each(function(){
	    		var _self = this;
	    		oThisMainEl.find('.PageList').each(function(){ 
	    			$(this).find('.RemoveMe').remove();
	    			var sActualHTML = $(this).html(); 
	    			$(this).html('<li><a rel="' +  $(_self).index() + '" href="#">' + ($(_self).index()+1) + '</li>' + sActualHTML);
	    		});
	    	});
	    	
	    	// add classes for the Pagination
	    	oThisMainEl.find('.PageList').each(function(){
	    		$(this).find('li:eq(0)').addClass("Last");
	    		$(this).find('li:last').addClass("Active");
	    	});
	    	
	    	// set numbers for pages w/ many elements
	    	DwApetito.actualizePagination(oThisMainEl);
    	});

    	// Left Arrow Click Event
    	$('.ContentSliderElement .LeftArrow a').click(function(){
    		
    		$(this).blur();
    		
    		// don't do anything if is inactive
    		if ($(this).closest('.LeftArrow').hasClass("Inactive")) return false;

    		// don't do anything if is sliding
    		if ( $(this).closest('.ContentSliderElement').hasClass("sliding") ) return false;
    		$(this).closest(".ContentSliderElement").addClass("sliding");
    		
    		// set slide direction and goSlide
    		slideDirection = 1;
    		DwApetito.goSlide(slideDirection, this);
    		    		
    		return false;
    	});
    	
    	// Right Arrow Click Event
    	$('.ContentSliderElement .RightArrow a').click(function(){
    		
    		$(this).blur();
    		
    		// don't do anything if is inactive
    		if ($(this).closest('.RightArrow').hasClass("Inactive")) return false;

    		// don't do anything if is sliding
    		if ( $(this).closest('.ContentSliderElement').hasClass("sliding") ) return false;
    		$(this).closest(".ContentSliderElement").addClass("sliding");
    		
    		// set slide direction and goSlide
    		slideDirection = -1;
    		DwApetito.goSlide(slideDirection, this);
    		    		
    		return false;
    	});
    	
    	// pagelist navigation click event
    	$('.PageList li a').click(function(){
    		
    		$(this).blur();
    		
    		var oThisContentSliderEl = $(this).closest(".ContentSliderElement");
    		var iThisIndex = parseInt($(this).attr("rel"));
    			
    		DwApetito.slideTo( oThisContentSliderEl, iThisIndex );
    		
    		return false;
    	});
    },
    
    goSlide : function(slideDirectionHandover, oLinkEl)
    {
    
    	// vars
		var oThisContentSliderEl = $(oLinkEl).closest(".ContentSliderElement");
		var oActiveElement = oThisContentSliderEl.find(".MainSliderContentWrapper .Active");
		var iIndexOfActiveElement = oActiveElement.index();
		var iNewIndex = iIndexOfActiveElement + slideDirectionHandover * -1;
		
		// slide 
		var iThisItemWidth = oActiveElement.width();
		// check if extra margin between elements (toggled by class in Main Slider Element)
		var bHasMargin = oThisContentSliderEl.hasClass("HasMarginBetweenElements");
		if (bHasMargin) iThisItemWidth = iThisItemWidth + this.getMarginFromMainClass(oThisContentSliderEl);
		var iActualMrgLft = parseInt(oThisContentSliderEl.find('.SliderContentItemsWrapper').css("marginLeft"));

		oThisContentSliderEl.find('.SliderContentItemsWrapper').animate({ 'marginLeft' :  iActualMrgLft + iThisItemWidth * slideDirectionHandover }
			
			,function(){ oThisContentSliderEl.removeClass("sliding");
			}
		);
		
		// set new active element
		oActiveElement.removeClass("Active");
		oThisContentSliderEl.find(".SliderContentItemsWrapper .ContentItem:eq(" + iNewIndex + ")").addClass("Active");
		
		DwApetito.checkArrowsAndNumbers(oThisContentSliderEl, iNewIndex);
    },
    
    slideTo : function ( oThisContentSliderEl, iNewIndex )
    {
    	
    	// vars
		var oActiveElement = oThisContentSliderEl.find(".MainSliderContentWrapper .Active");
		var iIndexOfActiveElement = oActiveElement.index();
		
		//slide
		var iNewMrgLeft = 0;
		for ( i = 0; i < iNewIndex; i++) { // get width of all elements before selected - they together give the new margin
			
			iNewMrgLeft -= parseInt( oThisContentSliderEl.find('.ContentItem:eq(' +  i + ')').width());
			iNewMrgLeft -= (oThisContentSliderEl.hasClass("HasMarginBetweenElements")) ? DwApetito.getMarginFromMainClass(oThisContentSliderEl) : 0;
		}
		
		oThisContentSliderEl.find('.SliderContentItemsWrapper').animate({ 'marginLeft' :  iNewMrgLeft }
    	
			,function(){ oThisContentSliderEl.removeClass("sliding");
			}
		);

		// set new active element
		oActiveElement.removeClass("Active");
		oThisContentSliderEl.find(".SliderContentItemsWrapper .ContentItem:eq(" + iNewIndex + ")").addClass("Active");
		
    	DwApetito.checkArrowsAndNumbers(oThisContentSliderEl, iNewIndex);    
    },
    
    checkArrowsAndNumbers : function(oThisContentSliderEl, iNewIndex)
    {
    
		// check if any arrows need to be enabled / disabled
		var iLengthOfList = oThisContentSliderEl.find(".ContentItem").length;
		
		// right arrow
		if ( iNewIndex + 1 == iLengthOfList ) oThisContentSliderEl.find(".RightArrow").addClass("Inactive");
		if ( iNewIndex + 1 < iLengthOfList && iLengthOfList > 1) oThisContentSliderEl.find(".RightArrow").removeClass("Inactive");
		//left arrow
		if ( iNewIndex == 0 ) oThisContentSliderEl.find(".LeftArrow").addClass("Inactive");
		if ( iNewIndex > 0 ) oThisContentSliderEl.find(".LeftArrow").removeClass("Inactive");
		
		// set new active number
		var oThisPageList = oThisContentSliderEl.find('.PageList').each(function(){
            /*$(this).find(".Active").removeClass("Active").css({"fontWeight" : "normal"});
            var iPageListLength = $(this).find('li').length;
            var iNewNumberIndex = iPageListLength - 1 - iNewIndex;
            $(this).find('li:eq(' + iNewNumberIndex + ')').addClass("Active").css({"fontWeight" : "bold"});
            */
		    $(this).find(".Active").removeClass("Active");
            $(this).find("li a").css({"color" : "#251C6D ", 'font-weight' : 'normal'});
            var iPageListLength = $(this).find('li').length;
            var iNewNumberIndex = iPageListLength - 1 - iNewIndex;
            $(this).find('li:eq(' + iNewNumberIndex + ') a').css({"color" : "#DE012F ", 'font-weight' : 'normal'});
		}); 

		// change inContent Numbering
		var oInContNumberingEl = oThisContentSliderEl.find('.SliderContentItemsWrapper .Active .InContentNumbering');
		oInContNumberingEl.text( ( iNewIndex + 1 ) +  "/" + iLengthOfList + " ");
		
		// change pagination for Sliders With Many Element
		DwApetito.actualizePagination(oThisContentSliderEl);
    },
    
    getMarginFromMainClass : function(oMainElement)
    {
    
    	// vars
    	var sThisClass = oMainElement.attr("class");
    	var iStartOfValue = sThisClass.indexOf("MarginBtwElems__");
    	var iEndOfValue = sThisClass.indexOf("Px__", iStartOfValue + 16);
    	var sStringWithMarginOnly = sThisClass.slice(iStartOfValue + 16, iEndOfValue);
    	
    	return parseInt(sStringWithMarginOnly);
    },
    
    actualizePagination : function(oMainElement)
    {
    
    	if (!oMainElement.hasClass("HasPagesWithManyElements")) return false;
    	
    	// vars
    	var sThisClass = oMainElement.attr("class");
    	var iStartOfValue = sThisClass.indexOf("ElementsPerPage__");
    	var iEndOfValue = sThisClass.indexOf("__", iStartOfValue + 17);
    	var sElmCntOnly = sThisClass.slice(iStartOfValue + 17, iEndOfValue);
    	var iElmCntOnly = parseInt(sElmCntOnly);
    	
    	// check which element in list is active and get numbers
    	var iIndexOfActEl = oMainElement.find('.SliderContentItemsWrapper .Active').index();
    	var iStartValue = 1 + iIndexOfActEl * iElmCntOnly;
    	var iEndValue = iStartValue + iElmCntOnly - 1;
    	
    	// get number of items
    	var iTotalNoOfEls = oMainElement.find('.Item').length;
		
		if (iTotalNoOfEls < iElmCntOnly || iEndValue > iTotalNoOfEls)
        	iEndValue = iTotalNoOfEls;

		
    	// set numbers
    	oMainElement.find('.Superscript .ActStart').text(iStartValue);
    	oMainElement.find('.Superscript .ActEnd').text(iEndValue);
    	oMainElement.find('.Superscript .TotalNoOfElems').text(iTotalNoOfEls);
    	
		        // set the whole height for container with count of .Item elements dynamically
        if ($('.MainSliderContentWrapper').hasClass('VariableHeight'))
        {
            var iItemHeight = $('.MainSliderContentWrapper').find('.Item:first-child').outerHeight(true);
            var iItemPageCount = iEndValue - iStartValue + 1;
            $('.MainSliderContentWrapper').css("height", (iItemHeight * iItemPageCount) + "px");
        }
		
    	return true;
    },
    
    ///
    /// function to initiate lightbox (facebox)
    ///
    /*faceboxForFormLayerInit : function()
    {
    	
    	$('a[rel=facebox]').facebox({ closeImage : 'script/facebox/closelabel.png' });
    },*/

    ///
    /// position links in big links box (link tiles)
    ///
    positionLinksInSquares : function()
    {
    	
    	$('.BigLinkBoxWrapper>.LinkSquares>li.PicLink>a').each(function(){
    		
    		var _oThisSpanEl = $(this).find("span");
    		var iThisStrongElWidth = $(this).find("span>strong").width();
    		var iThisStrongElHeight = _oThisSpanEl.height();
    		var iThisPadding = parseInt( _oThisSpanEl.css("paddingLeft")) + parseInt(_oThisSpanEl.css("paddingRight"));
    		var iThisWidthOfLi = 184;
    		
    		var iThisNewTopValue = 94 - ( iThisStrongElHeight / 2);
    		var iCorrection = ($.browser.msie) ? 1 : 0; 
    		var iThisNewLeftValue = 91 + iCorrection - ( ( iThisStrongElWidth + iThisPadding ) / 2 );
    		
    		//alert ("iThisStrongElWidth: " + iThisStrongElWidth + "\niThisSTrongElHeight: " + iThisStrongElHeight + "\niThisPadding: " + iThisPadding + "\niThisWidthOfLi: " + iThisWidthOfLi + "\niThisNewTopValue: " + iThisNewTopValue + "\niThisNewLeftValue: " + iThisNewLeftValue );
    		
    		_oThisSpanEl.css("width", iThisStrongElWidth+5);
    		_oThisSpanEl.css("left", iThisNewLeftValue + "px");
    		_oThisSpanEl.css("top", iThisNewTopValue + "px");
    	});
    },
    
/*    lightBoxInit : function()  old lightbox used for module m12 only - replaced by facebox
    {
        $('.ModuleM12Wrapper td a').lightBox({
            imageLoading: 'image/event/lightbox-ico-loading.gif',
            imageBtnClose: 'image/event/lightbox-btn-close.gif',
            imageBtnPrev: 'image/event/lightbox-btn-prev.gif',
            imageBtnNext: 'image/event/lightbox-btn-next.gif'
        });
    },*/
    
    toggleContentInit : function()
    {
        $('.ModuleA02Wrapper .Trigger').toggle(function(){
        	 
        		var _oAllTrigger = $('.ModuleA02Wrapper .Trigger');
        		
        		_oAllTrigger.each(function(){
        			
        			var _oSelf = $(this);
        			
        			if(_oSelf.hasClass('Active')){
        				
        				_oSelf.next('div.ToggableContent').slideUp(300, function(){
        		        	
        	        		
        				_oSelf.removeClass('Active');
        				
        				});
        			}
        			
        		});
        	
        		$(this).next('div.ToggableContent').slideDown(300,function()
	            {
        			
	            });
        		$(this).addClass('Active');
        	},
        	
        	function(){
        	
        		$(this).next('div.ToggableContent').slideUp(300, function(){
        	
        		
        		$(this).prev().removeClass('Active'); 
  
        	}
        
        	);
	            
	            
	            return false;
	            
	    });
       
        
    },
    
    rattleBox : function()
    {
    	$('.ButtonRed, .ButtonGreen').click(function(){
    		
    		var _oSelf = $(this);
    		var _oAllButton = $('.ButtonRed, .ButtonGreen');
    		var _oKlapper = _oSelf.parent('div').next('div');
    		
    		if( !_oSelf.hasClass('open') ){
    			_oKlapper.slideDown('slow', function() {
    				
    				_oSelf.addClass('Active');
        		    
        		 });
    		}
    		
    		_oSelf.addClass('open');
    		
    		_oAllButton.each(function(){	
    			var _oSelf = $(this);
    			var _oKlapper = _oSelf.parent('div').next('div');
				if(_oSelf.hasClass('Active') ){
    				_oKlapper.slideUp('slow');
    				_oSelf.removeClass('Active');
    				_oSelf.removeClass('open');
    			}
    		});
    		
    		
			
    		return false;
    	});
    },
    
    toggleSearchFormInit : function()
    {
        var _oSearchForm = $('.MetaInfosTop input.SearchText');
        this.sSearchFormStandardText = _oSearchForm.val();
        
        _oSearchForm.focus(function()
        {
            if ($(this).val() == DwApetito.sSearchFormStandardText) {
                $(this).val('');
            }
        }).blur(function()
        {
            if ($(this).val() == '') {
                $(this).val(DwApetito.sSearchFormStandardText);
            }
        });
    },
    
    
    toggleLinkSquares : function()
    {
    	var _oLinkSquare = $('.LinkSquares li');
    	_oLinkSquare.each(function(){
    		if($(this).find('.Text').length == 1){
	    		$(this).mouseover(function(){
	    			$(this).attr('class','TextSquare');
	    			$(this).find('.Pic').hide();
	    			$(this).find('.Text').show();
	    		});
	    		$(this).mouseout(function(){
	    			$(this).attr('class','PicLink');
	    			$(this).find('.Text').hide();
	    			$(this).find('.Pic').show();
	    		});
    		}
    	});
    },
    
    
    toggleMainNav : function()
    {
    	var _oMainNav = $('.Header .MainNav');
    	var _oMainNavTab = _oMainNav.find('li.MainNavOpener');
    	var _oMainNavSub = _oMainNav.find('li.MainNavOpener .MainNavSub');
    	
    	
    	_oMainNavTab.hover(
    	function(){
    		var _oCurrentTab = $(this);
    		var _oCurrentNavSub = $(this).find('.MainNavSub');
    		
    		timerShow = setTimeout(function(){
    			_oCurrentTab.addClass('hover');
    			_oCurrentNavSub.show();
    		
    		},200);
    		
    		
    	},
    	function(e){
   			clearTimeout(timerShow);
    		setTimeout(function(){
    			_oMainNavTab.removeClass('hover');
    			_oMainNavSub.hide();
    		
    		},200);

    	});
    	
    }
};
