<?php global $gp;

// Options
if ( is_singular( 'post' ) ) {
	$gp_post_type = 'post';
	$per_page = $gp['post_related_items_per_page'];
	$image_width = $gp['post_related_items_image']['width'];
	$image_height = $gp['post_related_items_image']['height'];
} elseif ( is_singular( 'gp_portfolio_item' ) ) {
	$gp_post_type = 'gp_portfolio_item';
	$per_page = $gp['portfolio_item_related_items_per_page'];
	$image_width = $gp['portfolio_item_related_items_image']['width'];
	$image_height = $gp['portfolio_item_related_items_image']['height'];
} elseif ( is_page_template( 'review-template.php' ) ) {
	$gp_post_type = 'page';
	$per_page = $gp['review_related_items_per_page'];
	$image_width = $gp['review_related_items_image']['width'];
	$image_height = $gp['review_related_items_image']['height'];
}

// Get Tags
$tags = wp_get_post_tags( get_the_ID() );
$tempQuery = $wp_query;

if ( $tags ) {

	$tag_ids = array();

	foreach ( $tags as $individual_tag ) $tag_ids[] = $individual_tag->term_id;
		
	$args = array(
		'post_status' => 'publish',
		'post_type'           => $gp_post_type,
		'ignore_sticky_posts' => 0,
		'orderby'             => 'rand',
		'order'               => 'desc',
		'paged'               => 1,
		'posts_per_page'      => $per_page,
		'offset'              => 0,
		'tag__in'             => $tag_ids,
		'post__not_in'        => array( get_the_ID() ),
	); 

	$gp_query = new wp_query( $args ); if ( $gp_query->have_posts() ) :
	
	// Column Width
	$column_width = ( 100 - ( ( $per_page * 4 ) - 4 ) ) / $per_page;
		
	?>
	
		<div class="related-wrapper">

			<div class="post-section-header">		
				<h3><?php echo _e( 'Related Items', 'gp_lang' ); ?></h3>
				<span class="post-section-header-line"></span>
			</div>
			
			<div class="inner-loop">
			
				<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>

					<article <?php post_class( 'post-item' ); ?> style="width: <?php echo esc_attr( $column_width ); ?>%;">
	
						<div class="post-thumbnail featured-content<?php if ( ! has_post_thumbnail() ) { ?> no-thumbnail<?php } ?>">
				
							<a href="<?php the_permalink(); ?>">	
							
								<?php if ( has_post_thumbnail() ) { ?>
						
									<?php $image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $image_width, $image_height, true, false, true ); ?>
							
									<?php if ( $gp['retina'] == 'gp-retina' ) {
										$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $image_width * 2, $image_height * 2, true, true, true );
									} else {
										$retina = '';
									} ?>
															
									<img src="<?php echo esc_url( $image[0] ); ?>" data-rel="<?php echo esc_url( $retina ); ?>" width="<?php echo absint( $image[1] ); ?>" height="<?php echo absint( $image[2] ); ?>" alt="<?php if ( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) { echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); } else { echo gp_prefix_hub_title( get_the_ID() ); } ?>" class="wp-post-image" />
							
								<?php } else { ?>
							
									<?php if ( get_post_format() == 'video' ) { 
										$post_format_class = 'fa-video-camera';
									} elseif ( get_post_format() == 'audio' ) {
										$post_format_class = 'fa-music';
									} elseif ( get_post_format() == 'gallery' ) {
										$post_format_class = 'fa-picture-o';
									} elseif ( get_post_format() == 'quote' ) {
										$post_format_class = 'fa-quote-right';
									} else {
										$post_format_class = 'fa-file-o';
									} ?>
										
									<i class="thumbnail-icon fa fa-2x <?php echo sanitize_html_class( $post_format_class ); ?>"></i>
							
									<img src="<?php echo esc_url( gp_images . 'blank.gif' ); ?>" style="width: <?php echo absint( $image_width ); ?>px; height: <?php echo absint( $image_height ); ?>px;" alt="<?php echo gp_prefix_hub_title( get_the_ID() ); ?>" class="wp-post-image" />
							
								<?php } ?>
													
							</a>
								
						</div><!-- END FEATURED IMAGE -->

						<div class="loop-header">
							<h2 class="loop-title"><a href="<?php the_permalink(); ?>" title="<?php echo gp_prefix_hub_title( get_the_ID() ); ?>"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></a></h2>					
						</div>
									
						<div class="entry-meta">							
							<span class="meta-date"><?php the_time( get_option( 'date_format' ) ); ?></span>
						</div>
						
					</article><!-- END POST -->
		
				<?php endwhile; ?>
			
			</div>
				
		</div><!-- END RELATED ITEMS -->

	<?php endif; wp_reset_postdata(); ?>

<?php } ?>