/**
 * Scripts within the customizer controls window.
 *
 * Contextually shows the color hue control and informs the preview
 * when users open or close the front page sections section.
 */

(function( $, api ) {
    wp.customize.bind('ready', function() {
    	// Show message on change.
        var nuevo_blog_settings = ['nuevo_blog_slider_num', 'nuevo_blog_latest_num', 'nuevo_blog_services_num', 'nuevo_blogjects_num', 'nuevo_blog_testimonial_num', 'nuevo_blog_blog_section_num', 'nuevo_blog_reset_settings', 'nuevo_blog_testimonial_num', 'nuevo_blog_partner_num'];
        _.each( nuevo_blog_settings, function( nuevo_blog_setting ) {
            wp.customize( nuevo_blog_setting, function( setting ) {
                var blogRiderNotice = function( value ) {
                    var name = 'needs_refresh';
                    if ( value && nuevo_blog_setting == 'nuevo_blog_reset_settings' ) {
                        setting.notifications.add( 'needs_refresh', new wp.customize.Notification(
                            name,
                            {
                                type: 'warning',
                                message: localized_data.reset_msg,
                            }
                        ) );
                    } else if( value ){
                        setting.notifications.add( 'reset_name', new wp.customize.Notification(
                            name,
                            {
                                type: 'info',
                                message: localized_data.refresh_msg,
                            }
                        ) );
                    } else {
                        setting.notifications.remove( name );
                    }
                };

                setting.bind( blogRiderNotice );
            });
        });

        /* === Radio Image Control === */
        api.controlConstructor['radio-color'] = api.Control.extend( {
            ready: function() {
                var control = this;

                $( 'input:radio', control.container ).change(
                    function() {
                        control.setting.set( $( this ).val() );
                    }
                );
            }
        } );

        

        // Sortable sections
        jQuery( "body" ).on( 'hover', '.nuevo-blog-drag-handle', function() {
            jQuery( 'ul.nuevo-blog-sortable-list' ).sortable({
                handle: '.nuevo-blog-drag-handle',
                axis: 'y',
                update: function( e, ui ){
                    jQuery('input.nuevo-blog-sortable-input').trigger( 'change' );
                }
            });
        });

        /* On changing the value. */
        jQuery( "body" ).on( 'change', 'input.nuevo-blog-sortable-input', function() {
            /* Get the value, and convert to string. */
            this_checkboxes_values = jQuery( this ).parents( 'ul.nuevo-blog-sortable-list' ).find( 'input.nuevo-blog-sortable-input' ).map( function() {
                return this.value;
            }).get().join( ',' );

            /* Add the value to hidden input. */
            jQuery( this ).parents( 'ul.nuevo-blog-sortable-list' ).find( 'input.nuevo-blog-sortable-value' ).val( this_checkboxes_values ).trigger( 'change' );

        });

        // Deep linking for counter section to about section.
        jQuery('.nuevo-blog-edit').click(function(e) {
            e.preventDefault();
            var jump_to = jQuery(this).attr( 'data-jump' );
            wp.customize.section( jump_to ).focus()
        });

        wp.customize.bind('ready', function() {
            jQuery('a[data-open="nuevo-blog-recent-posts"]').click(function(e) {
                e.preventDefault();
                wp.customize.section( 'sidebar-widgets-homepage-sidebar' ).focus()
            });
        });
        
    });
        // upsell 
        api.sectionConstructor['nuevo-blog-upsell'] = api.Section.extend( {
            // No events for this type of section.
            attachEvents: function () {},
            // Always make the section active.
            isContextuallyActive: function () {
                return true;
            }
        } );
})( jQuery, wp.customize );
