<?php 
/* Single sermon */
get_header();
if (have_posts()) {
    while (have_posts()) {
        the_post();
        $post_id = get_the_ID();
        $data = harvest_get_sermon_data($post_id);
        harvest_title_bar(get_the_title());
        ?>
		<div class="content_wrap">

			<div class="grid-container content">

				<?php 
        get_template_part('templates/sermon', 'details');
        ?>
				
<?php 
    }
}
//loop
?>
				
<?php 
// other posts in the series
if ($data['series']) {
    $tax_query[] = array('taxonomy' => 'ctc_sermon_series', 'field' => 'slug', 'terms' => $data['series_slug']);
    if ($data['topic']) {
        $tax_query[] = array('taxonomy' => 'ctc_sermon_topic', 'field' => 'slug', 'terms' => $data['topic_slug']);
        $tax_query['relation'] = 'AND';
    function widget($args, $instance)
    {
        global $post;
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Sermon', 'harvest') : $instance['title'], $instance, $this->id_base);
        $add_link = $instance['add_link'];
        $custom_link = $instance['custom_link'];
        $link = $instance['link'];
        $tag = $instance['tag'];
        $use_location = $instance['use_location'];
        $query = array('post_type' => 'ctc_sermon', 'order' => 'DESC', 'orderby' => 'date', 'posts_per_page' => 1);
        // Automatic override using the location as a tag
        if (!empty($tag) && $add_link) {
            $term = get_term_by('slug', $tag, 'ctc_sermon_topic');
            $link = get_term_link(intval($term->term_id), 'ctc_sermon_topic');
        }
        if ($use_location && $post->post_type == 'ctc_location') {
            $tag = $post->post_name;
            if (!$custom_link) {
                $term = get_term_by('slug', $tag, 'ctc_sermon_topic');
                $link = get_term_link(intval($term->term_id), 'ctc_sermon_topic');
            }
        }
        if (!empty($tag)) {
            $query['tax_query'] = array(array('taxonomy' => 'ctc_sermon_topic', 'field' => 'slug', 'terms' => $tag));
        }
        $posts = new WP_Query($query);
        if ($posts->have_posts()) {
            echo $before_widget;
            if ($title) {
                if ($add_link) {
                    $before_title = $before_title . '<a class="ctc-cal-week-title-link" href="' . $link . '">';
                    $after_title = '</a>' . $after_title;
                }
                echo $before_title . $title . $after_title;
            }
            echo '<div id="ctc-recent-sermon" class="ctc-sermon-grid">';
            while ($posts->have_posts()) {
                $posts->the_post();
                $post_id = get_the_ID();
                $data = harvest_get_sermon_data($post_id);
                ?>
			<div class="ctc-sermon">
<?php 
                if ($data['video']) {
                    ?>
	<?php 
                    if ($data['img']) {
                        ?>
		<script>
			// Add lazy loading of videos, using the image as the cover art
			jQuery(document).ready( function($) {
				$( '.ctc-sermon' ).css( {position: 'relative', 'padding-bottom': 'calc(56.25% + 5px)' } );
				
				var vid_src = '<?php 
                        echo strripos($data['video'], 'iframe') ? '<div class="ctc-sermon-video video-container">' . $data['video'] . '</div>' : '<div class="ctc-sermon-video">' . wp_video_shortcode(array('src' => $data['video'])) . '</div>';
                        ?>
';
				
				vid_src = vid_src.replace( 'autoPlay=false', 'autoPlay=true' );
				$( '.play-overlay' ).click( function(){
					$( this ).hide();
					$( '.overlay' ).fadeOut( 'fast', function() {
						$( this ).replaceWith( vid_src );
						$('.video-container').css( 'padding-top', 0 );
						$('.video-container iframe').css( 'border', 'none' );
				
					});
				} );
			})
			
		</script>
		<img class="ctc-sermon-img overlay" src="<?php 
                        echo $data['img'];
                        ?>
"/><div class="play-overlay"><i class="fa fa-play-circle-o fa-5x"></i></div>
	<?php 
                    } else {
                        ?>
		<?php 
                        if (strripos($data['video'], 'iframe')) {
                            ?>
						<div class="ctc-sermon-video video-container"><?php 
                            echo $data['video'];
                            ?>
</div>
		<?php 
                        } else {
                            ?>
						<div class="ctc-sermon-video"><?php 
                            echo wp_video_shortcode(array('src' => $data['video'], 'width' => 768));
                            ?>
</div>
		<?php 
                        }
                        ?>
	<?php 
                    }
                } elseif ($data['img']) {
                    ?>
					<a href="<?php 
                    echo $data['permalink'];
                    ?>
">
						<div class="ctc-grid-details">
							<h3><?php 
                    the_title();
                    ?>
</h3>
						</div>
<?php 
                    if ($data['logo_used']) {
                        ?>
<div class="ctc-sermon-logo accent-background"><?php 
                    }
                    ?>
						<img class="ctc-sermon-img" src="<?php 
                    echo $data['img'];
                    ?>
"/>
<?php 
                    if ($data['logo_used']) {
                        ?>
</div><?php 
                    }
                    ?>
					</a>
<?php 
                } else {
                    ?>
					<a href="<?php 
                    echo $data['permalink'];
                    ?>
">
						<div class="ctc-grid-full accent-background">
							<h1 class="ctc-sermon-name"><?php 
                    the_title();
                    ?>
</h1>
						</div>
					</a>
<?php 
                }
                ?>
				</div> <!-- .ctc-sermon -->
<?php 
                if ($data['video']) {
                    ?>
						<h3><a href="<?php 
                    echo $data['permalink'];
                    ?>
"><?php 
                    the_title();
                    ?>
</a></h3>	
<?php 
                } elseif ($data['audio'] && !$data['video']) {
                    ?>
					<div class="ctc-sermon-audio"><?php 
                    echo wp_audio_shortcode(array('src' => $data['audio']));
                    ?>
</div>
<?php 
                }
                ?>

<?php 
            }
            echo '</div>';
            echo $after_widget;
        }
    }