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

function serpentsoft_tabs_widget_init() {
    register_widget('serpentsoft_tabs_widget');
}

class serpentsoft_tabs_widget extends WP_Widget {

    function serpentsoft_tabs_widget() {
        $widget_ops = array('classname' => 'w-tabs', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-tabs');
        $this->WP_Widget('id-widget-tabs', theme_name . ' - Tabs', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);
        //$title = apply_filters('widget_title', $instance['title'] );
        //$title = '';
        $tab1_title = $instance['tab1_title']; if(!isset($tab1_title) || empty($tab1_title) || $tab1_title == '') { $tab1_title = __('Popular', 'serpentsoft'); }
        $tab1_postsnum = $instance['tab1_postsnum']; 

        $tab2_title = $instance['tab2_title']; if(!isset($tab2_title) || empty($tab2_title) || $tab2_title == '') { $tab2_title = __('Recent', 'serpentsoft'); }
        $tab2_postsnum = $instance['tab2_postsnum'];

        $tab3_title = $instance['tab3_title']; if(!isset($tab3_title) || empty($tab3_title) || $tab3_title == '') { $tab3_title = __('Comments', 'serpentsoft'); }
        $tab3_postsnum = $instance['tab3_postsnum']; ?>

        <section class="widget w-tabs"><h3 class="hidden">Tabs Widget</h3>
            <div class="widget-content clearfix">
                <ul class="tabs-header clearfix">
                    <li class="btn-theme active"><a data-target="#<?php echo $args['widget_id']; ?>_popular" data-toggle="tab"><?php echo $tab1_title; ?></a></li>
                    <li class="btn-theme"><a data-target="#<?php echo $args['widget_id']; ?>_recent" data-toggle="tab"><?php echo $tab2_title; ?></a></li>
                    <li class="btn-theme"><a data-target="#<?php echo $args['widget_id']; ?>_comments" data-toggle="tab"><?php echo $tab3_title; ?></a></li>
                </ul>

                <div class="tab-content">
                    <div class="tab-pane active" id="<?php echo $args['widget_id']; ?>_popular">
                        <aside class="row-fluid">
                            <h4 class="hidden"><?php echo $tab1_title; ?></h4>

                            <div class="posts-in-widget">
                                <div class="tabs-special-style">
                                    <ul class="media-list">
                                        <?php wp_reset_query();
                                        if (is_numeric($tab1_postsnum) && ($tab1_postsnum > 0)) :
                                            $popularposts = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $tab1_postsnum, 'post_status' => 'publish', 'meta_key' => 'serpentsoft_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'));
                                            while ($popularposts->have_posts()) : $popularposts->the_post(); ?>

                                                <li class="media">
                                                    <article>
                                                        <a href="<?php the_permalink(); ?>" class="span3" title="<?php the_title(); ?>">
                                                            <div class="media-object thumb-effect">
                                                                <div class="mask"></div>
                                                                
                                                                <?php serpentsoft_grab_post_banner_figure($popularposts->post->ID, array(), 'small'); ?>
                                                            </div>
                                                        </a>

                                                        <div class="media-body span9">
                                                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                                                                <h5 class="media-heading"><?php the_title(); ?></h5>
                                                            </a>

                                                            <div class="media">
                                                                <span title="Published Time"><i class="bo-icon-time"></i>
                                                                    <?php the_time(serpentsoft_getDateTimeFormat('dt')); ?>
                                                                </span>
                                                                
                                                                <?php echo serpentsoft_draw_small_stars($popularposts->post->ID); ?>
                                                            </div>
                                                        </div>
                                                    </article>
                                                </li>

                                            <?php endwhile; ?>
                                        <?php endif; ?>

                                    </ul>
                                </div>
                            </div>
                        </aside> <!-- End Row-Fluid -->
                    </div> <!-- End Popular Tab Pane -->


                    <div class="tab-pane" id="<?php echo $args['widget_id']; ?>_recent">
                        <aside class="row-fluid">
                            <h4 class="hidden"><?php echo $tab2_title; ?></h4>

