<?php 

$display_title_with_back_option = get_field('display_title_with_back_option');
$display_hero = get_field('display_hero');


if($display_hero) :



	$pre_title = get_field('pre_title'); 
	if (!isset($pre_title)){
		$pre_title = get_the_title();
	}
	$page_heading = get_field('page_heading'); 
	$video_full = get_field('video_full'); 
	$hero_background_image = get_field('hero_background_image'); 
	$buttons = get_field('buttons'); 
	$feature_image = get_the_post_thumbnail_url();

	?>

	<?php if ($hero_background_image): ?>
		<style>
			.hero {
				background-image: url(<?php echo $hero_background_image['url']; ?>);
			}
		</style>
	<?php endif; ?>




	<div class="hero style-dark">

		<div class="background-video">
			<div class="video-block-hover">
			</div>
			<?
				$video = get_field( 'video' );
				
	   		    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video, $match)) {	   		    
	   		    	$hero_video_id = $match[1];
	   		    }
	   		    


				if ( $video ) {
					// Add autoplay functionality to the video code
					if ( preg_match('/src="(.+?)"/', $video, $matches) ) {
						// Video source URL
						$src = $matches[1];
						
						// Add option to hide controls, enable HD, and do autoplay -- depending on provider
						$params = array(
	                                    'loop'  => 1,
	                                    'autoplay' => 1,
	                                    // 'autopause' => 0,
	                                    'muted' => true,
	                                    'mute' => true,
	                                    'controls' => 0,
	                                    'rel'  => 0,
	                                    'playlist' => $hero_video_id
						);
						
						$new_src = add_query_arg($params, $src);
						
						$video = str_replace($src, $new_src, $video);
						
						// add extra attributes to iframe html
						$attributes = 'frameborder="0" id="home-qwv"';
						
						$video = str_replace('></iframe>', ' ' . $attributes . '></iframe>', $video);
					}
				
					echo '<div class="embed-container hide-on-small-only">', $video, '</div>';
				}	?>
					
				</div>
		<div class="container">
			<div class="hero-inner">
				<h1 class="small"><?php echo $pre_title; ?></h1>
				<h2 class="h2"><?php echo $page_heading; ?></h2>
				<?php 
				// print_r($buttons);
				if ($buttons){ displayButtons($buttons); } ?>
			</div>
		</div>

		<?php if ($video): ?>
<!-- 		        <button id="play-fullscreen" class="btn btn-outline-white fullscreen-video-button hide-on-small-only" type="button" data-target="#video-{{ key }}" aria-expanded="false" aria-controls="video">Fullscreen</button> -->
		<?php endif; ?>

	</div>

<?php elseif ($display_title_with_back_option):  ?>

	<?php 
	global $post;
	$parent = wp_get_post_parent_id($post);

	$back_button_link = get_field('back_button_link');
	$back_title = get_field('back_title');
	?>
	<div class="container">

		<div class="singular-header">
			<div class="single-header-post-type parent-title">
				<?php if ($back_title) {echo $back_title; } else { echo get_the_title($parent); } ?>
			</div>
			<a href="<?php if ($back_button_link) {echo $back_button_link['url']; } else {the_permalink($parent); } ?>" class="control control-back inverse icon-arrow-thin-left"><span>Back</span>
	        </a>

		</div>
	<?php echo "<h1 class='center-align'>" . get_the_title() . "</h1>"; ?>
	</div>

<?php elseif (get_post_type() == 'location'):  ?>
<?php else: ?>

	<?php 
	if(get_post_type() == 'page') {
		echo "<div class='page-title'><div class='container'><h1>" . get_the_title() . "</h1></div></div>";
	}
	?>
<?php endif; ?>