<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

//
// Class SSD_Grid_Control
//

if( class_exists( 'WP_Customize_Control' ) ) {
    class SSD_Grid_Control extends WP_Customize_Control {
        public $type = 'text';

        public function render_content() {
            ?>
            <div class="ssd_wrapper_grid">
            </div>
            <?php
        }
    }

    class SSD_BackgroundImage_Control extends WP_Customize_Control {
        public $type = 'text';

        public function render_content() {
            ?>
            <label class="customize-control-select ssd_backgroundimage_label">
                <span class="customize-control-title"><?php echo $this->label ?></span>
                <button type="button" class="button upload-button ssd_backgroundimage_button" data-ssd_area="<?php echo $this->id ?>"><?php echo __( 'Select image', 'swifty-site-designer' ) ?></button>
            </label>
            <?php
        }
    }

    class SSD_BackgroundPattern_Control extends WP_Customize_Control {
        public $type = 'text';

        public function render_content() {
            ?>
            <label class="customize-control-select ssd_backgroundpattern_label">
                <span class="customize-control-title"><?php echo $this->label ?></span>
                <button type="button" class="button upload-button ssd_backgroundpattern_button" data-ssd_area="<?php echo $this->id ?>"><?php echo __( 'Select pattern', 'swifty-site-designer' ) ?></button>
            </label>
            <?php
        }
    }

    class SSD_FGImage_Control extends WP_Customize_Control {
        public $type = 'text';

        public function render_content() {
            ?>
            <label class="customize-control-select ssd_fgimage_label">
                <span class="customize-control-title"><?php echo $this->label ?></span>
                <button type="button" class="button upload-button ssd_fgimage_button" data-ssd_area="<?php echo $this->id ?>"><?php echo __( 'Select image', 'swifty-site-designer' ) ?></button>
            </label>
            <?php
        }
    }
}

//
// Class SSD_Obj_Control
//

if( class_exists( 'WP_Customize_Control' ) ) {
    class SSD_Obj_Control extends WP_Customize_Control {
        public $type = 'textarea';
    }
}

//
// Class SwiftySiteDesignerCustomizer
//

class SwiftySiteDesignerCustomizer {
    protected $test = '-';

    public function __construct() {
        $this->init();
    }

    public function init() {
        global $ssd_wp_customize;

//        add_filter( 'load_textdomain_mofile', array( &$this, 'hook_load_textdomain_mofile' ), 10, 2 );
        require_once plugin_dir_path( __FILE__ ) . 'lang.php';

        $this->hook_customize_register( $ssd_wp_customize );
//        add_action( 'customize_register', array( &$this, 'hook_customize_register' ) );

        add_action( 'customize_preview_init', array( &$this, 'hook_customize_preview_init' ) );
        add_action( 'customize_controls_enqueue_scripts', array( &$this, 'hook_customize_controls_enqueue_scripts' ) );
        add_action( 'customize_sanitize_ssd_settings_obj', array( &$this, 'hook_customize_sanitize_ssd_settings_obj' ) );

        // This is replaced by a ajax call processing the css.
//        add_action( 'customize_sanitize_ssd_css', array( &$this, 'hook_customize_sanitize_ssd_css' ) );

//        add_action( 'customize_save_response', array( &$this, 'hook_customize_save_response' ) );

        add_action( 'wp_ajax_swifty_customizer_reset', array( $this, 'hook_ajax_customizer_reset' ) );
        add_action( 'wp_ajax_get_customizer_logo', array( $this, 'hook_ajax_get_customizer_logo' ) );
        add_action( 'wp_ajax_set_customizer_logo', array( $this, 'hook_ajax_set_customizer_logo' ) );
        add_action( 'wp_ajax_sanitize_ssd_css', array( $this, 'hook_ajax_sanitize_ssd_css' ) );
    }

    public function hook_ajax_customizer_reset() {
        global $ssd_wp_customize;

        // domk Add nonce check

//        if ( ! $this->wp_customize->is_preview() ) {
//            wp_send_json_error( 'not_preview' );
//        }
//        if ( ! check_ajax_referer( 'customizer-reset', 'nonce', false ) ) {
//            wp_send_json_error( 'invalid_nonce' );
//        }

        $settings = $ssd_wp_customize->settings();
        // remove theme_mod settings registered in customizer
        foreach ( $settings as $setting ) {
            if ( 'theme_mod' == $setting->type ) {
                remove_theme_mod( $setting->id );
            }
        }

        wp_send_json_success();
    }

    public function hook_ajax_get_customizer_logo() {
        $data = $_POST[ 'data' ];

        $custom_logo_id = get_theme_mod( 'custom_logo' );

        global $wpdb;
        $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $data ) );

        wp_send_json_success( array (
            'id' => $custom_logo_id,
            'attachment' => $attachment
        ) );
    }

    public function hook_ajax_set_customizer_logo() {
        $data = $_POST[ 'data' ];

        set_theme_mod( 'custom_logo', $data );

        wp_send_json_success();
    }

    public function hook_ajax_sanitize_ssd_css() {
        $this->test = $_POST[ 'data' ];

        $buffer = swifty_read_text( dirname( __FILE__ ) . '/../../style.css' );
        $buffer .= stripslashes( $_POST[ 'data' ] );

        // Remove comments
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

        // Remove space after colons
        $buffer = str_replace(': ', ':', $buffer);

        // Remove whitespace
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);

        // Collapse adjacent spaces into a single space
        $buffer = preg_replace('!\s+!', ' ', $buffer);

        // Remove spaces that might still be left where we know they aren't needed
        $buffer = str_replace(array('} '), '}', $buffer);
        $buffer = str_replace(array('{ '), '{', $buffer);
        $buffer = str_replace(array('; '), ';', $buffer);
        $buffer = str_replace(array(', '), ',', $buffer);
        $buffer = str_replace(array(' }'), '}', $buffer);
        $buffer = str_replace(array(' {'), '{', $buffer);
        $buffer = str_replace(array(' ;'), ';', $buffer);
        $buffer = str_replace(array(' ,'), ',', $buffer);

        wp_send_json_success( array (
            'css' => $buffer
        ) );
    }
    
