/**
 * Bootloader
 *
 * @since 1.0
 */
function searchwp_live_search_init()
{
    load_plugin_textdomain('swplas', false, dirname(plugin_basename(__FILE__)) . '/languages/');
    // if an AJAX request is taking place, it's potentially a search so we'll want to prepare for that
    // else we'll prep the environment for the search form itself
    if (defined('DOING_AJAX') && DOING_AJAX) {
        include_once dirname(__FILE__) . '/includes/class-client.php';
        // Relevanssi support
        include_once dirname(__FILE__) . '/includes/class-relevanssi-bridge.php';
        $client = new SearchWP_Live_Search_Client();
        $client->setup();
    } else {
        include_once dirname(__FILE__) . '/includes/class-form.php';
        $form = new SearchWP_Live_Search_Form();
        $form->setup();
    }
}
    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     *
     * @return string|void
     */
    public function form($instance)
    {
        $widget_title = isset($instance['title']) ? $instance['title'] : __('Search', 'swplas');
        $widget_placeholder = isset($instance['placeholder']) ? $instance['placeholder'] : __('Search for...', 'swplas');
        $widget_destination = isset($instance['destination']) ? $instance['destination'] : '';
        // we'll piggyback SearchWP itself to pull a list of search engines
        $widget_engine = isset($instance['engine']) ? $instance['engine'] : 'default';
        $engines = array();
        if (class_exists('SearchWP')) {
            $engines['default'] = 'Default';
            $searchwp = SearchWP::instance();
            $searchwp_engines = $searchwp->settings['engines'];
            foreach ($searchwp_engines as $engine => $engine_settings) {
                if (isset($engine_settings['searchwp_engine_label'])) {
                    $engines[$engine] = $engine_settings['searchwp_engine_label'];
                }
            }
        }
        // we're going to utilize SearchWP_Live_Search_Form to populate the config dropdown
        $widget_config = isset($instance['config']) ? $instance['config'] : 'default';
        if (!class_exists('SearchWP_Live_Search_Form')) {
            include_once dirname(__FILE__) . '/class-form.php';
        }
        $form = new SearchWP_Live_Search_Form();
        $form->setup();
        ?>

		<p>
			<label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title:');
        ?>
</label>
			<input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo esc_attr($widget_title);
        ?>
">
		</p>
		<?php 
        if (!empty($engines)) {
            ?>
		<p>
			<label for="<?php 
            echo $this->get_field_id('engine');
            ?>
"><?php 
            _e('SearchWP Engine:');
            ?>
</label>
			<select name="<?php 
            echo $this->get_field_name('engine');
            ?>
" id="<?php 
            echo $this->get_field_id('engine');
            ?>
">
				<?php 
            foreach ($engines as $engine_name => $engine_label) {
                ?>
					<option value="<?php 
                echo esc_attr($engine_name);
                ?>
" <?php 
                selected($widget_engine, $engine_name);
                ?>
><?php 
                echo esc_html($engine_label);
                ?>
</option>
				<?php 
            }
            ?>
			</select>
		</p>
		<?php 
        }
        ?>
		<p>
			<label for="<?php 
        echo $this->get_field_id('config');
        ?>
"><?php 
        _e('Configuration:');
        ?>
</label>
			<select name="<?php 
        echo $this->get_field_name('config');
        ?>
" id="<?php 
        echo $this->get_field_id('config');
        ?>
">
				<?php 
        foreach ($form->configs as $config => $val) {
            ?>
					<option value="<?php 
            echo esc_attr($config);
            ?>
" <?php 
            selected($widget_config, $config);
            ?>
><?php 
            echo esc_html($config);
            ?>
</option>
				<?php 
        }
        ?>
			</select>
		</p>
		<?php 
        $swpuniqid = uniqid('swp');
        ?>
		<p><a href="#" class="button searchwp-widget-<?php 
        echo $swpuniqid;
        ?>
"><?php 
        _e('Advanced', 'searchwp');
        ?>
</a></p>
		<div class="searchwp-live-search-widget-advanced" style="display:none;">
			<p>
				<label for="<?php 
        echo $this->get_field_id('placeholder');
        ?>
"><?php 
        _e('Placholder:');
        ?>
</label>
				<input class="widefat" id="<?php 
        echo $this->get_field_id('placeholder');
        ?>
" name="<?php 
        echo $this->get_field_name('placeholder');
        ?>
" type="placeholder" value="<?php 
        echo esc_attr($widget_placeholder);
        ?>
">
			</p>
			<p>
				<label for="<?php 
        echo $this->get_field_id('destination');
        ?>
"><?php 
        _e('Destination fallback URL (optional):');
        ?>
</label>
				<input class="widefat" id="<?php 
        echo $this->get_field_id('destination');
        ?>
" name="<?php 
        echo $this->get_field_name('destination');
        ?>
" type="text" value="<?php 
        echo esc_attr($widget_destination);
        ?>
">
			</p>
		</div>
		<script type="text/javascript">
			jQuery(document).ready(function($){
				$('.searchwp-widget-<?php 
        echo $swpuniqid;
        ?>
').click(function(){
					var $advanced = $(this).parents().find('.searchwp-live-search-widget-advanced');
					if($advanced.is(':visible')){
						$advanced.hide();
					}else{
						$advanced.show();
					}
					return false;
				});
			});
		</script>
	<?php 
    }