<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package daisy
 */

get_header(); 
?>
<?php
	
	if( !get_theme_mod('homepage_hide_slider') ){
	$slider_custom = get_theme_mod('home_page_slider_one_repeater');
	if( $slider_custom ){
	?>
		<div class="container main-slider">
			<div id="headerSlider" class="owl-carousel owl-theme">
				<?php 
				
					parse_str($slider_custom, $sliders );
					foreach ( $sliders as $slider ) {
						foreach( $slider as $item ){
							if( get_post_thumbnail_id($item['homepage_slider_select_post']) ) {
							?>
							<div class="item">
								<img src="<?php echo bfi_thumb(wp_get_attachment_url(get_post_thumbnail_id($item['homepage_slider_select_post']),'full'),array('width'=> 1100, 'height'=> 480)) ?>" alt="" />
								<?php
									if( !get_theme_mod('slider_hide_caption') ){
								?>
								<div class="post-detail">
									<div class="inner-post-detail">
										<h2><a href="<?php echo get_permalink($item['homepage_slider_select_post']); ?>"><?php echo get_the_title($item['homepage_slider_select_post']); ?></a></h2> <!-- Post Title-->
									</div>
								</div>
								<?php
									}	
								?>
							</div>
							<?php
							}	
						}
					}
				
				?>
			</div>
		</div>
	<?php
	}
	}
?>
<?php
	$box_colum = get_theme_mod('homepage_box_column') ? get_theme_mod('homepage_box_column') : '3';
	$box_style = get_theme_mod('rst_style_box') ? get_theme_mod('rst_style_box') : '1';
	$extend_boxs = get_theme_mod('home_page_extend_box_repeater_three');
	$class_box = ($box_colum == '3') ? 'col-md-4 col-sm-4' : 'col-md-3 col-sm-3';
	if($box_style == 2){
		$class_box_style = ' box_style_02';
	}else{
		$class_box_style = ' ';
	}
	if( !get_theme_mod('homepage_hide_extend') ){
	if( $extend_boxs ){
?>		
<div class="container"> 
	<div class="content-box-home row">
		<?php 
			parse_str($extend_boxs, $boxs );
			foreach ( $boxs as $box ) {
				foreach( $box as $item_box ){
				?>	
					<div class="<?php echo esc_attr($class_box) . esc_attr($class_box_style); ?>">
						<div class="rst-inner-box-home">
							<a href="<?php echo esc_url($item_box['rst_custom_link_box']) ?>">
								<div class="thumbnail-box">
								<img alt="" src="<?php echo bfi_thumb(wp_get_attachment_url($item_box['rst_custom_image_box'],'full'),array('width'=> 370, 'height'=> 220)) ?>">
								</div>
								<div class="rst-container-box-detail-home">
									<div class="rst-inner-box-detail-home">
										<h2>
											<?php echo $item_box['rst_custom_title_box'] ?>
										</h2> <!-- Post Title-->
									</div>
								</div>
							</a>
						</div>
					</div>
				<?php
				}
			}
		?>	
	</div>
</div>
<?php
	}
	}
?>