//    public function hook_load_textdomain_mofile( $mofile, $domain ) {
//        if( $domain === 'swifty-site-designer' ) {
//            $mofile = str_replace( 'swifty-site-designer/ssd/lib/swifty_plugin/languages', 'swifty-site-designer/ssd/lib/swifty_plugin/languages/swifty-', $mofile );
//            $mofile = str_replace( 'swifty-site-designer/languages/', 'swifty-site-designer/languages/swifty-', $mofile );
//        }
//        return $mofile;
//    }

    public function hook_customize_register( $wp_customize ) {
        if( LibSwiftyPluginView::is_ss_mode() ) {
            $wp_customize->remove_section( 'themes' );
        }
        $wp_customize->remove_section( 'title_tagline' );
        $wp_customize->remove_section( 'colors' );
        $wp_customize->remove_section( 'background_image' );
        $wp_customize->remove_section( 'static_front_page' );
        $wp_customize->remove_section( 'nav' );

        // Remove the menu's panel. See http://stackoverflow.com/questions/32324815/menus-panel-in-wordpress-customizer
        if( isset( $wp_customize->nav_menus ) ) {
            remove_action( 'customize_controls_enqueue_scripts', array( $wp_customize->nav_menus, 'enqueue_scripts' ) );
            remove_action( 'customize_register', array( $wp_customize->nav_menus, 'customize_register' ), 11 );
            remove_filter( 'customize_dynamic_setting_args', array( $wp_customize->nav_menus, 'filter_dynamic_setting_args' ) );
            remove_filter( 'customize_dynamic_setting_class', array( $wp_customize->nav_menus, 'filter_dynamic_setting_class' ) );
            remove_action( 'customize_controls_print_footer_scripts', array( $wp_customize->nav_menus, 'print_templates' ) );
            remove_action( 'customize_controls_print_footer_scripts', array( $wp_customize->nav_menus, 'available_items_template' ) );
            remove_action( 'customize_preview_init', array( $wp_customize->nav_menus, 'customize_preview_init' ) );
        }

        // For translations:
        __( 'example', 'swifty-site-designer' );
        __( 'examples', 'swifty-site-designer' );
        __( 'layout', 'swifty-site-designer' );
        __( 'layouts', 'swifty-site-designer' );
        __( 'style', 'swifty-site-designer' );
        __( 'styles', 'swifty-site-designer' );
        __( 'color', 'swifty-site-designer' );
        __( 'colors', 'swifty-site-designer' );
        __( 'main menu', 'swifty-site-designer' );
        __( 'logo', 'swifty-site-designer' );
        __( 'title', 'swifty-site-designer' );
        __( 'slogan', 'swifty-site-designer' );
        __( 'headlines', 'swifty-site-designer' );
        __( 'normal text', 'swifty-site-designer' );

        foreach( array( /*'category',*/ 'layout', 'style', 'color'/*, 'imageset'*//*, 'pagetype'*/ ) as $typ ) {
            $name = $typ . 's';
            $typ_single = $typ;
            if( $typ === 'category' ) {
                $name = 'examples';
                $typ_single = 'example';
            }
            $wp_customize->add_section(
                'ssd_' . $typ . 's',
                array(
                    'title' => ucfirst( swifty__( $name, 'swifty-site-designer' ) ),
                    'description' => sprintf( __( 'Please select a %s of your choice:', 'swifty-site-designer' ), swifty__( $typ_single, 'swifty-site-designer' ) ),
                    'priority' => 1,
                )
            );

            // A section will not show up unless it has at least one control, so we'll make a fake control

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => 'ssd_section_' . $typ,
                'code2' => 'ssd_' . $typ,
                'section' => 'ssd_' . $typ . 's',
                'default' => 'none',
                'class' => 'SSD_Grid_Control',
            ) );

            if( $typ === 'color' ) {
                for ( $i = 1; $i <= 7; $i++ ) {
                    $this->ui_add_setting_and_control( $wp_customize, array(
                        'code' => 'ssd_details[palcolors][' . $i . ']',
                        'section' => 'ssd_' . $typ . 's',
                        'default' => '-',
                        'class' => 'WP_Customize_Color_Control',
                    ) );
                }
            }
        }

        $this->ui_add_details( $wp_customize );

        $wp_customize->add_section(
            'ssd_panel_advanced',
            array(
                'title' => __( 'Advanced', 'swifty-site-designer' ),
                'description' => '',
            )
        );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' =>  'ssd_details[customcss]',
            'section' => 'ssd_panel_advanced',
            'default' => '',
            'type' => 'textarea',
            'label' => __( 'Custom css / less', 'swifty-site-designer' )
        ) );

        $wp_customize->add_section(
            'ssd_debug',
            array(
                'title' => __( 'Debug', 'swifty-site-designer' ),
                'description' => '',
                'priority' => 1,
            )
        );

        $wp_customize->add_setting( 'ssd_settings_obj', array(
            'default' => '{}',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_setting( 'ssd_css', array(
            'default' => '',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );
    }

    public function ui_add_details( $wp_customize ) {
        $wp_customize->add_panel( 'ssd_panel_areas', array(
            'priority' => 10,
            'capability' => 'edit_theme_options',
            'theme_supports' => '',
            'title' => __( 'Areas', 'swifty-site-designer' ),
        ) );

        $wp_customize->add_panel( 'ssd_panel_details', array(
            'priority' => 10,
            'capability' => 'edit_theme_options',
            'theme_supports' => '',
            'title' => __( 'Details', 'swifty-site-designer' ),
        ) );

        $wp_customize->add_setting( 'ssd_details[category_id]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_setting( 'ssd_details[layout_id]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_setting( 'ssd_details[style_id]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_setting( 'ssd_details[color_id]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_setting( 'ssd_details[focusrect]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $this->ui_add_details_menu( $wp_customize );
        $this->ui_add_details_logo( $wp_customize );
        $this->ui_add_details_title( $wp_customize );
        $this->ui_add_details_slogan( $wp_customize );
        $this->ui_add_details_headlines( $wp_customize, 'headlines' );
        $this->ui_add_details_headlines( $wp_customize, 'bodytext' );
        $this->ui_add_details_siteicon( $wp_customize );
        foreach( array( 'page', 'main', 'topbar', 'header', 'navbar', 'sidebar', 'extrasidebar', 'footer', 'bottombar' ) as $area ) {
            $this->ui_add_details_section_area( $wp_customize, $area );
        }
    }

    public function ui_add_details_section_area( $wp_customize, $section_name ) {
        $title = $section_name;
        if( $title === 'extrasidebar' ) {
            $title = __( 'Extra Sidebar', 'swifty-site-designer' );
        }
        if( $title === 'main' ) {
            $title = __( 'Content', 'swifty-site-designer' );
        }
        $title = ucfirst( str_replace( '_', ' ', swifty__( $title, 'swifty-site-designer' ) ) );
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $wp_customize->add_section(
            'ssd_details_section_area_' . $section_name,
            array(
                'title' => $title,
                'description' => '',
                'priority' => 1,
                'panel' => 'ssd_panel_areas',
            )
        );

        if( $section_name !== 'page' && $section_name !== 'main' ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[on]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Show', 'swifty-site-designer' ) . ' ' . $section_name,
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'on' => __( 'Show', 'swifty-site-designer' ),
                    'off' => __( 'Hide', 'swifty-site-designer' ),
                )
            ) );

            if( strpos( $section_name, 'sidebar' ) !== false ) {
                $this->ui_add_setting_and_control( $wp_customize, array(
                    'code' => $code_section . '[position]',
                    'section' => $section,
                    'default' => 'default',
                    'type' => 'radio',
                    'label' => __( 'Position', 'swifty-site-designer' ),
                    'choices' => array(
                        'default' => __( 'Default', 'swifty-site-designer' ),
                        'left' => __( 'Left', 'swifty-site-designer' ),
                        'right' => __( 'Right', 'swifty-site-designer' ),
                    )
                ) );
            }
        }

        $wp_customize->add_setting( 'ssd_details[' . $section_name . '][def_bg_type]', array(
            'default' => 'default',
            'transport'   => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_type]',
            'section' => $section,
            'default' => 'none', // must not be 'default'!
            'type' => 'radio',
            'label' => __( 'Background type', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'color' => __( 'Color', 'swifty-site-designer' ),
                'image' => __( 'Image', 'swifty-site-designer' ),
                'pattern' => __( 'Pattern', 'swifty-site-designer' ),
            )
        ) );

        $wp_customize->add_setting( 'ssd_details[' . $section_name . '][def_bg_img]', array(
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_img]',
            'section' => $section,
            'default' => '',
            'label' => __( 'Background image', 'swifty-site-designer' ),
            'class' => 'SSD_BackgroundImage_Control'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_bg_im_ssry]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Center image vertically', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ' %:',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_im_ssry]',
            'section' => $section,
            'default' => '50',
            'type' => 'text'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_pattern]',
            'section' => $section,
            'default' => '',
            'label' => __( 'Background pattern', 'swifty-site-designer' ),
            'class' => 'SSD_BackgroundPattern_Control'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_color]',
            'section' => $section,
            'default' => '#c4c4c4',
            'label' => __( 'Background color', 'swifty-site-designer' ),
            'class' => 'WP_Customize_Color_Control'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_colr2]',
            'section' => $section,
            'default' => '#898989',
            'label' => __( 'Second background color', 'swifty-site-designer' ),
            'class' => 'WP_Customize_Color_Control'
        ) );

        if( in_array( $section_name, array( 'header', 'footer', 'topbar', 'navbar', 'bottombar' ) ) ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[set_height]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Height', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
                )
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_height]',
                'section' => $section,
                'default' => '0',
                'type' => 'text'
            ) );
        }

        if( $section_name === 'page' || strpos( $section_name, 'sidebar' ) !== false ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[set_width]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Width', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
                )
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_width]',
                'section' => $section,
                'default' => '0',
                'type' => 'text'
            ) );
        }

        if( $section_name !== 'page' ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[set_padding]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Padding', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
                )
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_padding_top]',
                'section' => $section,
                'default' => '0',
                'type' => 'text',
                'label' => __( 'Padding top', 'swifty-site-designer' ),
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_padding_bottom]',
                'section' => $section,
                'default' => '0',
                'type' => 'text',
                'label' => __( 'Padding bottom', 'swifty-site-designer' ),
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_padding_left]',
                'section' => $section,
                'default' => '0',
                'type' => 'text',
                'label' => __( 'Padding left', 'swifty-site-designer' ),
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[px_padding_right]',
                'section' => $section,
                'default' => '0',
                'type' => 'text',
                'label' => __( 'Padding right', 'swifty-site-designer' ),
            ) );
        }

        if( $section_name === 'page' ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[boxed]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Show page in', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'wide' => __( 'Full window', 'swifty-site-designer' ),
                    'boxed' => __( 'Centered box', 'swifty-site-designer' ),
                )
            ) );
        }
    }

    public function ui_add_details_menu( $wp_customize ) {
        $section_name = 'mainmenu';
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $this->ui_helper_add_details( $wp_customize, $section_name );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_menu_type]',
            'section' => $section,
            'default' => 'swifty',
            'type' => 'radio',
            'label' => __( 'Show menu', 'swifty-site-designer' ),
            'transport' => 'refresh',
            'choices' => array(
                'swifty' => __( 'Swifty', 'swifty-site-designer' ),
                'wp' => __( 'WordPress', 'swifty-site-designer' )
            )
        ) );

        $menu_choices = array();
        $wp_menus = wp_get_nav_menus();

        foreach ( $wp_menus as $menu ) {
            $menu_choices[ $menu->term_id ] = $menu->name;
        }

        reset( $menu_choices );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[wp_menu]',
            'section' => $section,
            'default' => key( $menu_choices ),
            'type' => 'select',
            'transport' => 'refresh',
            'choices' => $menu_choices,
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[leftright]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Align to side', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'left' => __( 'Left', 'swifty-site-designer' ),
                'right' => __( 'Right', 'swifty-site-designer' ),
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_bg_color]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Background color', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_color]',
            'section' => $section,
            'default' => '#c4c4c4',
            'class' => 'WP_Customize_Color_Control'
        ) );

        $this->ui_add_details_ui_font( $wp_customize, $code_section, $section );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_txt_color]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Text color', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[txt_color]',
            'section' => $section,
            'default' => '#e0e0e0',
            'class' => 'WP_Customize_Color_Control'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_bg_transparency]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Menu transparency', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ' %:',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[bg_transparency]',
            'section' => $section,
            'default' => '0',
            'type' => 'text'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[open_sub]',
            'section' => $section,
            'default' => 'click',
            'type' => 'radio',
            'label' => __( 'Open submenus on', 'swifty-site-designer' ),
            'choices' => array(
                'click' => __( 'Click', 'swifty-site-designer' ),
                'hover' => __( 'Mouse hover or click', 'swifty-site-designer' ),
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[show_arrows]',
            'section' => $section,
            'default' => 'on',
            'type' => 'radio',
            'label' => __( 'Show arrows for submenus', 'swifty-site-designer' ),
            'choices' => array(
                'on' => __( 'Show', 'swifty-site-designer' ),
                'off' => __( 'Hide', 'swifty-site-designer' ),
            )
        ) );
    }

    public function ui_add_details_logo( $wp_customize ) {
        $section_name = 'logo';
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        __( 'logo', 'swifty-site-designer' ); // ensure language extraction
        $this->ui_helper_add_details( $wp_customize, $section_name );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[fg_img]',
            'section' => $section,
            'default' => '',
            'label' => __( 'Logo image', 'swifty-site-designer' ),
            'class' => 'SSD_FGImage_Control',
            'transport' => 'refresh'
        ) );

        $wp_customize->add_setting( 'custom_logo', array(
            'theme_supports' => array( 'custom-logo' ),
            'transport'      => 'postMessage',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $custom_logo_args = get_theme_support( 'custom-logo' );
        $wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'custom_logo', array(
            'label'         => __( 'Logo image', 'swifty-site-designer' ),
            'section'       => $section,
            'height'        => $custom_logo_args[0]['height'],
            'width'         => $custom_logo_args[0]['width'],
            'flex_height'   => $custom_logo_args[0]['flex-height'],
            'flex_width'    => $custom_logo_args[0]['flex-width'],
        ) ) );


        $this->ui_helper_add_positioning( $wp_customize, $section_name );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_widthheight]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Maximum width & height', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[px_width]',
            'section' => $section,
            'default' => '0',
            'type' => 'text'
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[px_height]',
            'section' => $section,
            'default' => '0',
            'type' => 'text'
        ) );
    }

    public function ui_add_setting_and_control_helper( &$ar, $key, $default, $settings ) {
        if( array_key_exists( $key, $settings ) ) {
            $ar[ $key ] = $settings[ $key ];
        } else if( isset( $default ) ) {
            $ar[ $key ] = $default;
        }
    }

    public function ui_add_setting_and_control( $wp_customize, $settings ) {
        $ar = array();
        $this->ui_add_setting_and_control_helper( $ar, 'default', 'default', $settings );
        $this->ui_add_setting_and_control_helper( $ar, 'transport', 'postMessage', $settings );

        $wp_customize->add_setting( $settings[ 'code' ], array_merge( $ar, array( 'sanitize_callback' => 'swifty_sanitize_setting' ) ) );

        $ar = array(
            'settings' => $settings[ 'code' ],
            'section' => $settings[ 'section' ],
        );
        $this->ui_add_setting_and_control_helper( $ar, 'type', null, $settings );
        $this->ui_add_setting_and_control_helper( $ar, 'label', null, $settings );
        $this->ui_add_setting_and_control_helper( $ar, 'choices', null, $settings );

        if( array_key_exists( 'class', $settings ) ) {
            $code = $settings[ 'code' ];
            if( array_key_exists( 'code2', $settings ) ) {
                $code = $settings[ 'code2' ];
            }
            $wp_customize->add_control( new  $settings[ 'class' ]( $wp_customize, $code, $ar ) );
        } else {
            $wp_customize->add_control( $settings[ 'code' ], $ar );
        }
    }

    public function ui_add_details_title( $wp_customize ) {
        __( 'title', 'swifty-site-designer' ); // ensure language extraction
        $this->ui_helper_add_details( $wp_customize, 'title' );

        $wp_customize->add_setting( 'blogname', array(
            'default'    => get_option( 'blogname' ),
            'type'       => 'option',
            'capability' => 'manage_options',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_control( 'blogname', array(
            'label'      => __( 'Title text', 'swifty-site-designer' ),
            'section'    => 'ssd_details_section_area_title',
        ) );

        $this->ui_helper_add_txt_ui( $wp_customize, 'title' );
        $this->ui_helper_add_positioning( $wp_customize, 'title' );
    }

    public function ui_add_details_slogan( $wp_customize ) {
        __( 'slogan', 'swifty-site-designer' ); // ensure language extraction
        $this->ui_helper_add_details( $wp_customize, 'slogan' );

        $wp_customize->add_setting( 'blogdescription', array(
            'default'    => get_option( 'blogdescription' ),
            'type'       => 'option',
            'capability' => 'manage_options',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_control( 'blogdescription', array(
            'label'      => __( 'Slogan text', 'swifty-site-designer' ),
            'section'    => 'ssd_details_section_area_slogan',
        ) );

        $this->ui_helper_add_txt_ui( $wp_customize, 'slogan' );
        $this->ui_helper_add_positioning( $wp_customize, 'slogan' );
    }

    public function ui_add_details_siteicon( $wp_customize ) {
        $this->ui_helper_add_details( $wp_customize, 'siteicon' );

        $wp_customize->add_setting( 'site_icon', array(
            'type'       => 'option',
            'capability' => 'manage_options',
            'sanitize_callback' => 'swifty_sanitize_setting'
        ) );

        $wp_customize->add_control( new WP_Customize_Site_Icon_Control( $wp_customize, 'site_icon', array(
            'label'       => __( 'Site icon', 'swifty-site-designer' ),
            'description' => sprintf(
                __( 'The site icon (favicon) is used as a browser and app icon for your site.', 'swifty-site-designer' )
            ),
            'section'     => 'ssd_details_section_area_siteicon',
            'priority'    => 60,
        ) ) );
    }

    public function ui_add_details_ui_font( $wp_customize, $code_section, $section ) {
        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[txt_font]',
            'section' => $section,
            'default' => 'default',
            'type' => 'select',
            'label' => __( 'Font', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'Arial, Helvetica, sans-serif' => 'Universal: Helvetica',
                'Courier New, Courier, monospace' => 'Universal: Courier New',
                'Georgia, serif' => 'Universal: Georgia',
                'Lucida Sans Unicode, Lucida Grande, sans-serif' => 'Universal: Lucida Sans Unicode',
                'Times New Roman, Times, serif' => 'Universal: Times New Roman',
                'Trebuchet MS, Helvetica, sans-serif' => 'Universal: Trebuchet',
                'Verdana, Geneva, sans-serif' => 'Universal: Verdana',
                'google_Lato' => 'Google sans serif: Lato',
                'google_Montserrat' => 'Google sans serif: Montserrat',
                'google_Open Sans' => 'Google sans serif: Open Sans',
                'google_Roboto' => 'Google sans serif: Roboto',
                'google_Muli' => 'Google sans serif: Muli',
                'google_Ubuntu' => 'Google sans serif: Ubuntu',
                'google_Oswald' => 'Google sans serif: Oswald',
                'google_Nunito' => 'Google sans serif: Nunito',
                'google_Lora' => 'Google serif: Lora',
                'google_Playfair Display' => 'Google serif: Playfair Display',
                'google_Noto Serif' => 'Google serif: Noto Serif',
                'google_Josefin Slab' => 'Google serif: Josefin Slab',
                'google_Roboto Slab' => 'Google serif: Roboto Slab',
                'google_Arvo' => 'Google serif: Arvo',
                'google_Sacramento' => 'Google special: Sacramento',
                'google_Cabin Sketch' => 'Google special: Cabin Sketch',
                'google_Capriola' => 'Google special: Capriola',
                'google_Amatic SC' => 'Google special: Amatic SC',
                'google_Londrina Solid' => 'Google special: Londrina Solid',
                'google_Pacifico' => 'Google special: Pacifico',
                'google_Delius Swash Caps' => 'Google special: Delius Swash Caps',
                'google_Bubblegum Sans' => 'Google special: Bubblegum Sans',
                'google_Special Elite' => 'Google special: Special Elite',
            )
        ) );
    }

    public function ui_add_details_headlines( $wp_customize, $section_name ) {
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $this->ui_helper_add_details( $wp_customize, $section_name );

        $this->ui_add_details_ui_font( $wp_customize, $code_section, $section );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_txt_color]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Text color', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[txt_color]',
            'section' => $section,
            'default' => '#101010',
            'class' => 'WP_Customize_Color_Control'
        ) );

        if( $section_name !== 'bodytext' ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[txt_case]',
                'section' => $section,
                'default' => 'none',
                'type' => 'radio',
                'label' => __( 'Text uppercase', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'inherit' => __( 'Normal', 'swifty-site-designer' ),
                    'uppercase' => __( 'Uppercase', 'swifty-site-designer' ),
                )
            ) );
        }

    }

    public function ui_helper_add_txt_ui( $wp_customize, $key ) {
        $section_name = $key;
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $this->ui_add_details_ui_font( $wp_customize, $code_section, $section );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_fontsize]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Font size', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[fontsize]',
            'section' => $section,
            'default' => '0',
            'type' => 'text',
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_txt_color]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Text color', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[txt_color]',
            'section' => $section,
            'default' => '#c4c4c4',
            'class' => 'WP_Customize_Color_Control',
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_txt_shadow]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Shadow', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'on' => __( 'On', 'swifty-site-designer' ),
                'off' => __( 'Off', 'swifty-site-designer' ),
            )
        ) );
    }

    public function ui_helper_add_positioning( $wp_customize, $key ) {
        $section_name = $key;
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[leftright]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Align to side', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'left' => __( 'Left', 'swifty-site-designer' ),
                'right' => __( 'Right', 'swifty-site-designer' ),
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_horizontal]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Horizontal distance from side', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[px_horizontal]',
            'section' => $section,
            'default' => '0',
            'type' => 'text',
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[topbottom]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Align to side', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'top' => __( 'Top', 'swifty-site-designer' ),
                'bottom' => __( 'Bottom', 'swifty-site-designer' ),
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[set_vertical]',
            'section' => $section,
            'default' => 'default',
            'type' => 'radio',
            'label' => __( 'Vertical distance from side', 'swifty-site-designer' ),
            'choices' => array(
                'default' => __( 'Default', 'swifty-site-designer' ),
                'custom' => __( 'Custom', 'swifty-site-designer' ) . ':',
            )
        ) );

        $this->ui_add_setting_and_control( $wp_customize, array(
            'code' => $code_section . '[px_vertical]',
            'section' => $section,
            'default' => '0',
            'type' => 'text',
        ) );
    }

    // todo add title parameter instead of this key trick translation?
    public function ui_helper_add_details( $wp_customize, $key ) {
        $section_name = $key;
        $name = swifty__( $section_name, 'swifty-site-designer' );
        if( $section_name === 'mainmenu' ) {
            $name = __( 'main menu', 'swifty-site-designer' );
        }
        if( $section_name === 'bodytext' ) {
            $name = __( 'normal text', 'swifty-site-designer' );
        }
        if( $section_name === 'siteicon' ) {
            $name = __( 'site icon', 'swifty-site-designer' );
        }
        $code_section = 'ssd_details[' . $section_name . ']';
        $section = 'ssd_details_section_area_' . $section_name;

        $wp_customize->add_section(
            'ssd_details_section_area_' . $section_name,
            array(
                'title' => ucfirst( str_replace( '_', ' ', $name ) ),
                'description' => '',
                'priority' => 1,
                'panel' => 'ssd_panel_details',
            )
        );

        if( $key !== 'headlines' && $key !== 'bodytext' && $key !== 'siteicon' ) {
            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[on]',
                'section' => $section,
                'default' => 'default',
                'type' => 'radio',
                'label' => __( 'Show', 'swifty-site-designer' ) . ' ' . $name, // possibly wrong for some languages
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'on' => __( 'Show', 'swifty-site-designer' ),
                    'off' => __( 'Hide', 'swifty-site-designer' ),
                )
            ) );

            $this->ui_add_setting_and_control( $wp_customize, array(
                'code' => $code_section . '[inarea]',
                'section' => $section,
                'default' => 'default',
                'type' => 'select',
                'label' => __( 'Show in area', 'swifty-site-designer' ),
                'choices' => array(
                    'default' => __( 'Default', 'swifty-site-designer' ),
                    'topbar' => ucfirst( __( 'topbar', 'swifty-site-designer' ) ),
                    'header' => ucfirst( __( 'header', 'swifty-site-designer' ) ),
                    'navbar' => ucfirst( __( 'navbar', 'swifty-site-designer' ) ),
                    'sidebar' => ucfirst( __( 'sidebar', 'swifty-site-designer' ) ),
                    'extrasidebar' => __( 'Extra Sidebar', 'swifty-site-designer' ),
                    'footer' => ucfirst( __( 'footer', 'swifty-site-designer' ) ),
                    'bottombar' => ucfirst( __( 'bottombar', 'swifty-site-designer' ) ),
                )
            ) );
        }
    }

    public function hook_customize_preview_init() {
        global $swifty_build_use;
        $bust_add = '?swcv=ssd_' . '3.0.1';

        if( $swifty_build_use == 'build' ) {
            wp_enqueue_script( 'ssd_steal_js', get_template_directory_uri() . '/ssd/js/preview.min.js' . $bust_add, array( 'jquery' ), '20130508', true );
        } else {
//            wp_enqueue_script( 'ssd_require_config_js', get_template_directory_uri() . '/ssd/js/require_config.js', array( 'ssd_require_js' ), '1.0', false );
//            wp_enqueue_script( 'ssd_require_js', get_template_directory_uri() . '/ssd/js/libs/swiftylib/require.js', array( 'jquery' ), '1.0', false );
//            wp_enqueue_script( 'ssd_steal_js', get_template_directory_uri() . '/ssd/js/libs/steal/steal.js' . $bust_add, array( 'jquery' ), '1.0', false );
//            wp_localize_script( 'ssd_steal_js', 'steal', array(
//                'configPath' => get_template_directory_uri() . '/ssd/js/stealconfig.js',// . $bust_add,
//                'main' => "../preview.js" . $bust_add,
//                'bundlesPath' => get_template_directory_uri() . '/ssd/js'
//            ) );

//            wp_enqueue_script( 'ssd_require_config_js', get_template_directory_uri() . '/ssd/js/require_config.js' . $bust_add, array( 'ssd_require_js' ), '1.0', false );
//            wp_enqueue_script( 'ssd_require_js', get_template_directory_uri() . '/ssd/js/libs/swiftylib/require.js' . $bust_add, array( 'jquery' ), '1.0', false );
//
//            wp_enqueue_script( 'ssd_steal_js', get_template_directory_uri() . '/ssd/js/preview.js' . $bust_add, array( 'ssd_require_config_js', 'jquery' ), '20130508', true );

            wp_enqueue_script( 'ssd_require_config_js', get_template_directory_uri() . '/ssd/js/require_config.js', array( 'ssd_require_js' ), '1.0', false );
            wp_enqueue_script( 'ssd_require_js', get_template_directory_uri() . '/ssd/js/libs/swiftylib/require.js', array( 'jquery' ), '1.0', false );
    //        wp_enqueue_script( 'ssd_swcreator_js', get_template_directory_uri() . '/ssd/js/swcreator.js', array( 'ssd_require_config_js' ), '1.0', true );
    //        wp_enqueue_script( 'ssd_require_js', get_template_directory_uri() . '/ssd/js/libs/swiftylib/require.js' );
    //        wp_enqueue_script( 'ssd_lessc', get_template_directory_uri() . '/ssd/js/libs/swiftylib/lessc.js' );
            wp_enqueue_script( 'ssd_steal_js', get_template_directory_uri() . '/ssd/js/preview.js', array( 'ssd_require_config_js', 'customize-preview'/*, 'ssd_lessc'*/ ), '20130508', false );
        }

        wp_enqueue_style( 'ssd_steal_css', get_template_directory_uri() . '/ssd/js/preview.css' . $bust_add, array(), '20130508', 'all' );

        wp_localize_script( 'ssd_steal_js', 'ssd_data2', array(
            'ssd_url' => get_template_directory_uri() . '/',
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'swifty_allow_external' => get_user_option( 'swifty_allow_external' )
        ) );
        wp_localize_script( 'ssd_steal_js', 'ssd_data', array(
            'ssd_url' => get_template_directory_uri() . '/',
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'swifty_allow_external' => get_user_option( 'swifty_allow_external' )
        ) );
    }

    public function hook_customize_controls_enqueue_scripts() {
        global $swifty_build_use;
        $bust_add = '?swcv=ssd_' . '3.0.1';
        $includes = array( 'jquery', 'customize-controls',
            // These modules and probably used by the WP customizer, but fail if requirejs is loaded first
            'backbone', 'jquery-ui-core', 'jquery-ui-mouse', 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-slider'
        );

        if( $swifty_build_use == 'build' ) {
            wp_enqueue_script( 'ssd_customize', get_template_directory_uri() . '/ssd/js/customize.min.js' . $bust_add, $includes, '20130508', true );
        } else {
            wp_enqueue_script( 'ssd_require_config_js', get_template_directory_uri() . '/ssd/js/require_config.js' . $bust_add, array( 'ssd_require_js' ), '1.0', false );
            wp_enqueue_script( 'ssd_require_js', get_template_directory_uri() . '/ssd/js/libs/swiftylib/require.js' . $bust_add, $includes, '1.0', false );

            wp_enqueue_script( 'ssd_customize', get_template_directory_uri() . '/ssd/js/customize.js' . $bust_add, array( 'ssd_require_config_js', 'customize-controls' ), '20130508', true );
        }

        wp_localize_script( 'ssd_customize', 'ssd_data', array(
            'ssd_url' => get_template_directory_uri() . '/',
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'sitename' => get_bloginfo( 'name' ),
            'swifty_allow_external' => get_user_option( 'swifty_allow_external' ),
            'tmpl' => array(
                'colors' => $this->get_zip_colors()
            ),
            'ss2_hosting_name' => apply_filters( 'swifty_SS2_hosting_name', false )
        ) );

        // Add default WP media manager js/css etc
        wp_enqueue_media();

        global $ssd_oLocale;

        wp_localize_script( 'ssd_customize', 'swifty_data', array(
            'i18n' => $ssd_oLocale
        ) );
    }

    public function get_zip_colors() {
        $path = dirname( __FILE__ ) . '/../tmpl/colors';
        $colors = array();

        foreach( glob( $path . '/*.less' ) as $filename ) {
            $less = swifty_read_text( $filename );

            $lines = explode( '\n', $less );
            foreach( $lines as $line ) {
                $pos = strpos( $line, '@swifty_color_3: #' );
                if( $pos >= 0 ) {
                    $line = substr( $line, $pos + 17 );
                    $pos = strpos( $line, ';' );
                    if( $pos >= 0 ) {
                        array_push( $colors, array(
                            'ky' => basename( $filename, '.less' ),
                            'color' => substr( $line, 0, $pos )
                        ) );
                    }
                }
            }
        }
        
        return $colors;
    }

    public function hook_customize_sanitize_ssd_settings_obj( $value/*, $setting*/ ) {
        $upload_dir = wp_upload_dir();

        $this->test = $value;

        $test = array();

        // Find all stuff images in the settings
        $check_hash = preg_match_all( '/(http[s]*:\/\/stuff\.swifty\.online.*swifty=)/', $value, $links );
        foreach( $links[1] as $link ) {
            list( $link ) = explode( '?', $link );
            $linkparts = explode( '/', $link );
            $file = array_pop( $linkparts );
            $filename = trailingslashit( $upload_dir[ 'path' ] ) . $file;
            $fileurl = trailingslashit( $upload_dir[ 'url' ] ) . $file;
            $fileurl = str_replace( '/', '\/', $fileurl );
            $fn = str_replace( '\/', '/', $link );

            // dorh Temp trick to handle incorrect DNS on AMH servers

            $host = 'stuff.swifty.online';
            $dns = '8.8.8.8';  // Google Public DNS
            $ip = `nslookup $host $dns`; // the backticks execute the command in the shell
            $ips = array();
            if(preg_match_all('/Address: ((?:\d{1,3}\.){3}\d{1,3})/', $ip, $match) > 0){
                $ips = $match[1];
            }
            if( count( $ips ) > 0 ) {
                $fn = str_replace( 'stuff.swifty.online', $ips[ 0 ], $fn );
            }

            // Copy the stuff image to the users hosting account
//            copy( str_replace( '\/', '/', $link ), $filename );
            copy( $fn, $filename );

            // Replace the image in the settings by the url to the image in the user hosting account
            $value = str_replace( $link, $fileurl, $value );

            $test[] = $link;
//            $test[] = $filename;
//            $test[] = $upload_dir[ 'url' ];
//            $test[] = $filename;
//            $test[] = $fileurl;
        }

        $this->test = $test;
//        $this->test = $value;

        return $value;
    }

    public function hook_customize_sanitize_ssd_css( $value/*, $setting*/ ) {
        $this->test = $value;

        $buffer = swifty_read_text( dirname( __FILE__ ) . '/../../style.css' );
        $buffer .= $value;

        // Remove comments
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

        // Remove space after colons
        $buffer = str_replace(': ', ':', $buffer);

        // Remove whitespace
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);

        // Collapse adjacent spaces into a single space
        $buffer = preg_replace('!\s+!', ' ', $buffer);

        // Remove spaces that might still be left where we know they aren't needed
        $buffer = str_replace(array('} '), '}', $buffer);
        $buffer = str_replace(array('{ '), '{', $buffer);
        $buffer = str_replace(array('; '), ';', $buffer);
        $buffer = str_replace(array(', '), ',', $buffer);
        $buffer = str_replace(array(' }'), '}', $buffer);
        $buffer = str_replace(array(' {'), '{', $buffer);
        $buffer = str_replace(array(' ;'), ';', $buffer);
        $buffer = str_replace(array(' ,'), ',', $buffer);

        return $buffer;
    }

    public function hook_customize_save_response() {
        return array(
            'test' => $this->test
        );
    }
}

