/*
 * (c) 2016 .hausformat
 */

;(function () {

    'use strict';

    /* HF-Todo
     * - bessere Lösung für siblings bei toggle Parent
     * - Responsive Akkorion nicht schliessen
     * */

    // import jquery
    var $ = jQuery;

    $(document).ready(function () {
        $('.js-toggle-class').click(function () {

            var $this = $(this);
            var selector = $this.data('toggle-selector');
            var className = $this.data('toggle-class');
            var toggleParent = $this.data('toggle-parent');
            var closeAllOther = $this.data('toggle-close-other');

            if (!selector) {
                selector = 'html';
            }

            if (toggleParent) {
                if (closeAllOther) {
                    $(this).closest(selector).siblings(selector).not($this.closest(selector)).removeClass(className);
                    $(this).closest(selector).siblings(selector).not($this.closest(selector)).trigger('hf-toggle', {
                        active: $(this).closest(selector).hasClass(className),
                        className: className
                    });

                    $this.closest(selector).toggleClass(className);

                    $this.closest(selector).trigger('hf-toggle', {
                        active: $(this).closest(selector).hasClass(className),
                        className: className
                    });
                } else {
                    $this.closest(selector).toggleClass(className);

                    $this.closest(selector).trigger('hf-toggle', {
                        active: $(this).closest(selector).hasClass(className),
                        className: className
                    });
                }
            } else {
                $(selector).toggleClass(className);

                $(selector).trigger('hf-toggle', {
                    active: $(selector).hasClass(className),
                    className: className
                });
            }
        });

    });

    /* Anpassung Tabber - letzter geöffneter Zustand herstellen */
    var cookieName = 'websiteTabs';
    $(".hf-tab-content.hf-tab").removeClass("is-open");
    if ($.cookie(cookieName)) {
        var tabId = $.cookie(cookieName);
        $('#c' + tabId).addClass("is-open");

        scrollToTabber('c'+tabId);
    }

    $('.hf-tabber').each(function () {
        var aktTabberID = $(this).attr("id");
        $('#'+aktTabberID+' > .hf-tabs .hf-tab > .hf-tab-header').each(function () {
            var $tab = $(this);
            var tabId = $tab.attr('data-tab-id');
            $('#c' + tabId + ".hf-tab").on('hf-toggle', function (_, status) {
                if ($(this).hasClass(status.className)) {
                    $.cookie(cookieName, ""+tabId);
                    scrollToTabber(aktTabberID);
                }
                else {
                    $.removeCookie(cookieName);
                }
            });
        });
    });

    function scrollToTabber(tabberID) {
        if ($("#"+tabberID+" .hf-tab.is-open")[0]){
            var $container = $("html,body");
            var $scrollTo = $("#"+tabberID+" .hf-tab.is-open");
            var siteheaderHeight = 0;
            if ($(".hf-breadcrumb").length) {
                var breadcrumb = $(".hf-breadcrumb").height();
                siteheaderHeight = $("#siteheader").height() + breadcrumb;
            } else {
                siteheaderHeight = $("#siteheader").height();
            }
            var scrollValue = $scrollTo.offset().top - siteheaderHeight - 30;
            $container.animate({scrollTop: scrollValue, scrollLeft: 0},500);
        }
    }

})();