                            <div class="posts-in-widget">
                                <div class="tabs-special-style">
                                    <ul class="media-list">
                                        <?php
                                        if (is_numeric($tab2_postsnum) && ($tab2_postsnum > 0)) :
                                            $recentposts = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $tab2_postsnum, 'post_status' => 'publish'));
                                            while ($recentposts->have_posts()) : $recentposts->the_post();
                                                ?>

                                                <li class="media">
                                                    <article>
                                                        <a href="<?php the_permalink(); ?>" class="span3" title="<?php the_title(); ?>">
                                                            <div class="media-object thumb-effect">
                                                                <div class="mask"></div>
                                                                <?php serpentsoft_grab_post_banner_figure($recentposts->post->ID, array(), 'small'); ?>
                                                            </div>
                                                        </a>

                                                        <div class="media-body span9">
                                                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                                                                <h5 class="media-heading"><?php the_title(); ?></h5>
                                                            </a>

                                                            <div class="media">                                                                
                                                                <span title="Published Time"><i class="bo-icon-time"></i>
                                                                    <?php the_time(serpentsoft_getDateTimeFormat('dt')); ?>
                                                                </span>
                                                                
                                                                <?php echo serpentsoft_draw_small_stars($recentposts->post->ID); ?>
                                                            </div>
                                                        </div>
                                                    </article>
                                                </li>

                                            <?php endwhile; ?>
                                        <?php endif; ?>

                                    </ul>
                                </div>
                            </div>
                        </aside> <!-- End Row-Fluid -->
                    </div> <!-- End Recent Tab Pane -->


                    <div class="tab-pane" id="<?php echo $args['widget_id']; ?>_comments">
                        <aside class="row-fluid">
                            <h4 class="hidden">Comments</h4>

                            <div class="posts-in-widget">
                                <div class="tabs-special-style comments">
                                    <ul class="media-list">
                                        <?php $comments = get_comments('status=approve&number=' . $tab3_postsnum); ?>

                                        <?php foreach ($comments as $comment) { ?>
                                            <li class="media">
                                                <article>
                                                    <div class="span3 media-object">
                                                        <?php echo get_avatar($comment, '65'); ?>
                                                    </div>

                                                    <div class="media-body span9">
                                                        <h5 class="media-heading"><?php echo strip_tags($comment->comment_author); ?></h5>

                                                        <div class="media">
                                                            <span><i class="bo-icon-time"></i>
                                                                <?php $dt_format = serpentsoft_getDateTimeFormat('dt');
                                                                echo get_comment_date($dt_format, $comment->comment_ID); ?></span>

                                                            <p class="summary"><?php echo wp_html_excerpt($comment->comment_content, 60); ?>...</p>

                                                            <div class="leave-comment">
                                                                <a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>">Leave Comment</a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </article>
                                            </li>

                                        <?php } ?>
                                    </ul>
                                </div>
                            </div>                                        
                        </aside>
                    </div>

                </div>
            </div>

        </section>

    <?php }

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

        $instance['tab2_title'] = strip_tags($new_instance['tab2_title']);
        $instance['tab2_postsnum'] = strip_tags($new_instance['tab2_postsnum']);

        $instance['tab3_title'] = strip_tags($new_instance['tab3_title']);
        $instance['tab3_postsnum'] = strip_tags($new_instance['tab3_postsnum']);

        return $instance;
    }

    function form($instance) {
        $defaults = array('tab1_title' => 'Popular', 'tab1_postsnum' => '4', 'tab2_title' => 'Recent', 'tab2_postsnum' => '4', 'tab3_title' => 'Comments', 'tab3_postsnum' => '4', 'width' => '270');

        $instance = wp_parse_args((array) $instance, $defaults);
        ?>

        <p>
            <label><strong><?php _e('First Tab', 'serpentsoft'); ?>: </strong></label>

            <label for="<?php echo $this->get_field_id('tab1_title'); ?>">(<?php _e('Default: Popular', 'serpentsoft'); ?>)</label>

            <input id="<?php echo $this->get_field_id('tab1_title'); ?>" name="<?php echo $this->get_field_name('tab1_title'); ?>" value="<?php echo $instance['tab1_title']; ?>" class="widefat" type="text" />

        <p></p>

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

        </p>

        <hr />

        <p>
            <label><strong><?php _e('Second Tab', 'serpentsoft'); ?>: </strong></label>

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

        <p></p>

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

        </p>

        <hr />

        <p>
            <label><strong><?php _e('Third Tab', 'serpentsoft'); ?>: </strong></label>

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

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

<?php } } ?>