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

function serpentsoft_googlemap_widget_init() {
    register_widget('serpentsoft_googlemap_widget');
}

class serpentsoft_googlemap_widget extends WP_Widget {

    function serpentsoft_googlemap_widget() {
        $widget_ops = array('classname' => 'w-map');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-googlemap');
        $this->WP_Widget('id-widget-googlemap', theme_name . ' - Google Map', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);

        $title = apply_filters('widget_title', $instance['widget_title']);

        $googlemap_url = $instance['googlemap_url'];
        $height = $instance['height'];
        $view_larger_map = $instance['view_larger_map'];

        if ($googlemap_url) {
            echo $before_widget;

            if ($title) {
                echo $before_title;
                echo $title;
                echo $after_title;
            }

            echo serpentsoft_googlemap( htmlspecialchars_decode($googlemap_url), $height, $view_larger_map );

            echo $after_widget;
        }
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['widget_title'] = strip_tags($new_instance['widget_title']);
        $instance['googlemap_url'] = strip_tags($new_instance['googlemap_url']);
        $instance['height'] = strip_tags($new_instance['height']);
        $instance['view_larger_map'] = strip_tags($new_instance['view_larger_map']);

        return $instance;
    }

    function form($instance) {
        $defaults = array('widget_title' => __('Google Map', 'serpentsoft'), 'googlemap_url' => '', 'height' => '220', 'view_larger_map' => '');
        $instance = wp_parse_args((array) $instance, $defaults); ?>

        <style>
            .googlemap-text{
                display: block;
                width: 228px;
            }
            .googlemap-code{
                height: 75px;
            }

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

        <p>
            <label for="<?php echo $this->get_field_id('googlemap_url'); ?>"><?php _e('Google Map URL', 'serpentsoft'); ?></label><br />
            <small style="color: red;"><b>Warning:</b> Don't use the short url</small>
            <textarea class="googlemap-text googlemap-code" id="<?php echo $this->get_field_id('googlemap_url'); ?>" name="<?php echo $this->get_field_name('googlemap_url'); ?>"><?php echo $instance['googlemap_url']; ?></textarea>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Map 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']; ?>" />
        </p>

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