<?php
if ( function_exists('get_field') ):

	if (get_field('page_sections')){
		$sectionCount = count( get_field('page_sections') ); //GET THE COUNT 
		$previousSection = 'none'; 
	}
	// check if the flexible content field has rows of data
	if( have_rows('page_sections') ):
		$count = 1;
		while ( have_rows('page_sections') ) : the_row(); ?>
			<?php 
			$anchor = get_sub_field('anchor');
			$style = get_sub_field('style');
			$pre_title = get_sub_field('pre_title'); 
			$main_title = get_sub_field('main_title'); 
			$introduction = get_sub_field('introduction'); 
			?>

			<section id="<?php echo $anchor; ?>" class="clearfix
			row-index-<?php echo $count; ?> 
			page-section style-<?php echo $style; ?> <?php if ($count == $sectionCount) { echo 'last'; } ?>" >


			<?php if ($pre_title OR $main_title OR $introduction): ?>
				<div class="section-introduction center-align">
					<div class="container ">
							<?php if ($pre_title): ?>
									<h5 class="pre-title wow fadeInUp" data-wow-delay=".2s">
										<?php echo $pre_title; ?> 
									</h5>
							<?php endif; ?>

							<?php if ($main_title): ?>
									<h2 class="section-title wow fadeInUp" data-wow-delay=".4s">
										<span><?php echo $main_title; ?></span> 
									</h2>
							<?php endif; ?>

							<?php if ($introduction): ?>
									<div class="lead-para <?php if( get_row_layout() != 'plain_text' ) { echo 'introduction-text-top'; } ?> wow fadeInUp" data-wow-delay=".6s">
										<?php echo $introduction; ?> 
									</div>
							<?php endif; ?>
					</div>
				</div> 
			<?php endif; ?>

			<?php 

			if( have_rows('section_content') ):
				$contentCount = 1;
				while ( have_rows('section_content') ) : the_row(); ?>

					<div class="section-inner <?php echo get_row_layout(); ?> ">

					<!-- plain text  -->	
					<?php if( get_row_layout() == 'plain_text' ): ?>

						<?php 
						$content = get_sub_field('content'); 
						$buttons = get_sub_field('buttons'); 
						?>

						<div class="container ">
							<div class="wysiwyg ">
								<?php the_sub_field('content'); ?>
							</div>
						</div>
						<?php elseif( get_row_layout() == 'fullwidth_section' ): ?>
							<?php 
							$content = get_sub_field('content'); 
							?>

							<div class="wysiwyg ">
								<?php the_sub_field('content'); ?>
							</div>
						<?php elseif( get_row_layout() == 'instagram_feed' ): ?>
							<?php 
							$content = get_sub_field('content'); 
							?>
							<div class="wide-container">
								<div class="wysiwyg ">
									<?php the_sub_field('content'); ?>
								</div>
							</div>


			        <!-- latest news -->
			        <?php elseif( get_row_layout() == 'news' ): ?>
			        	<?php
			        	$sponsors = get_sub_field('sponsors'); 
			        	$title = get_sub_field('title'); 
			        	$background_title = get_sub_field('background_title'); 
			        	?>
			        	<div class="container">
			        		<div class="feature-title-wrapper">
			        			<span class="background-title"><?php echo $background_title; ?></span>
				        		<h2><?php echo $title; ?></h2>
				        	</div>

				        	<div class="row">
				        		<div class="col s12 m12 l8">
				        			<div class="row">

						        	<?php 
						        	   // the query
						        	   $the_query = new WP_Query( array(
						        	      'post_type' => 'post',
						        	      'posts_per_page' => 5,
						        	   )); 
						        	   $postCount = 0;
						        	?>

						        	<?php if ( $the_query->have_posts() ) : ?>
						        	  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

						        	    <?php if($postCount == 0) {?>
						        	    	<div class="col s12 m6">
						        	    	<?php get_template_part('template-parts/template', 'archive-listing'); ?>
						        	    </div>
						        	    <div class="col s12 m6">
						        	    <?php } else { ?>

						        	    	<div class="small-post">
						        	    		<a href="<?php the_permalink();?>">

							        	    		<?php the_post_thumbnail('thumbnail'); ?>
							        	    		<h5><?php the_title(); ?></h5>
							        	    		<?php get_template_part('template-parts/template', 'post-meta'); ?>
							        	    	</a>
						        	    	</div>
						        	    <?php } ?>
						        	    <?php $postCount++; ?>
						        	  <?php endwhile; ?>
						        	</div>
						        	  <?php wp_reset_postdata(); ?>

						        	<?php else : ?>
						        	  <p><?php __('No News'); ?></p>
						        	<?php endif; ?>
						        </div>

						        </div>
						        <div class="col s12 m12 l4">

						        	<?php if ($sponsors): ?>
						        		<div class="sponsor-block">
						        			<h4 class="sponsor-title">From our sponsors</h4>
						        			<?php foreach( $sponsors as $post ): 

						        			    // Setup this post for WP functions (variable must be named $post).
						        			    setup_postdata($post); 
						        			    $link = get_field('link');

						        			    ?>
						        			    	
						        			       <a href="<?php if ($link) {echo $link['url']; } else { the_permalink(); } ?>"> 
						        			       	<?php the_post_thumbnail(); ?>
						        			       </a>
						        			    
						        			<?php endforeach; ?>
						        		</div>
						        	<?php endif; ?>
						        </div>
						    </div>

			        	</div>

			        <?php elseif( get_row_layout() == 'whats_on_slider' ): ?>

	        		<!-- this is the spaced slider -->
	        		<?php
	       	 			$title = get_sub_field('title'); 
	       	 			$populate_with = get_sub_field('populate_with'); 
	       	 			$buttons = get_sub_field('buttons'); 
	       	 			$term = get_sub_field('term'); 
	       	 			$slides = get_sub_field('slides'); 
	       	 			$expand_wide = get_sub_field('expand_wide'); 

	       	 			$numSlides = get_sub_field('number_of_slides_displayed_on_desktop');
	       	 		
	       	 			if (!$numSlides) {
	       	 				$numSlides = 2; 
	       	 			}

	       	 			if (is_array($numSlides)) {
	       	 				$numSlides = $numSlides[0]; 
	       	 			}

	       	 			// print_r( $numSlides);
	       	 		?>
	       	 		
	       	 		<div class="container">
	       	 			<?php if ($title): ?>
	       	 				<div class="header-buttons clearfix">
    	       	 				<h2 class="section-inner-title">
    	       	 					<?php echo $title; ?>
    	       	 				</h2>
    						</div>
	       	 			<?php endif; ?>
	       	 		</div>

	       	 		<div class="slider-section-wrapper">
    	       	 		<div class="wide-container">

    	       	 			<div class="event-slider-container">
	    	       	 		<div class="slider event-slider slider-count-<?php echo $numSlides; ?>">
	    	       	 			<?php if ($populate_with == 'manual'): ?>
		    							<?php if ($slides): ?>
		    							    <?php while ( have_rows('slides') ) : ?>
		    								    <?php the_row(); ?>

		    								    <?php 
		    								    	$image = get_sub_field('image');
		    								    	$date = get_sub_field('date'); 
		    								    	$title = get_sub_field('title'); 
		    								    	$category = get_sub_field('category'); 
		    								    	$link_url = get_sub_field('link_url'); 
													$class='';
													if($link_url) {
														$postid = url_to_postid($link_url);
														
														if($postid > 1 && get_post_type($postid)=='tribe_events')	{
															$class='tribe-event-url event-modal';
														} else {
															$class='custom-url not-in-md';
														}
													}
		    								    ?>
		    								    <div class="slide">
			    								    <div class="card">

							    		       	 			<div class="card-image-container">
							    	       	 					<?php if ($image): ?>
			    								    				<a href="<?php echo $link_url; ?>" class="<?php echo $class;?>">
	    	       	 			       	 							<div class="card-ratio-image-container">
							    		       	 						<img src="<?php echo $image['sizes']['small-card-size']; ?>" alt="<?php echo $image['title']; ?>">
							    		       	 					</div>
							    		       	 					</a>
							    		       	 				<?php endif; ?>
							    		       	 				
							    		       	 				<div class="card-event-category">
			    								    				<?php echo $category; ?>
							    		       	 				</div>
							    		       	 			</div>

			    								    	<div class="card-inner">
			    								    		<div class="card-event-date">
			    								    			<?php echo $date; ?>
			    								    		</div>
			    								    		<div class="card-event-name">
			    								    		<a href="<?php echo $link_url; ?>" class="<?php echo $class;?>">
			    								    			<?php echo $title; ?>
			    								    		</a>
			    								    		</div>
			    								    	</div>
			    								    </div>
			    								</div>


		    									<?php endwhile; ?>
		    							<?php endif; ?>
	    	       	 			<?php elseif ($populate_with == 'term'): ?>
	    	       	 				<?php 
	    	       	 				$slugs = array(); 
	    	       	 				foreach ($term as $termeach) {
	    	       	 					array_push($slugs, $termeach->slug);
	    	       	 				}

	    	       	 				$number_of_events = get_sub_field('number_of_events'); 
	    	       	 				if (!$number_of_events) {
	    	       	 					$number_of_events = 6; 
	    	       	 				}
			       	 				$events = tribe_get_events(
			       	 					array('post_type' => 'tribe_events', 
			       	 						'posts_per_page' => $number_of_events,
											'meta_key'		=>'_EventStartDate',
											'orderby'		=>'_EventStartDate',
											'order'			=>'ASC',
			       	 						'eventDisplay'=>'upcoming',
			       	 						'tax_query'=> array(
		   	 						            array(
		   	 						                'taxonomy' => 'tribe_events_cat',
		   	 						                'field' => 'slug',
		   	 						                'terms' => $slugs
		   	 						                )
		   	 						            )
			       	 					) 
			       	 				);

	    	       	 				if ( $events ) {
	    	       	 					foreach ($events as $post ) {

	    	       	 						setup_postdata( $post ); ?>

		    								    <div class="slide">
			    								    <?php get_template_part('templates/template', 'event-card'); ?>
			    								</div>

	    	       	 							<?php }
	    	       	 							/* Restore original Post Data */
	    	       	 							wp_reset_postdata();
	    	       	 						} else {
	    	       	 							echo "No upcoming events";
	    	       	 						} ?>
       	 						<?php else: ?>

	    	       	 				<?php 
	    	       	 				$slugs = array(); 

	    	       	 				$number_of_events = get_sub_field('number_of_events'); 
	    	       	 				if (!$number_of_events) {
	    	       	 					$number_of_events = 6; 
	    	       	 				}
			       	 				$events = tribe_get_events(
			       	 					array('post_type' => 'tribe_events', 
			       	 						'posts_per_page' => $number_of_events,
											'meta_key'		=>'_EventStartDate',
											'orderby'		=>'_EventStartDate',
											'order'			=>'ASC',
			       	 					) 
			       	 				);

	    	       	 				if ( $events ) {
	    	       	 					foreach ($events as $post ) {

	    	       	 						setup_postdata( $post ); ?>

		    								    <div class="slide">
			    								    <?php get_template_part('template-parts/template', 'event-card'); ?>
			    								</div>

	    	       	 							<?php }
	    	       	 							/* Restore original Post Data */
	    	       	 							wp_reset_postdata();
	    	       	 						} else {
	    	       	 							echo "No upcoming events";
	    	       	 						} ?>


	    	       	 			<?php endif; ?>

	    	       	 		</div>

		    	       	 		<div class="event-slider-navigation">
		    	       	 		</div>
    	       	 			<div class="event-slider-container">

	    	       	 	</div>
	    	       	 	<div class="button-container">
		    	       	 	<a href="<?php echo tribe_get_events_link(); ?>" class="theme-button">View event calendar</a>
		    	       	 </div>
	    	       	 </div>

	       	 		<div class="container"> 
						<?php if ($buttons): ?>
						    <div class="theme-buttons hide-on-large-only">
						    <?php while ( have_rows('buttons') ) : ?>
							    <?php the_row(); ?>
							    <?php 
							    	$button_label = get_sub_field('button_label');
							    	$button_link = get_sub_field('button_link'); 
   	 	    				    	$is_pdf = strpos($button_link, '.pdf');

							    ?>
									<a href="<?php echo $button_link; ?>" class="theme-button light chevron" target="<?php if ($is_pdf){ echo '_none';} ?>">
			        					<?php if ($button_label){ echo $button_label; } else { echo 'Read more'; } ?>
			        				</a>
								<?php endwhile; ?>
							</div>
						<?php endif; ?>
					</div>
					<!-- content columns  -->
				    <?php elseif( get_row_layout() == 'content_columns' ): ?>
				    	<div class="container">
				    	<?php 
				    		$number_of_columns = get_sub_field('number_of_columns');
				    		$colClass = "m6";

				    		if ($number_of_columns == 2) {
				    			$colClass = "m6";
				    		} elseif ($number_of_columns == 3) {
				    			$colClass = "m4";
				    		} elseif ($number_of_columns == 4) {
				    			$colClass = "m3";
				    		}
				    	?>
	 					<?php if (get_sub_field('columns')): ?>

	 					    <div class="row">

		 					    <?php while ( have_rows('columns') ) : the_row(); ?>
		 	    					<?php 
		 	    					$image = get_sub_field('image');
		 	    					$title = get_sub_field('title');
		 	    					$content = get_sub_field('content');
		 	    					$buttons = get_sub_field('buttons');


		 	    					?>

		 					    		<div class="col s12 <?php echo $colClass; ?>">
		 					    				<?php if ($image): ?>
											         <img src="<?php echo $image['sizes']['card-size']; ?>" alt="<?php echo $image['title']; ?>">
											    <?php endif; ?>

		 					    				<h3><?php echo $title; ?></h3>
		 					    				<div class="wysiwyg">
		 					    					<?php echo $content; ?>
		 					    				</div>
	    				        				<?php if ($buttons){ displayButtons($buttons); } ?>


		 					    		</div>
		 						<?php endwhile; ?>
								</div>


	 					<?php endif; //end if columns ?>
	 				</div>
				<?php elseif( get_row_layout() == 'feature_grid' ): ?>

		        	<?php $grid_boxes = get_sub_field('grid_boxes'); ?>

		        	<div class="container">
		        		<div class="feature-grid">
				        	<?php // Check rows exists.
				        	if( have_rows('grid_boxes') ):

				        	    // Loop through rows.
				        	    while( have_rows('grid_boxes') ) : the_row();

				        	        // Load sub field value.
				        	        $background_image = get_sub_field('background_image');
				        	        $title = get_sub_field('title');
				        	        $content = get_sub_field('content');
				        	        $link = get_sub_field('link');
				        	        ?>
				        	        <div class="feature-grid-box" style="background-image:url(<?php if ($background_image){ echo $background_image['url']; }?>)">
			        	       	 		<a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>">
				        	        
			        	        			<h2><?php echo $title; ?></h2>
			        	        			<div class="feature-box-content">
				        	        			<?php echo $content; ?>
				        	        		</div>	
				        	        		<?php if ($link): ?>
					        	        		<div class="cursive-learn-more " >Learn more</div>
					        	        	<?php endif;?>
			        	        		</a>
		        	        		</div>
				        	    <?php // End loop.
				        	    endwhile;

				        	// No value.
				        	else :
				        	    // Do something...
				        	endif;
				        	?>
				        </div>
				    </div>


				<!-- alternating rows -->
				<?php elseif( get_row_layout() == 'leftright_row' ): ?>


					<?php 

					$background_title = get_sub_field('background_title'); 
					$content = get_sub_field('content'); 

					$image = get_sub_field('image'); 
					$style = get_sub_field('style'); 

					// echo $contentCount;
					if ($contentCount% 2 == 0) {
						$imageDirection = 'fadeInLeft';
						$titleDirection = 'fadeInRight';
					} else {
						$imageDirection = 'fadeInRight';
						$titleDirection = 'fadeInLeft';
					}
					?>

					<div class="alt-row clearfix ">
						<div class="<?php if ($style != 'extrawide') {echo 'container';} else { echo 'extrawide '; } ?> clearfix">


									<div class="alt-row-column">

										<div class="alt-row-content">
											<span class="background-title wow <?php echo $titleDirection; ?>" data-wow-delay=".2s"><?php echo $background_title; ?></span>
											<div class="wysiwyg wow fadeIn" data-wow-delay=".8s">
												<?php echo $content; ?>
											</div>
										</div>	
									</div>

									<div class="alt-row-column alt-row-image-column">
										<?php if ($image): ?>
											<div class="alt-row-image wow <?php echo $imageDirection; ?>" data-wow-delay=".3s" >
												<img src="<?php echo $image['sizes']['card-size']; ?>" alt="<?php echo $image['alt']; ?>">
											</div>
											<?php endif; ?>
										</div>
							</div>
						</div>


					<!-- basic gallery -->
					<?php elseif( get_row_layout() == 'gallery_block' ): ?>
						<?php 
							$content = get_sub_field('content');
							$images = get_sub_field('images');
						?>
						<div class="container">
							<div class="row">
								<div class="col s12 l5">
									<div class="gallery-content wow fadeIn" data-wow-delay=".2s"">
										<?php echo $content; ?>
											
										</div>
								</div>
								<div class="col s12 l7">

								<div class="gallery-slider-container slider-container">
									<div class="gallery-slider">
									
									<?php 

					        		if( $images ):
					        			foreach( $images as $image ): ?>
					        				<div class="gallery-slide">
						        				<img src="<?php echo $image['sizes']['gallery-size']; ?>" alt="<?php echo $image['alt']; ?>">
						        			</div>

					        			<?php endforeach; 

					        		endif; ?>
					        	</div>
					        	<div class="gallery-slider-navigation slider-navigation">
					        	</div>

					        </div>
					        </div>

			        	</div>
			        </div>


			        <!-- single image -->
			        <?php elseif( get_row_layout() == 'icon_grid' ): ?>
			        	<?php $icon_grid_boxes = get_sub_field('icon_grid_boxes'); ?>

			        	<div class="container">
			        		<div class="row">
					        	<?php // Check rows exists.
					        	$iconsDelay = .2;
					        	if( have_rows('icon_grid_boxes') ):

					        	    // Loop through rows.
					        	    while( have_rows('icon_grid_boxes') ) : the_row();

					        	        // Load sub field value.
					        	        $image = get_sub_field('image');
					        	        $title = get_sub_field('title');
					        	        $content = get_sub_field('content');
					        	        ?>

					        	        <div class="col s12 m4">
					        	        	<div class="icon-grid-box center-align">
					        	        	<div class="icon-grid-image wow fadeInUp" data-wow-delay="<?php echo $iconsDelay; ?>s">
					        	        		<?php if ($image): ?>
				        	        				<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>">
				        	        			<?php endif; ?>
			        	        			</div>					        	        
			        	        			<h4 class="wow fadeIn" data-wow-delay="<?php echo $iconsDelay+.2; ?>s""><?php echo $title; ?></h4>
			        	        			<div class="wow fadeIn" data-wow-delay="<?php echo $iconsDelay+.4; ?>s">
				        	        			<?php echo $content; ?>
				        	        		</div>	
			        	        		</div>
			        	        		</div>
					        	    <?php // End loop.
					        	    $iconsDelay += .2;
					        	    endwhile;

					        	// No value.
					        	else :
					        	    // Do something...
					        	endif;
					        	?>
					        </div>
					    </div>






			        <?php endif; //end if contents get_row_layout?>
					<?php $contentCount++; ?>
			    </div>
			    <?php endwhile; ?>
			<?php endif; ?>


		</section> 

		<?php $count++; ?>

	<?php endwhile; ?>

<?php endif; ?>
<?php  $previousSection = get_row_layout(); ?>
<?php endif; ?>