////////////////////////////////////////

function swifty_hide_info_blocks() {
    if( LibSwiftyPluginView::is_ss_mode() ) {
        // Hide info blocks at the top of panels (in Swifty mode).
        ?>
        <style>
            #customize-info {
                display: none !important;
            }
            .customize-section-title h3, .customize-info .accordion-section-title {
                display: none !important;
            }
        </style>
        <?php
    }
}

////////////////////////////////////////

function swifty_show_wizard() {
    global $swifty_build_use;

    ?>
    <style>
        .wp-full-overlay {
            position: inherit;
        }
        .ssd_wizard_wrapper {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            z-index: 510000;
            background-color: white;
        }
    </style>
    <div class="ssd_wizard_wrapper"></div>
    <?php

    if( $swifty_build_use == 'build' ) {
        $swifty_font_url = get_template_directory_uri() . '/css/swifty-font.css';
    } else {
        $swifty_font_url = get_template_directory_uri() . '/ssd/lib/swifty_plugin/css/swifty-font.css';
    }

    $font_version = (int) '1461829806000';

    wp_enqueue_style(
        'swifty-font.css',
        $swifty_font_url,
        array(),
        $font_version,
        'all'
    );

    wp_enqueue_style( 'ssd-font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css' );
}

////////////////////////////////////////

function swifty_init_php_output() {
    swifty_hide_info_blocks();

    if( ! LibSwiftyPluginView::wordpress_is_at_least_version( '4.5' ) ) {
        swifty_fix_overlay_footer_css();
    }

    $show_wizard = apply_filters( 'swifty_get_ssd_show_wizard', 'show' );
    if( $show_wizard === 'show' ) {
        swifty_show_wizard();
    }
}

add_action( 'customize_controls_print_footer_scripts', 'swifty_init_php_output' );

////////////////////////////////////////

function swifty_fix_overlay_footer_css() {
    ?>
    <style>
        .expanded .wp-full-overlay-footer {
            border-top: 1px solid #ddd;
            height: 45px;
            left: 0;
            position: fixed;
            width: 300px;
        }
    </style>
    <?php
}

////////////////////////////////////////

function swifty_sanitize_setting( $value ) {
    // dorh Add better sanitizing.
    return $value;
}

////////////////////////////////////////

function hook_swifty_customize_save_after() {
    do_action( 'swifty_set_ssd_show_wizard', 'hide' );
}

add_action( 'customize_save_after', 'hook_swifty_customize_save_after' );

////////////////////////////////////////

new SwiftySiteDesignerCustomizer();