<!-- Blog Body 
====================================-->
<div id="tf-content">
	<div class="container"><!-- Container -->		
		<?php			
			$pagenavi = get_theme_mod('rst_general_pagenavi') ? get_theme_mod('rst_general_pagenavi') : 1;			
			$template = get_theme_mod('rst_general_template') ? get_theme_mod('rst_general_template') : 'large';			
			$layout = get_theme_mod('rst_general_layout') ? get_theme_mod('rst_general_layout') : 3;		
		?>
		<div class="row<?php echo ( $pagenavi == 3 ? ' rst-wrap-ajax' : '' ); ?><?php echo ( $layout == 2 ? ' has-sidebar-left' : '' ); ?><?php echo ( get_theme_mod('rst_general_layout') == 3 ? ' has-sidebar-right' : '' ); ?>"  data-sticky_parent=""><!-- Row -->
			<?php 
				$class = "";
				if( $layout == 1  && $template == 'large') {
					$class .= 'col-md-10 col-md-offset-1 rst-content-wrap';
				}
				if( $layout == 1 && $template == 'grid') {
					$class .= 'col-md-12 rst-content-wrap';
				}
				if( $layout == 2 ) {
					$class .= 'col-md-8 pull-right';
				}
				if( $layout == 3 ) {
					$class .= 'col-md-8 pull-left';
				}
			?>
			<?php
				global $wp_query;				
				$post_per_page = get_theme_mod('rst_general_numberpost') ? get_theme_mod('rst_general_numberpost') : get_option('posts_per_page');
				
				$args = array(
					'posts_per_page' 	=> absint($post_per_page),
					'paged' 			=> max( get_query_var( 'paged' ), get_query_var( 'page' ))
				);
				//$args = array_merge( $wp_query->query_vars, $args );
				$the_query = new WP_Query( $args );
				$wp_query = $the_query;
			?>
			<?php
				global $rst_blog;
				$column = get_theme_mod('rst_general_column') ? get_theme_mod('rst_general_column') : 3;				$excerpt_length = get_theme_mod('rst_general_excerpt_length');
				$rst_blog = array(
					'layout'		=> $layout,
					'template'		=> $template,
					'column' 			=> $column,
					'post_per_page'		=> $post_per_page,
					'excerpt_length'	=> $excerpt_length,
				);
			?>  
			<?php 
				if( $pagenavi == 3 ) {
					$rstkey =  uniqid();
					global $rst_pagenavi;
					$rst_pagenavi = array(
						'key' 		=> 'rst_'.$rstkey,
						'max-paged'	=> ceil(( $the_query->found_posts - $post_per_page ) / 3)
					);
			?>
			<script type="text/javascript">
				var rst_<?php echo esc_html($rstkey) ?> = new rst_blocks();
				rst_<?php echo esc_html($rstkey) ?>.query = '<?php echo str_replace( '\'', '\\\'', json_encode($args) ) ?>';
				rst_<?php echo esc_html($rstkey) ?>.atts = '<?php echo str_replace( '\'', '\\\'', json_encode($rst_blog) ) ?>';
				rst_<?php echo esc_html($rstkey) ?>.url = '<?php echo admin_url( "admin-ajax.php" ); ?>';
			</script>
			<?php } ?>
			<div class="<?php echo esc_attr($class) ?>"> <!-- Left Content for Blog Posts -->
				<?php 
					if( $template == 'grid' ) {
						$class_grid = "rst-grid column-".sanitize_html_class($column);
					}else{
						$class_grid= " ";
					}
				?>
				<div class="row <?php echo $class_grid ?> <?php echo ($pagenavi == 3 ? 'rst-inner-ajax' : '' ); ?>">
				<?php if ( $the_query->have_posts() ) : ?>						
					<?php while ($the_query->have_posts() ) : $the_query->the_post(); ?>
						<?php
							/*
							 * Include the Post-Format-specific template for the content.
							 * If you want to override this in a child theme, then include a file
							 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
							 */
							get_template_part( 'content', get_post_format() );
						?>

					<?php endwhile; ?>

				<?php else : ?>
					<?php get_template_part( 'content', 'none' ); ?>
				<?php endif; 
				?>
				</div>
				<div class="text-center"><!-- Pagination -->
					<?php 
						if( $pagenavi == 1 ) {
							rst_the_posts_navigation(); 

						}
						elseif( $pagenavi == 2 ) {
							rst_navigation_prev_next(); 

						}
						elseif( $pagenavi == 3 ) {
							rst_the_posts_navigation_ajax();
							
						}
					?>
				</div>
			</div><!-- #primary -->
			<?php
				if( $layout == 2 || $layout == 3 ) {
			?>
				<?php get_sidebar(); ?>
			<?php
				}
			?>
		</div><!-- end Row -->
	</div><!-- End Container -->
</div>
<?php get_footer(); ?>
