<?php
/**
 * The template for displaying the grid section with sidebar.
 *
 * @package WordPress
 * @subpackage Edition
 * @since Edition 1.0
 */
global 
$tw_order_class, 	// section order 
$tw_section, 		// section object
$grid_size,			// grid size
$post_ID, // post id
$paged_section;		// section pagination

$tw_section 	= themewich_section_object(); 		// section post options
$section_post 	= $post; // Store Section Post
$grid_size 		= 'fullgrid'; // Set first grid class

// Set post classes
$tw_section->post_classes  = array(
	'no-top-margin',
	'grid',
	'section',
	'grid-sidebar',
	$tw_order_class,
	$tw_section->title_class
); ?>

<div <?php post_class($tw_section->post_classes); ?>>
	<?php themewich_section_edit(); // edit button ?>

	<div class="container">
		<div class="rightside">
			<div class="rightcontent imagegrid">
				<?php get_template_part( TW_SECTIONS_DIR .'/section-title'); 	// Section title  ?>
				<div class="loading"></div>
				<div class="grid-wrapper" id="section-<?php echo (int) $post_ID; ?>">
					<div class="homeposts sidebar-grid">        
						<div class="isotopecontainer" data-value="2">
							<?php 
								// for pagination
								global $wp_query; 
								$paged_section = isset( $_GET['paged_section'] ) ? (int) $_GET['paged_section'] : 1;
								
								// Additional query arguments
								$args = array(
									'ignore_sticky_posts'   => 1,   					// Ignore Sticky Posts
									'post_type'             => array( 'post' ), 		// Get posts
									'paged'                 => $paged_section,  				// 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();  
							?>

							<div class="isobrick">
								<?php get_template_part(TW_TEMPLATES_DIR . '/template-grid'); ?>
							</div>
							
							<?php 
								$grid_size = 'halfgrid';  // Sest remaining grid size
								endwhile; 			// End Loop 
							?>
							<div class="clear"></div>
						</div>

						<?php 
							if ( isset($tw_section->section_pagination) && $tw_section->section_pagination != 'off') : 
								themewich_pagination($wp_query);
							endif; // end if pagination 
						?>

						<?php wp_reset_query(); 	// Reset Query  ?>

					</div><!--.homeposts-->
				</div>
                    
			</div><!--.rightcontent-->
			<div class="clear"></div>
		</div><!--.rightside-->

		<div class="sidebar">
			<div class="sidebarcontent">
				<?php
					if ($tw_section->custom == true) {
						global $post;
						$post_id = $post->ID;
					} else {
						$post_id = $section_post->ID; // Get correct post ID 
					}
					if ( ! acf_Widget::dynamic_widgets( 'Grid Section Sidebar', $post_id ) ) {
						dynamic_sidebar( 'Grid Section Sidebar' ); //fallback to default sidebar if nothing is set
					} 
				?>
            </div>
        </div>

    </div><!--.container-->
</div><!--.homesection-->