
var tng = {
    highlightSearchTerm: function(searchTerms) {
        // highlight words on page that were searched if cookie is present
        if(typeof searchTerms !== 'undefined') {
            $.each(searchTerms, function(index, element) {
                $('body').highlight(element);
            });
        }
    },

    modifyLinks: function(term) {
        $('.result-list-item b a').each(function(index, element) {
            var href = $(element).attr('href');
            pagePattern = new RegExp(/\.html$/);
            if (pagePattern.test(href)) {
                $(element).attr('href', href + '?term=' + encodeURI(term));
            }
        });
    },

    hightlightSearchResultList: function() {
        var searchTermArray = window.location.search.match(/tx_kesearch_pi1%5Bsword%5D=([^& ]+)(?:&|$)/);

        if(searchTermArray !== null) {
            if(searchTermArray.length > 1) {
                var searchTerm = searchTermArray[1];
                var searchTerms = decodeURIComponent(searchTerm).split('+');
                tng.highlightSearchTerm(searchTerms);
                tng.modifyLinks(searchTerm);
            }
        }
    },

    highlightSearchDetail: function() {
        var searchTermArray = window.location.search.match(/term=([^& ]+)(?:&|$)/);
        if(searchTermArray !== null) {
            if(searchTermArray.length > 1) {
                var searchTerm = searchTermArray[1];
                var searchTerms = decodeURIComponent(searchTerm).split('+');
                tng.highlightSearchTerm(searchTerms);
            }
        }
    }
};
(function ($) {
    $(function () {
        $(document).ready(function () {

            $('#page-1 .counter').counterUp({
                delay: 10,
                time: 800
            });

            $('#menu ul li').mouseenter(function(){
                $(this)
                    .prev()
                    .find('a')
                    .addClass('removeBorder');
                });
            $('#menu ul li').mouseleave(function(){
                $(this)
                    .prev()
                    .find('a')
                    .removeClass('removeBorder');
            });

            // get search term if search was submitted
            tng.hightlightSearchResultList();

            // highlight search term on result page
            tng.highlightSearchDetail();
        });
    });

    $(function() {

        $('a[rel*="lightbox"]').magnificPopup({
            type: 'image',
            tLoading: 'Lade Bild...',
            tClose: 'Schließen (Esc)',
            image: {
                titleSrc: function(item) {
                    var title = item.el.attr('title');
                    var description = item.el.attr('alt');
                    return ((title)?title:'') + ((description)?'<small>'+ description +'</small>':'');
                }
            },
            gallery: {
                enabled: true,
                navigateByImgClick: true,
                preload: [0,1],
                tCounter: '%curr% von %total%',
                tPrev: '<<',
                tNext: '>>'
            }
        });


        $('.tabs ul li a.ui-tabs-anchor').each(function () {
            var current = $(this).attr('href');
            var n = current.indexOf('#');
            $(this).attr('href', current.substr(n));
        });

        var accordions = $('.accordion');
        accordions.imagesLoaded(function () {
            accordions.accordion();
        });

        $('.tabs').tabs();

    });
    $('.mobileNavigationButton').click(function() {
        $('.mainNavigation').toggleClass('block', 10);
        return false;
    });
})(jQuery);

// on resize close submenu and remove class open
var width = $(window).width();
$(window).resize(function(){
    if ($(window).width() != width) {
        width = $(window).width();
        $('.subMenuHolder').css("display", "");
        $('.toggleSubMenu').removeClass("open");
    }
});

$(document).ready(function() {
    // match height js
    $('.match').matchHeight({
        byRow: true,
        property: 'height'
    });
    // menuToggle
    $('.hasChildren').find('.toggleSubMenu').click(function(){
        $(this).toggleClass('open').next().slideToggle();
    });

    $('.textAccordion').find('.click').click(function(){
        $(this).toggleClass('open').next().slideToggle();
    });
});