/* global wp, jQuery */
/**
 * File customizer.js.
 *
 * Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Theme Customizer preview reload changes asynchronously.
 */

( function( $ ) {

    'use strict';

    wp.customize( 'booklium_color_scheme', function( booklium_colors_preset ) {
        booklium_colors_preset.bind( function( newColorScheme ) {
            var newColors = bookliumColorScheme[newColorScheme] || bookliumColorScheme['default'];
            if ( newColors ) {
                $.each(newColors, function (colorName, color) {
                    setSettingColor(colorName, color.value);
                });
            }
        } );
    } );

    function setSettingColor(controlName, newColor) {
        var control =  wp.customize.control(controlName);

        if ( !control ) {
            return; // if color exists in default colors map, but control fpr this color doesn't exist
        }

        var picker = $(control.container).find('.color-picker-hex');

        control.setting.set(newColor);
        picker.val( control.setting() ).wpColorPicker({defaultColor: newColor});
        picker.attr( 'data-default-color', newColor );
        picker.attr( 'placeholder', newColor );
    }
}( jQuery ) );
