/* global wp */
var customize_tabs_focus = function ( $ ) {
    'use strict';
    $( function () {
        var customize = wp.customize;
        $( '.customize-partial-edit-shortcut' ).on( 'DOMNodeInserted', function () {
            $( this ).on( 'click', function() {
                var controlId = $( this ).attr( 'class' );
                var tabToActivate = '';

                if ( controlId.indexOf( 'widget' ) !== -1 ) {
                    tabToActivate = $( '.customizer-tab>.widgets' );
                } else {
                    var controlFinalId = controlId.split( ' ' ).pop().split( '-' ).pop();
                    tabToActivate = $( '.customizer-tab>.' + controlFinalId );
                }

                customize.preview.send( 'tab-previewer-edit', tabToActivate );
            } );
        } );
    } );
};

customize_tabs_focus( jQuery );

/* global wp */
var customize_control_tabs = function ( $ ) {
    'use strict';

    $( function () {
        var customize = wp.customize;

        // Switch tab based on customizer partial edit links.
        customize.previewer.bind( 'tab-previewer-edit', function( data ) {
            $( data.selector ).trigger( 'click' );
        } );

        // Hide all controls
        $( '.tabs-control' ).each( function () {
            var customizerSection = $( this ).closest( '.accordion-section' );
            //Hide all controls in section.
            hideAllExceptCurrent( customizerSection );

            //Show controls under first radio button.
            var shownCtrls = $( this ).find( '.customizer-tab > input:checked' ).data( 'controls' );
            showControls( customizerSection, shownCtrls );
        } );

        $( '.customizer-tab > label' ).on( 'click', function () {
            var customizerSection = $( this ).closest( '.accordion-section' );
            var controls = $( this ).prev().data( 'controls' );

            //Hide all controls in section
            hideAllExceptCurrent( customizerSection );
            showControls( customizerSection, controls );
        } );
    } );
};

customize_control_tabs( jQuery );

/**
 * Handles showing the controls when the tab is clicked.
 * 
 * @param customizerSection
 * @param controlsToShowArray
 */
function showControls( customizerSection, controlsToShowArray ) {
    'use strict';
    jQuery.each( controlsToShowArray, function ( index, controlId ) {
        var parentSection = customizerSection[ 0 ];
        if ( controlId === 'widgets' ) {
            jQuery( parentSection ).children( 'li[class*="widget"]' ).css( 'display', 'list-item' );
            return true;
        }
        jQuery( '#customize-control-' + controlId ).css( 'display', 'list-item' );
    } );
}

/**
 * Utility function that hides all the controls in the panel except the tabs control.
 *
 * @param customizerSection
 * @param controlId
 */
function hideAllExceptCurrent( customizerSection ) {
    'use strict';
    jQuery( customizerSection ).children( 'li.customize-control' ).css( 'display', 'none' );
}