( function( api ) {

    api.sectionConstructor['upsell'] = api.Section.extend( {

        // No events for this type of section.
        attachEvents: function () {},

        // Always make the section active.
        isContextuallyActive: function () {
            return true;
        }
    } );

} )( wp.customize );
jQuery(document).ready(function($) {

    /**
     * Script for Customizer icons
     */ 
    $(document).on('click', '.cv-customize-icons li', function() {
        $(this).parents('.cv-customize-icons').find('li').removeClass();
        $(this).addClass('selected');
        var iconVal = $(this).parents('.icons-list-wrapper').find('li.selected').children('i').attr('class');
        var inpiconVal = iconVal.split(' ');
        $(this).parents( '.cv-customize-icons' ).find('.cv-icon-value').val(inpiconVal[1]);
        $(this).parents( '.cv-customize-icons' ).find('.selected-icon-preview').html('<i class="' + iconVal + '"></i>');
        $('.cv-icon-value').trigger('change');
    });

    /**
     * Radio Image control in customizer
     */
    // Use buttonset() for radio images.
    $( '.customize-control-radio-image .buttonset' ).buttonset();

    // Handles setting the new value in the customizer.
    $( '.customize-control-radio-image input:radio' ).change(
        function() {

            // Get the name of the setting.
            var setting = $( this ).attr( 'data-customize-setting-link' );

            // Get the value of the currently-checked radio input.
            var image = $( this ).val();

            // Set the new value.
            wp.customize( setting, function( obj ) {

                obj.set( image );
            } );
        }
    );

    /**
     * Multiple checkboxes
     */

    $( '.customize-control-multiple-checkboxes input[type="checkbox"]' ).on( 'change', function() {
        checkbox_values = $( this ).parents( '.customize-control' ).find( 'input[type="checkbox"]:checked' ).map(
            function() {
                return this.value;
            }
        ).get().join( ',' );

        $( this ).parents( '.customize-control' ).find( 'input[type="hidden"]' ).val( checkbox_values ).trigger( 'change' );
    });

    /**
      * Function for repeater field
     */
    function enrollment_refresh_repeater_values(){
        $(".cv-repeater-field-control-wrap").each(function(){
            
            var values = []; 
            var $this = $(this);
            
            $this.find(".cv-repeater-field-control").each(function(){
            var valueToPush = {};   

            $(this).find('[data-name]').each(function(){
                var dataName = $(this).attr('data-name');
                var dataValue = $(this).val();
                valueToPush[dataName] = dataValue;
            });

            values.push(valueToPush);
            });

            $this.next('.cv-repeater-collector').val(JSON.stringify(values)).trigger('change');
        });
    }

    $('#customize-theme-controls').on('click','.cv-repeater-field-title',function(){
        $(this).next().slideToggle();
        $(this).closest('.cv-repeater-field-control').toggleClass('expanded');
    });

    $('#customize-theme-controls').on('click', '.cv-repeater-field-close', function(){
        $(this).closest('.cv-repeater-fields').slideUp();;
        $(this).closest('.cv-repeater-field-control').toggleClass('expanded');
    });

    $("body").on("click",'.cv-repeater-add-control-field', function(){

        var fLimit = $(this).parent().find('.field-limit').val();
        var fCount = $(this).parent().find('.field-count').val();
        if( fCount < fLimit ) {
            fCount++;
            $(this).parent().find('.field-count').val(fCount);
        } else {
            $(this).before('<span class="cv-limit-msg">Only '+fLimit+' repeater field will be permitted.</span>');
            return;
        }

        var $this = $(this).parent();
        if(typeof $this != 'undefined') {

            var field = $this.find(".cv-repeater-field-control:first").clone();
            if(typeof field != 'undefined'){
                
                field.find("input[type='text'][data-name]").each(function(){
                    var defaultValue = $(this).attr('data-default');
                    $(this).val(defaultValue);
                });

                field.find("input[type='url'][data-name]").each(function(){
                    var defaultValue = $(this).attr('data-default');
                    $(this).val(defaultValue);
                });
                
                field.find(".cv-repeater-icon-list").each(function(){
                    var defaultValue = $(this).next('input[data-name]').attr('data-default');
                    $(this).next('input[data-name]').val(defaultValue);
                    $(this).prev('.cv-repeater-selected-icon').children('i').attr('class','').addClass(defaultValue);
                    
                    $(this).find('li').each(function(){
                        var icon_class = $(this).find('i').attr('class');
                        if(defaultValue == icon_class ){
                            $(this).addClass('icon-active');
                        }else{
                            $(this).removeClass('icon-active');
                        }
                    });
                });

                field.find('.cv-repeater-fields').show();

                $this.find('.cv-repeater-field-control-wrap').append(field);

                field.addClass('expanded').find('.cv-repeater-fields').show(); 
                $('.accordion-section-content').animate({ scrollTop: $this.height() }, 1000);
                enrollment_refresh_repeater_values();
            }

        }
        return false;
     });
    
    $("#customize-theme-controls").on("click", ".cv-repeater-field-remove",function(){
        if( typeof  $(this).parent() != 'undefined'){
            $(this).closest('.cv-repeater-field-control').slideUp('normal', function(){
                $(this).remove();
                enrollment_refresh_repeater_values();
            });
        }
        return false;
    });

    $("#customize-theme-controls").on('keyup change', '[data-name]',function(){
        enrollment_refresh_repeater_values();
        return false;
    });

    /*Drag and drop to change order*/
    $(".cv-repeater-field-control-wrap").sortable({
        orientation: "vertical",
        update: function( event, ui ) {
            enrollment_refresh_repeater_values();
        }
    });

    $('body').on('click', '.cv-repeater-icon-list li', function(){
        var icon_class = $(this).find('i').attr('class');
        $(this).addClass('icon-active').siblings().removeClass('icon-active');
        $(this).parent('.cv-repeater-icon-list').prev('.cv-repeater-selected-icon').children('i').attr('class','').addClass(icon_class);
        $(this).parent('.cv-repeater-icon-list').next('input').val(icon_class).trigger('change');
        enrollment_refresh_repeater_values();
    });

    $('body').on('click', '.cv-repeater-selected-icon', function(){
        $(this).next().slideToggle();
    });

    /**
     * Typography ajax scripts
     */
    $(document).on('change', '.typography_face', function() {
        var font_family = $(this).val();
        var dis = $(this);
        $.ajax({
            url: ajaxurl,
            data: ({
                'action': 'get_google_font_variants',
                'font_family': font_family,
            }),
            success: function(response) {
                dis.parent('.typography-font-family').next('.typography-font-style').children('select').html(response);
                var mtFontWeight = dis.parent('.typography-font-family').next('.typography-font-style').children('select').attr( 'data-customize-setting-link' );
                var setVal = dis.parent('.typography-font-family').next('.typography-font-style').children('select').val();
                wp.customize( mtFontWeight, function( obj ) {
                    obj.set( setVal );
                } );
            }
        });
    });   

    $('.typography-color .color-picker-hex').wpColorPicker({

        change: function(event, ui){

            var setting = $( this ).attr( 'data-customize-setting-link' );
            var hexcolor = $( this ).wpColorPicker( 'color' );
            // Set the new value.
            wp.customize( setting, function( obj ) {
                obj.set(hexcolor);
            });
        }
    });

    $( ".slider-range-size" ).slider({
        range: "min",
        value: 18,
        min: 12,
        max: 100,
        step: 1,
        slide: function( event, ui ) {
            $(this).parent().find( ".slider-value-size" ).text( ui.value );

            var setting = $(this).parent().find("input").attr( 'data-customize-setting-link' );

            // Set the new value.
            wp.customize( setting, function( obj ) {

                obj.set( ui.value );
            });

        }
    });

    $( ".slider-range-size" ).each(function(){
        $( this ).slider('value', $( this ).attr('value'));
    });

    $('.slider-value-size').each(function(){
        var value = $( this ).next().attr('value');
        $(this).text(value);
    });

    $( ".slider-range-line-height" ).slider({
        range: "min",
        value: 1.5,
        min: 0.8,
        max: 5,
        step: 0.1,
        slide: function( event, ui ) {
            $(this).parent().find( ".slider-value-line-height" ).text( ui.value );

            var setting = $(this).parent().find("input").attr( 'data-customize-setting-link' );

            // Set the new value.
            wp.customize( setting, function( obj ) {
                obj.set( ui.value );
            });

        }
    });

    $( ".slider-range-line-height" ).each(function(){
        $( this ).slider('value', $( this ).attr('value'));
    });

    $('.slider-value-line-height').each(function(){
        var value = $( this ).next().attr('value');
        $(this).text(value);
    });
    
});

/**
 * Toggle field.
 */
wp.customize.controlConstructor['cv-toggle'] = wp.customize.Control.extend({
    ready: function(){
        'use strict';

        var control = this,
            checkboxValue = control.setting._value;

            //console.log(control);

        // Save the value
        this.container.on( 'change', 'input', function() {
            checkboxValue = ( jQuery( this ).is( ':checked' ) ) ? true : false;
            control.setting.set( checkboxValue );
        });
    }
});