(function ($) {
    "use strict";

    $(function () {

	$(document).foundation();

        $('.page-breadcrumb-wrapper .toggle-sidebar').click(function () {
            $('body').toggleClass('sidebar-collapsed');
            $('body').removeClass('sidebar-expanded');
        });

        $('.header__inner .toggle-sidebar').click(function () {
            $('body').toggleClass('sidebar-expanded');
            $('body').removeClass('sidebar-collapsed');
        });

        //smooth anchor scroll
        $('a[href*="#"]:not([href="#"])').not('.tab-title > a').click(function () {
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                if (target.length) {
                    $('html, body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                }
            }
        });


        //scroll pane for syntax blocks
        $('.highlight').addClass('horizontal-only scroll-pane');

        $('.scroll-pane').jScrollPane({autoReinitialise: true});
        $('.scroll-pane').each(
                function ()
                {
                    $(this).jScrollPane({autoReinitialise: true});

                    var api = $(this).data('jsp');
                    var throttleTimeout;
                    $(window).bind(
                            'resize',
                            function ()
                            {
                                if (!throttleTimeout) {
                                    throttleTimeout = setTimeout(
                                            function ()
                                            {
                                                api.reinitialise();
                                                throttleTimeout = null;

                                            }
                                    );
                                }
                            }
                    );

                }
        );

        //stylize external links
        var comp = new RegExp(location.host);

        $('a').each(function () {
            var _ahref = $(this).attr('href');
            if (_ahref) {
                if (!comp.test(_ahref) && _ahref.indexOf('http') == 0 && $(this).html().indexOf('<') == -1 && _ahref.indexOf('http://docs.continuum.io') == -1) {
                    $(this).addClass('url-external');
                    $(this).prop('target', '_blank');
                }
            }
        });

        //quick menu toggler
        $('.layout-two-col__second .pane-menu-docs-menu .pane-title').click(function () {
            $('.layout-two-col__second .pane-menu-docs-menu ul.menu').toggle();

            //hide main left nav when dropdown is open
            $('.layout-two-col__second .pane-node-field-doc-navigation-menu').toggle();
        });

        //get active menu title
        var active_menu_el = $('.layout-two-col__second .pane-menu-docs-menu ul.menu > li.active a');

        if (active_menu_el.length == 0) {
            active_menu_el = $('.layout-two-col__second .pane-menu-docs-menu ul.menu > li.active-trail a');
        }

        if (active_menu_el.length > 0) {
            $('.layout-two-col__second .pane-menu-docs-menu .pane-title').html(active_menu_el.html());
        }

        //was this helpful form - show submit on radio select and reset equalized cols
        $('.helpful-form input[name="submitted[was_this_helpful]"]').click(function () {

            $('.helpful-form .webform-submit').show();
            $('.layout-two-col__first').css('height', 'auto');

        });

        //toggle visibility
        $('.show-more').click(function () {
            $('.' + $(this).data('show-cls')).toggle();
            $(this).html($('.' + $(this).data('show-cls')).is(':visible') ? $(this).data('txt-hide') : $(this).data('txt-more'));
            $(document).foundation('equalizer', 'reflow');
        });

        //add pencil icon for anaconda cloud docs
        var current_root_doc = location.pathname.split('/')[1];
        var current_doc_page = location.pathname.split('/')[2];

        if (current_root_doc == 'anaconda-cloud') {
            //add pencil icon next to heading
            var h1_heading_el = $('article.node-doc-cache > h1');
            h1_heading_el.append('<a href="https://github.com/ContinuumIO/anaconda-cloud-docs/edit/master/docs/' + current_doc_page + '.rst" class="doc-edit-ico" title="Edit" target="_blank"><i class="fa fa-pencil" aria-hidden="true"></i></a>');
        }

    });

})(jQuery);
