<?php

/*******************************************
 * search
 *******************************************/
class cake_search_widget extends WP_Widget {

	function __construct() {
		$widget_ops = array(
			'classname'   => 'cake_search_widget',
			'description' => esc_html__( 'A search form for your site.', 'cakeart' )
		);
		parent::__construct(
			'widget_cake_search_widget',
			esc_html__( 'Thim: Search On Header', 'cakeart' ), $widget_ops );
		add_action( 'load-widgets.php', array( &$this, 'thim_script' ) );
	}

	function thim_script() {
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );
	}

	function widget( $args, $instance ) {
		extract( $args, EXTR_SKIP );
		$background_color = empty( $instance['background_color'] ) ? '' : apply_filters( 'widget_background_color', $instance['background_color'] );

		if ( $background_color ) {
			$css = ' style="background: ' . $background_color . '"';
		} else {
			$css = "";
		}

		echo ent2ncr( $args['before_widget'] );

		echo '<a href="#" class="widget-smartsearch search-link" id="header-search" ' . $css . '></a>';

		echo ent2ncr( $args['after_widget'] );
	}

	function update( $new_instance, $old_instance ) {
		$instance                     = $old_instance;
		$instance['background_color'] = $new_instance['background_color'];

		return $instance;
	}

	function form( $instance ) {
		$instance         = wp_parse_args( (array) $instance, array( 'background_color' => '0 0' ) );
		$background_color = $instance['background_color'];
		?>

		<script type="text/javascript">
			jQuery(document).ready(function ($) {
				$('.thim-picker-color').wpColorPicker();
				$("body").bind("ajaxComplete", function () {
					$('.thim-picker-color').wpColorPicker();
				});
			});
		</script>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'background_color' ) ); ?>"><?php esc_html_e( 'Background Color', 'cakeart' ); ?></label>
			<input class="thim-picker-color" type="text" id="<?php echo esc_attr( $this->get_field_id( 'background_color' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'background_color' ) ); ?>" value="<?php echo esc_attr( $background_color ); ?>" />
		</p>
		<?php
	}
}

add_action( 'widgets_init', 'thim_register_cake_search_widget' );

function thim_register_cake_search_widget() {
	register_widget( 'cake_search_widget' );
}
