<?php add_action('widgets_init', 'serpentsoft_socialFbLikeBox_widget_init');

function serpentsoft_socialFbLikeBox_widget_init() {
    register_widget('serpentsoft_socialFbLikeBox_widget');
}

class serpentsoft_socialFbLikeBox_widget extends WP_Widget {

    function serpentsoft_socialFbLikeBox_widget() {
        $widget_ops = array('classname' => 'w-social-counter', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-social-fb-like-box');
        $this->WP_Widget('id-widget-social-fb-like-box', theme_name . ' - Facebook Like Box', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $page_url = $instance['page_url'];

        $width = $instance['width'];
        $height = $instance['height'];
        
        $show_header = $instance['show_header'];
        $show_stream = $instance['show_stream'];
        $show_faces = $instance['show_faces'];
        $show_border = $instance['show_border'];

        $color_scheme = $instance['color_scheme'];
        
        echo $before_widget;
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif; ?>

        <div class="social-fb-like-box">
            <div class="fb-like-box" data-href="<?php echo $page_url; ?>" data-width="<?php echo $width; ?>" data-height="<?php echo $height; ?>" data-colorscheme="<?php echo $color_scheme; ?>" data-show-faces="<?php echo $show_faces ? 'true' : 'false'; ?>" data-header="<?php echo $show_header ? 'true' : 'false'; ?>" data-stream="<?php echo $show_stream ? 'true' : 'false'; ?>" data-show-border="<?php echo $show_border ? 'true' : 'false'; ?>"></div>
        </div>
        
    <?php echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['page_url'] = $new_instance['page_url'];

        $instance['width'] = strip_tags($new_instance['width']);
        $instance['height'] = strip_tags($new_instance['height']);
        
        $instance['show_header'] = strip_tags($new_instance['show_header']);
        $instance['show_stream'] = strip_tags($new_instance['show_stream']);
        $instance['show_faces'] = strip_tags($new_instance['show_faces']);
        $instance['show_border'] = strip_tags($new_instance['show_border']);

        $instance['color_scheme'] = strip_tags($new_instance['color_scheme']);
        return $instance;
    }

    function form($instance) {
        $defaults = array('title' => __('Find us on Facebook', 'serpentsoft'),
            'page_url' => '',
            'width' => '270',
            'height' => '300',
            
            'show_header' => TRUE,
            'show_stream' => FALSE,
            'show_faces' => TRUE,
            'show_border' => TRUE,
            
            'color_scheme' => 'light'
        );
        $instance = wp_parse_args((array) $instance, $defaults); ?>

        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
                   value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('page_url'); ?>"><?php _e('Page URL', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('page_url'); ?>" name="<?php echo $this->get_field_name('page_url'); ?>"
                   value="<?php echo $instance['page_url']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Box Width', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>"
                   value="<?php echo $instance['width']; ?>" class="widefat" type="text" />
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Box Height', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>"
                   value="<?php echo $instance['height']; ?>" class="widefat" type="text" />
        </p>
        
        
        <p>
            <label for="<?php echo $this->get_field_id('show_header'); ?>"><?php _e('Show Header?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('show_header'); ?>" name="<?php echo $this->get_field_name('show_header'); ?>"
                   value="true" <?php if ($instance['show_header']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('show_stream'); ?>"><?php _e('Show Stream?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('show_stream'); ?>" name="<?php echo $this->get_field_name('show_stream'); ?>"
                   value="true" <?php if ($instance['show_stream']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('show_faces'); ?>"><?php _e('Show Faces?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('show_faces'); ?>" name="<?php echo $this->get_field_name('show_faces'); ?>"
                   value="true" <?php if ($instance['show_faces']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('show_border'); ?>"><?php _e('Show Border?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('show_border'); ?>" name="<?php echo $this->get_field_name('show_border'); ?>"
                   value="true" <?php if ($instance['show_border']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('color_scheme'); ?>"><?php _e('Color Scheme', 'serpentsoft'); ?> :</label>

            <select id="<?php echo $this->get_field_id('color_scheme'); ?>" name="<?php echo $this->get_field_name('color_scheme'); ?>">
                <option value="light" <?php if($instance['color_scheme'] == 'light') { echo ' selected="selected"'; } ?>><?php _e('White', 'serpentsoft'); ?></option>
                <option value="dark" <?php if($instance['color_scheme'] == 'dark') { echo ' selected="selected"'; } ?>><?php _e('Dark Gray', 'serpentsoft'); ?></option>
            </select>
        </p>
        
    <?php } } ?>