<?php
/**
 * The template for displaying the tabbed slideshow section
 *
 * @package WordPress
 * @subpackage Edition
 * @since Edition 1.0
 */
global
$tw_order_class, 	// section order
$tw_section; 		// section object

$tw_section = themewich_section_object(); // section post options
$section_id = get_the_ID( $post ); // section id

// Set post classes
$tw_section->post_classes  = array(
	'section-full',
	'section',
	$tw_order_class,
	$tw_section->title_class
); ?>

<!-- Home Section -->
<div <?php post_class($tw_section->post_classes); ?>>

	<?php if ( tw_issetor($tw_section->title_class) && $tw_section->title_class == 'has-title' ) : ?>
		<div class="container">
			<div class="col-md-12">
				<?php get_template_part(TW_SECTIONS_DIR .'/section-title'); ?>
			</div><!--.col-md-12 -->
		</div><!-- .container -->
	<?php endif; ?>

	<?php themewich_section_edit(); ?>

	<div class="pagetitleloading">
        <div class='single-slideshow info pager full homeslideshow loading-dark <?php echo tw_issetor($tw_section->autoplay); ?>' data-pause="<?php echo tw_issetor($tw_section->delay); ?>" data-pager="#bx-pager-<?php echo (int) $section_id; ?>" data-speed="1000">

			<!-- Slider -->
			<ul class='bxslider'>
				<?php
					global $wp_query; // for pagination

					// Query arguments
					$args = array(
						'ignore_sticky_posts'   => 1,   					// Ignore Sticky Posts
						'post_type'             => array( 'post' ), 		// Get posts
						'nopaging'				=> false,					// Use paging
						'paged'                 => 1,  						// Add pagination
					);

					// Filter the latest posts by type chosen
					$args = themewich_post_args($args);

					wp_reset_query(); 					// Reset query
					$wp_query = new WP_Query($args); 	// Query the posts

					// Start the loop
					while ($wp_query->have_posts()) : $wp_query->the_post();

					$tw_categories = ag_get_cats_array(3);   // get all post categories
				?>
				<?php if ( has_post_thumbnail() ) : ?>
					<li>
						<?php
							// Set More Link
							global $more; $more = 0;
							// Print slide image with animation classes
							the_post_thumbnail( 'fullslideshow', array( 'class' => 'kanimate kenburnsreverse' ) );
						?>

						<div class="overlay"></div>

						<div class="slidecaptionwrap <?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?>">
							<div class="slidecaptioncenter container">
								<div class="slidecaption" style="width:80%">

									<div class="badge">
										<?php echo ag_get_cats(3); ?>
									</div>

									<a href="<?php the_permalink(); ?>" class="slidetitlelink">
										<h2 class="slidetitle">
											<?php the_title(); ?>
										</h2>
									</a>

									<?php if ( 'post' == get_post_type() ) : ?>
										<div class="tiny-details">
											<?php themewich_date('human') ?>
											<span class="tw-bullet">&nbsp;&bull;&nbsp;</span>
											<span class="author">
												<?php _e( 'By', 'themewich' );?> <span><?php tw_author_link('posts'); ?></span>
											</span>
										</div>

										<?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?>
										<a class="button" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
											<?php
												if ( isset($matches[1]) && $matches[1] ) {
													echo esc_html( $matches[1] );
												} else {
													_e('Read More', 'themewich');
												}
											?>
										</a>
									<?php endif; ?>

								</div><!-- .slidecaption -->

								<div class="slidereview" style="width:20%">
									<?php echo themewich_review_circle('title'); ?>
								</div>

							</div><!-- .slidecaptioncenter -->
						</div><!-- .slidecaptionwrap -->

						<div class="navfade-bottom"></div>

						<a class="slidelink" href="<?php the_permalink(); ?>">
							<?php the_title(); ?>
						</a>
					</li>

					<?php
						endif;
						endwhile; 			// End Loop
					?>
			</ul><!-- .bxslider -->
		</div><!-- .single-slideshow -->

		<div class="pagercontainer">
			<?php
				$number_of_posts = ($wp_query->have_posts()) ? sizeof($wp_query->posts) : 0; // Calculate column widths depending on number of slides

				switch ($number_of_posts) {
					case 2:
						$column = 'col-md-6';
						break;
					case 3:
						$column = 'col-md-9';
						break;
					default:
						$column = 'col-md-12';
						break;
				}
			?>

			<div class="container">
				<div class="pagertable <?php echo sanitize_html_class( $column ); ?> info carousel nopager autoplay homeslideshow" data-width="270" style="display:block;" data-pause="1000">
					<ul id="bx-pager-<?php echo (int) $section_id; ?>" class="bx-slider-pager">
						<?php
							$i=0; // counter for the slide index
							while ($wp_query->have_posts()) : $wp_query->the_post();

							$tw_categories = ag_get_cats_array(3);   // get all post categories
						?>

						<?php if ( has_post_thumbnail() ) : ?>
								<li class="<?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?>">
									<a data-slide-index="<?php echo (int) $i; ?>" href="#">
										<span class="pagertitle">
											<?php
												if (strlen($post->post_title) > 45) {
													echo mb_substr( the_title( $before = '', $after = '', false ), 0, 45 ) . '...';
					                            } else {
					                                the_title();
					                            }
					                        ?>
										</span>

										<span class="time-ago">
											<?php if ( 'post' == get_post_type() ) : ?>
												<?php themewich_date('human'); ?>
											<?php else: ?>
												<?php if ( function_exists( 'wc_get_template' ) ) : ?>
													<?php wc_get_template( 'loop/price.php' ); ?>
												<?php endif; ?>
											<?php endif; ?>
										</span>
									</a>
								</li>
						<?php else : $i--; endif; ?>

						<?php
							$i++; // increment counter
							endwhile;
							wp_reset_postdata(); // clean up after query
						?>
					</ul>
				</div><!--.pagertable-->
			</div><!--.container-->
		</div><!-- .pagercontainer -->
	</div><!--.pagetitleloading-->
</div><!--.homesection-->