<?php
/**
 * The template to display single post
 *
 * @package TOURIMO
 * @since TOURIMO 1.0
 */

// Full post loading
$full_post_loading        = tourimo_get_value_gp( 'action' ) == 'full_post_loading';

// Prev post loading
$prev_post_loading        = tourimo_get_value_gp( 'action' ) == 'prev_post_loading';
$prev_post_loading_type   = tourimo_get_theme_option( 'posts_navigation_scroll_which_block' );

// Position of the related posts
$tourimo_related_position = tourimo_get_theme_option( 'related_position' );

// Type of the prev/next posts navigation
$tourimo_posts_navigation = tourimo_get_theme_option( 'posts_navigation' );
$tourimo_prev_post        = false;

// Rewrite style of the single post if current post loading via AJAX and featured image and title is not in the content
if ( ( $full_post_loading 
		|| 
		( $prev_post_loading && 'article' == $prev_post_loading_type )
	) 
	&& 
	! in_array( tourimo_get_theme_option( 'single_style' ), array( 'style-6' ) )
) {
	tourimo_storage_set_array( 'options_meta', 'single_style', 'style-6' );
}

get_header();

while ( have_posts() ) {

	the_post();

	// Type of the prev/next posts navigation
	if ( 'scroll' == $tourimo_posts_navigation ) {
		$tourimo_prev_post = get_previous_post( true );         // Get post from same category
		if ( ! $tourimo_prev_post ) {
			$tourimo_prev_post = get_previous_post( false );    // Get post from any category
			if ( ! $tourimo_prev_post ) {
				$tourimo_posts_navigation = 'links';
			}
		}
	}

	// Override some theme options to display featured image, title and post meta in the dynamic loaded posts
	if ( $full_post_loading || ( $prev_post_loading && $tourimo_prev_post ) ) {
		tourimo_sc_layouts_showed( 'featured', false );
		tourimo_sc_layouts_showed( 'title', false );
		tourimo_sc_layouts_showed( 'postmeta', false );
	}

	// If related posts should be inside the content
	if ( strpos( $tourimo_related_position, 'inside' ) === 0 ) {
		ob_start();
	}

	// Display post's content
	get_template_part( apply_filters( 'tourimo_filter_get_template_part', 'templates/content', 'single-' . tourimo_get_theme_option( 'single_style' ) ), 'single-' . tourimo_get_theme_option( 'single_style' ) );

	// If related posts should be inside the content
	if ( strpos( $tourimo_related_position, 'inside' ) === 0 ) {
		$tourimo_content = ob_get_contents();
		ob_end_clean();

		ob_start();
		do_action( 'tourimo_action_related_posts' );
		$tourimo_related_content = ob_get_contents();
		ob_end_clean();

		$tourimo_related_position_inside = max( 0, min( 9, tourimo_get_theme_option( 'related_position_inside' ) ) );
		if ( 0 == $tourimo_related_position_inside ) {
			$tourimo_related_position_inside = mt_rand( 1, 9 );
		}

		$tourimo_p_number = 0;
		$tourimo_related_inserted = false;
		for ( $i = 0; $i < strlen( $tourimo_content ) - 3; $i++ ) {
			if ( '<' == $tourimo_content[ $i ] && 'p' == $tourimo_content[ $i + 1 ] && in_array( $tourimo_content[ $i + 2 ], array( '>', ' ' ) ) ) {
				$tourimo_p_number++;
				if ( $tourimo_related_position_inside == $tourimo_p_number ) {
					$tourimo_related_inserted = true;
					$tourimo_content = ( $i > 0 ? substr( $tourimo_content, 0, $i ) : '' )
										. $tourimo_related_content
										. substr( $tourimo_content, $i );
				}
			}
		}
		if ( ! $tourimo_related_inserted ) {
			$tourimo_content .= $tourimo_related_content;
		}

		tourimo_show_layout( $tourimo_content );
	}

	// Comments
	do_action( 'tourimo_action_before_comments' );
	comments_template();
	do_action( 'tourimo_action_after_comments' );

	// Related posts
	if ( 'below_content' == $tourimo_related_position
		&& ( 'scroll' != $tourimo_posts_navigation || tourimo_get_theme_option( 'posts_navigation_scroll_hide_related' ) == 0 )
		&& ( ! $full_post_loading || tourimo_get_theme_option( 'open_full_post_hide_related' ) == 0 )
	) {
		do_action( 'tourimo_action_related_posts' );
	}

	// Post navigation: type 'scroll'
	if ( 'scroll' == $tourimo_posts_navigation && ! $full_post_loading ) {
		?>
		<div class="nav-links-single-scroll"
			data-post-id="<?php echo esc_attr( get_the_ID( $tourimo_prev_post ) ); ?>"
			data-post-link="<?php echo esc_attr( get_permalink( $tourimo_prev_post ) ); ?>"
			data-post-title="<?php the_title_attribute( array( 'post' => $tourimo_prev_post ) ); ?>">
		</div>
		<?php
	}
}

get_footer();
