function widget($args, $instance)
    {
        global $post;
        extract($args);
        wp_reset_postdata();
        wp_reset_query();
        $title = apply_filters('widget_title', $instance['title']);
        $post_orderby = isset($instance['post_orderby']) ? $instance['post_orderby'] : 'ID';
        $post_order = isset($instance['post_order']) ? $instance['post_order'] : 'DESC';
        $post_filter_condition = isset($instance['post_filter_condition']) ? $instance['post_filter_condition'] : 'both';
        $post_rows = isset($instance['rows']) ? (int) $instance['rows'] : 1;
        $autoplay = isset($instance['auto']) ? $instance['auto'] : null;
        $widget_column = 3;
        $post_shows = isset($instance['post_shows']) ? (int) $instance['post_shows'] : 16;
        $current_postID = get_the_ID();
        $post_category = mars_get_current_postterm($current_postID, 'category');
        $post_tag = mars_get_current_postterm($current_postID, 'post_tag');
        $i = 0;
        $posts_query = array('post_type' => 'post', 'showposts' => $post_shows, 'posts_per_page' => $post_shows, 'post__not_in' => array($current_postID));
        if ($post_filter_condition == 'both') {
            if (isset($post_category)) {
                $posts_query['category__in'] = $post_category;
            }
            if (isset($post_tag)) {
                $posts_query['tag__in'] = $post_tag;
            }
        }
        if ($post_filter_condition == 'category') {
            if (isset($post_category)) {
                $posts_query['category__in'] = $post_category;
            }
        }
        if ($post_filter_condition == 'post_tag') {
            if (isset($post_tag)) {
                $posts_query['tag__in'] = $post_tag;
            }
        }
        if ($post_orderby) {
            $posts_query['orderby'] = $post_orderby;
        }
        if ($post_order) {
            $posts_query['order'] = $post_order;
        }
        $wp_query = new WP_Query($posts_query);
        if ($wp_query->found_posts > 1) {
            ?>
			<?php 
            if ($widget_column == 3) {
                ?>
          		<div id="carousel-latest-<?php 
                print $this->id;
                ?>
" class="carousel carousel-<?php 
                print $this->id;
                ?>
 slide video-section" data-ride="carousel">
          	<?php 
            } elseif ($widget_column == 2) {
                ?>
          		<div class="row post-section meta-maxwidth-360">
          	<?php 
            } else {
                ?>
          		<div id="carousel-latest-<?php 
                print $this->id;
                ?>
" class="carousel carousel-<?php 
                print $this->id;
                ?>
 slide video-section" <?php 
                if ($post_shows > 3) {
                    ?>
 data-ride="carousel"<?php 
                }
                ?>
>
          	<?php 
            }
            ?>
          		<?php 
            if ($widget_column == 3) {
                ?>
                    <div class="section-header">
                <?php 
            } elseif ($widget_column == 2) {
                ?>
                	<div class="col-sm-12 section-header">
	          	<?php 
            } else {
                ?>
	          		<div class="section-header">
	          	<?php 
            }
            ?>
	          			<?php 
            if ($title) {
                ?>
                        	<h3><?php 
                print $title;
                ?>
</h3>
                        <?php 
            }
            ?>
                        <?php 
            if ($widget_column != 2) {
                ?>
				            <?php 
                if ($post_shows >= 3 || $post_shows == -1) {
                    ?>
					            <?php 
                    if ($post_shows >= $wp_query->post_count && $post_shows > 3 * $post_rows) {
                        ?>
						            <ol class="carousel-indicators section-nav">
						            	<li data-target="#carousel-latest-<?php 
                        print $this->id;
                        ?>
" data-slide-to="0" class="bullet active"></li>
						                <?php 
                        $c = 0;
                        for ($j = 1; $j < $wp_query->post_count; $j++) {
                            if ($j % (3 * $post_rows) == 0 && $j < $post_shows) {
                                $c++;
                                print '<li data-target="#carousel-latest-' . $this->id . '" data-slide-to="' . $c . '" class="bullet"></li> ';
                            }
                        }
                        ?>
						            </ol>
					            <?php 
                    }
                    ?>
				            <?php 
                }
                ?>
	                    <?php 
            }
            ?>
                    </div>
                    
                    <?php 
            if ($widget_column == 3) {
                ?>
	                    <div class="latest-wrapper">
	                    	<div class="row">
			                     <div class="carousel-inner">
			                       	<?php 
                if ($wp_query->have_posts()) {
                    $i = 0;
                    while ($wp_query->have_posts()) {
                        $wp_query->the_post();
                        $i++;
                        ?>
				                       	<?php 
                        if ($i == 1) {
                            ?>
				                       		<div class="item active">
				                       	<?php 
                        }
                        ?>
	
				                       		<div class="col-sm-4 col-xs-6 item <?php 
                        print $this->id;
                        ?>
-<?php 
                        print get_the_ID();
                        ?>
">
				                                <?php 
                        if (has_post_thumbnail()) {
                            print '<a href="' . get_permalink(get_the_ID()) . '">' . get_the_post_thumbnail(null, 'video-lastest', array('class' => 'img-responsive')) . '</a>';
                        }
                        ?>
	                                            <h3><a href="<?php 
                        the_permalink();
                        ?>
"><?php 
                        the_title();
                        ?>
</a></h3>
		                                     </div> 
					                    <?php 
                        if ($i % ($post_rows * 3) == 0 && $i < $post_shows) {
                            ?>
</div><div class="item"><?php 
                        }
                    }
                    ?>
</div><?php 
                }
                ?>
 
			                        </div>
	                            </div>
	                    </div>
                    <?php 
            }
            ?>
                </div><!-- /#carousel-->		
		<?php 
        }
        wp_reset_query();
        wp_reset_postdata();
    }
    function widget($args, $instance)
    {
        global $post;
        extract($args);
        wp_reset_postdata();
        wp_reset_query();
        $title = apply_filters('widget_title', $instance['title']);
        $video_orderby = isset($instance['video_orderby']) ? $instance['video_orderby'] : 'ID';
        $video_order = isset($instance['video_order']) ? $instance['video_order'] : 'DESC';
        $video_filter_condition = isset($instance['video_filter_condition']) ? $instance['video_filter_condition'] : 'both';
        $video_rows = isset($instance['rows']) ? (int) $instance['rows'] : 1;
        $autoplay = isset($instance['auto']) ? $instance['auto'] : null;
        $widget_column = 3;
        $video_shows = isset($instance['video_shows']) ? (int) $instance['video_shows'] : 16;
        $current_videoID = get_the_ID();
        $video_category = mars_get_current_postterm($current_videoID, 'categories');
        $video_tag = mars_get_current_postterm($current_videoID, 'video_tag');
        $i = 0;
        $videos_query = array('post_type' => 'video', 'showposts' => $video_shows, 'posts_per_page' => $video_shows, 'post__not_in' => array($current_videoID));
        if ($video_filter_condition == 'both') {
            if (!empty($video_category)) {
                $videos_query['tax_query'] = array(array('taxonomy' => 'categories', 'field' => 'id', 'terms' => $video_category));
            }
            if (!empty($video_tag)) {
                $videos_query['tax_query'] = array(array('taxonomy' => 'video_tag', 'field' => 'id', 'terms' => $video_tag));
            }
        }
        if ($video_filter_condition == 'categories') {
            if (!empty($video_category)) {
                $videos_query['tax_query'] = array(array('taxonomy' => 'categories', 'field' => 'id', 'terms' => $video_category));
            }
        }
        if ($video_filter_condition == 'video_tag') {
            if (!empty($video_tag)) {
                $videos_query['tax_query'] = array(array('taxonomy' => 'video_tag', 'field' => 'id', 'terms' => $video_tag));
            }
        }
        if (isset($video_orderby)) {
            if ($video_orderby == 'views') {
                $videos_query['meta_key'] = 'count_viewed';
                $videos_query['orderby'] = 'meta_value_num';
            } elseif ($video_orderby == 'likes') {
                $videos_query['meta_key'] = 'like_key';
                $videos_query['orderby'] = 'meta_value_num';
            } else {
                $videos_query['orderby'] = $video_orderby;
            }
        }
        if (isset($video_order)) {
            $videos_query['order'] = $video_order;
        }
        if (isset($post->ID)) {
            $videos_query['post__not_in'] = array($post->ID);
        }
        $wp_query = new WP_Query($videos_query);
        if ($wp_query->found_posts > 1) {
            ?>
			<?php 
            if ($widget_column == 3) {
                ?>
          		<div id="carousel-latest-<?php 
                print $this->id;
                ?>
" class="carousel carousel-<?php 
                print $this->id;
                ?>
 slide video-section" data-ride="carousel">
          	<?php 
            } elseif ($widget_column == 2) {
                ?>
          		<div class="row video-section meta-maxwidth-360">
          	<?php 
            } else {
                ?>
          		<div id="carousel-latest-<?php 
                print $this->id;
                ?>
" class="carousel carousel-<?php 
                print $this->id;
                ?>
 slide video-section" <?php 
                if ($video_shows > 3) {
                    ?>
 data-ride="carousel"<?php 
                }
                ?>
>
          	<?php 
            }
            ?>
          		<?php 
            if ($widget_column == 3) {
                ?>
                    <div class="section-header">
                <?php 
            } elseif ($widget_column == 2) {
                ?>
                	<div class="col-sm-12 section-header">
	          	<?php 
            } else {
                ?>
	          		<div class="section-header">
	          	<?php 
            }
            ?>
	          			<?php 
            if ($title) {
                ?>
                        	<h3><?php 
                print $title;
                ?>
</h3>
                        <?php 
            }
            ?>
                        <?php 
            if ($widget_column != 2) {
                ?>
				            <?php 
                if ($video_shows >= 3 || $video_shows == -1) {
                    ?>
					            <?php 
                    if ($video_shows >= $wp_query->post_count && $video_shows > 3 * $video_rows) {
                        ?>
						            <ol class="carousel-indicators section-nav">
						            	<li data-target="#carousel-latest-<?php 
                        print $this->id;
                        ?>
" data-slide-to="0" class="bullet active"></li>
						                <?php 
                        $c = 0;
                        for ($j = 1; $j < $wp_query->post_count; $j++) {
                            if ($j % (3 * $video_rows) == 0 && $j < $video_shows) {
                                $c++;
                                print '<li data-target="#carousel-latest-' . $this->id . '" data-slide-to="' . $c . '" class="bullet"></li> ';
                            }
                        }
                        ?>
						            </ol>
					            <?php 
                    }
                    ?>
				            <?php 
                }
                ?>
	                    <?php 
            }
            ?>
                    </div>
                    
                    <?php 
            if ($widget_column == 3) {
                ?>
	                    <div class="latest-wrapper">
	                    	<div class="row">
			                     <div class="carousel-inner">
			                       	<?php 
                if ($wp_query->have_posts()) {
                    $i = 0;
                    while ($wp_query->have_posts()) {
                        $wp_query->the_post();
                        $i++;
                        ?>
				                       	<?php 
                        if ($i == 1) {
                            ?>
				                       		<div class="item active">
				                       	<?php 
                        }
                        ?>
	
				                       		<div class="col-sm-4 col-xs-6 item <?php 
                        print $this->id;
                        ?>
-<?php 
                        print get_the_ID();
                        ?>
">
				                       			<div class="item-img">
				                                <?php 
                        if (has_post_thumbnail()) {
                            print '<a href="' . get_permalink(get_the_ID()) . '">' . get_the_post_thumbnail(null, 'video-lastest', array('class' => 'img-responsive')) . '</a>';
                        }
                        ?>
													<a href="<?php 
                        echo get_permalink(get_the_ID());
                        ?>
"><div class="img-hover"></div></a>
												</div>				                                
	                                            <h3><a title="<?php 
                        the_title();
                        ?>
" href="<?php 
                        the_permalink();
                        ?>
"><?php 
                        the_title();
                        ?>
</a></h3>
												<?php 
                        print apply_filters('mars_video_meta', null);
                        ?>
		                                     </div> 
					                    <?php 
                        //if ( $i % 3 == 0 ){
                        if ($i % ($video_rows * 3) == 0 && $i < $video_shows) {
                            ?>
</div><div class="item"><?php 
                        }
                    }
                    ?>
</div><?php 
                }
                ?>
 
			                        </div>
	                            </div>
	                    </div>
                    <?php 
            }
            ?>
                </div><!-- /#carousel-->
				<?php 
            if ($autoplay == 'on') {
                ?>
				<script>
				(function($) {
				  "use strict";
					jQuery('.carousel-<?php 
                print $this->id;
                ?>
').carousel({
						 pause: false
					});
				})(jQuery);
				</script>				
				<?php 
            }
            ?>
		<?php 
        }
        wp_reset_query();
        wp_reset_postdata();
    }