/* iTabs ~ Sean Catchpole - Version 2.2 - MIT/GPL */
$(document).ready(function(){
    // ALLE MARKIEREN
    $('#cat_checker').click(function() {
        if($("input[class='cat']").attr('checked')) {
            $("input[class='cat']").attr('checked', false);
        }else {
            $("input[class='cat']").attr('checked', true);
        }

        return false;
    });
    $('#cat_checker1').click(function() {
        if($("input[class='cat1']").attr('checked')) {
            $("input[class='cat1']").attr('checked', false);
        }else {
            $("input[class='cat1']").attr('checked', true);
        }

        return false;
    });	
    $('#cat_checker2').click(function() {
        if($("input[class='cat2']").attr('checked')) {
            $("input[class='cat2']").attr('checked', false);
        }else {
            $("input[class='cat2']").attr('checked', true);
        }

        return false;
    });

    // TABS
    var activeTab;
    var inactiveTab;
    var activeLink;
    var inactiveLink;
    if($('.yearList').length || $('#nothingFound').length) {
        activeTab = '#ergebnis';
        inactiveTab = '#suche';
        activeLink = '#resultLink';
        inactiveLink = '#searchLink'
    }else {
        activeTab = '#suche';
        inactiveTab= '#ergebnis';
        activeLink = '#searchLink';
        inactiveLink = '#resultLink'
    }

    $(activeTab).show();
    $(activeLink).addClass('active');
    $(inactiveTab).hide();
    $(inactiveLink).removeClass('active');


    $('#tabs a').click(function(){
        if(! $(this).hasClass('inactive')) {
            if(this.href.match(/#suche/)) {
                activeTab = '#suche';
                inactiveTab = '#ergebnis';
                activeLink = '#searchLink';
                inactiveLink = '#resultLink'
            }else {
                activeTab = '#ergebnis';
                inactiveTab = '#suche';
                activeLink = '#resultLink';
                inactiveLink = '#searchLink'
            }

            $(activeTab).show();
            $(activeLink).addClass('active');
            $(inactiveTab).hide();
            $(inactiveLink).removeClass('active');
        }
    });

    $("#tabs a").click(function(e){e.preventDefault();});

    // PAGINATION
    $('#itemsOnPageSwitcher a').click(function(e){
        e.preventDefault();

        var itemsOnPage = $(this).html();

        $('#itemsOnPageInput').val(itemsOnPage);
        $('#submitButton').click();
    });

    if($('.document').length > $('#resultsPerPage').val()) {
        $("#pagination a").click(function(e){e.preventDefault();});

        function pageHandler(pageNumber) {
            // show / hide documents
            $('.document').each(function(index, element){
                var document = $(element);
                if(document.hasClass('page_'+pageNumber)) {
                    document.show();
                } else {
                    document.hide();
                }
            });

            // show / hide geltungsbereich lists
            $('.geltungsbereichList').each(function(index, element){
                var geltungsbereich  = $(element);
                var hide = true;

                $('.document', geltungsbereich).each(function(index, element){
                    var document = $(element);

                    if(document.css('display') != 'none') {
                        hide = false;
                    }
                });

                if(hide) {
                    geltungsbereich.hide();
                }else {
                    geltungsbereich.show();
                }
            });

            // show / hide year lists
            $('.yearList').each(function(index, element){
                var year  = $(element);
                var hide = true;

                $('.geltungsbereichList', year).each(function(index, element){
                    var geltungsbereich = $(element);

                    if(geltungsbereich.css('display') != 'none') {
                        hide = false;
                    }
                });

                if(hide) {
                    year.hide();
                }else {
                    year.show();
                }
            });
        }


        $('#pagination').pagination({
            items: $('#resultsCount').val(),
            itemsOnPage: $('#resultsPerPage').val(),
            cssStyle: 'light-theme',   
            hrefTextPrefix: document.location.pathname+document.location.search+'#',
            prevText: 'zurück',
            nextText: 'vor',
            onInit: function() {
                pageHandler(1);
            },
            onPageClick: function(pageNumber, event) {
                pageHandler(pageNumber);
                $(document).scrollTop(0);
            }
        });
    }else {
        if($('#resultsCount').val() <= 20) {
            $('#itemsOnPageSwitcher').hide();
        }
    }
});
