<?php 
get_header();
$id = get_query_var('cat');
?>
<section id="main" role="main">
    <div class="container">
        <div class="row">
            <div class="col-sm-9 right-content profile-bg">
                <div class="row">
                    <?php if($redux_demo['ad1'] != '') {
                        echo '<div class="header_ad">' . $redux_demo['ad1'] . '</div>';
                    } ?>
                    <div class="category-wrap">
                        <div class="col-sm-4 border-right">
                            <div class="outer">
                                <div class="inner">
                                    <h2><?php $name = single_cat_title( '', false ); echo $name; ?></h2>
                                    <?php
                                    $category_description = category_description();
                                    if ( ! empty( $category_description ) ) { ?>
                                        <h3><?php echo $category_description;?></h3>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-2 border-right">
                            <div class="outer">
                                <div class="inner">
                                    <div class="total-posts">
                                    <?php
                                    $postsInCat = get_term_by('name', $name, 'category');
                                    $postsInCat = $postsInCat->count;
                                    echo $postsInCat;
                                    ?>
                                    </div>
                                    <div class="total-posts-description"><?php _e('Total Posts', 'Aruna');?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="outer">
                                <div class="inner">
                                    <?php
                                    $week = date('W');
                                    $year = date('Y');
                                    $args = array();
                                    $args['posts_per_page'] = 1;
                                    $args['category__in'] = $id;
                                    $args['meta_key'] = '_ratings_up';
                                    $args['orderby'] = 'meta_value';
                                    $args['order'] = 'DESC';
                                    //$args['year'] = $year;
                                    //$args['w'] = $week - 1;
                                    //performance improvements
                                    $args['no_found_rows'] = true;
                                    $args['update_post_term_cache'] = true;
                                    $args['update_post_meta_cache'] = true;

                                    $query = new WP_Query($args);
                                    if($query->have_posts() ) : $query->the_post();
                                        global $post; 
                                        $type = get_post_format($post->ID);
                                        $upvotes = get_post_meta($post->ID, '_ratings_up', true);
                                        $downvotes = get_post_meta($post->ID, '_ratings_down', true);
                                        $thumb = '';
                                        if($type == 'image' || $type == 'standard') {
                                            $thumb = get_post_meta($post->ID, '_standard_image', true);
                                        }
                                        elseif($type == 'gallery') {
                                            $thumb = get_post_meta($post->ID, '_slider_image1', true);
                                        }
                                        if($thumb == '') {
                                            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                                        }
                                        if($thumb == '' && $type == 'video') {
                                            $thumb = get_template_directory_uri() . '/img/video_placeholder.jpg';
                                        }
                                        if($thumb == '') {
                                            $thumb = get_template_directory_uri() . '/img/missing_img.png';
                                        }
                                        $resized = aq_resize($thumb, 82, 82, true);
                                        if($resized == '') {
                                            $resized = $thumb;
                                            //we can't resize the original image so we keep that one
                                        }
                                        if($resized != '' || $thumb != '') { ?>
                                            <figure>
                                                <a href="<?php the_permalink();?>">
                                                    <img src="<?php if($resized != '') echo $resized; else echo $thumb; ?>" alt="<?php the_title();?>" />
                                                </a>
                                            </figure>
                                        <?php } ?>

                                        <div class="text">
                                            <h4><?php _e('Top post (last week)', 'Aruna');?></h4>
                                            <div class="separator"></div>
                                            <a href="<?php the_permalink();?>" class="user-name"><?php the_title();?></a>
                                            <div class="counters">
                                                <span class="item"><?php echo $upvotes . ' '; _e('up votes', 'Aruna');?></span> -
                                                <span class="item"><?php echo $downvotes . ' '; _e('down votes', 'Aruna');?></span>
                                            </div>
                                        </div>
                                    <?php endif; wp_reset_postdata();?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-9"> 
                        <?php if($redux_demo['ad2'] != '') {
                            echo '<div class="header_ad">' . $redux_demo['ad2'] . '</div>';
                        } ?>
                        <div class="main-wrap">
                            <?php 
                            if ( have_posts() ) :
                                $count = 1;
                                while ( have_posts() ) : the_post(); 
                                    if($count == 3 && $redux_demo['ad3'] != '') {
                                        echo '<div class="header_ad ad_between">' . $redux_demo['ad3'] . '</div>';
                                    }
                                    get_template_part( 'content', get_post_format() );
                                    $count++;
                                endwhile; 
                                get_template_part('includes/pagination');
                            endif; 
                            ?>
                        </div>
                    </div>
                    <?php get_sidebar('right');?>
                </div>
            </div>
            <?php get_sidebar('left');?>
        </div>
    </div>
</section>
<?php get_footer();?>