<?php
/**
 * Template Name: Blog Right Sidebar
 * The main template file for display blog page.
 *
 * @package WordPress
*/

get_template_part("lib/fallback.home.lib");

/**
*	Get Current page object
**/
$page = get_page($post->ID);

/**
*	Get current page id
**/

if(!isset($pp_homepage) && !isset($current_page_id) && isset($page->ID))
{
    $current_page_id = $page->ID;
}
else
{
	$current_page_id = $pp_homepage;
}

if(!isset($hide_header) OR !$hide_header)
{
	get_header(); 
}

//Get page Sidebar
$page_sidebar = get_post_meta($current_page_id, 'page_sidebar', true);
if(empty($page_sidebar))
{
	$page_sidebar = 'Blog Sidebar';
}

//If display on homepage then disable header
if(!isset($hide_header) OR !$hide_header)
{
?>
		
</div>

<?php
	if(has_post_thumbnail($current_page_id, 'full'))
    {
        $image_id = get_post_thumbnail_id($current_page_id); 
        $image_thumb = wp_get_attachment_image_src($image_id, 'full', true);
        $pp_page_bg = $image_thumb[0];
?>
<style>
.page_caption { background: #f9f9f9 url('<?php echo $pp_page_bg; ?>') center center no-repeat; background-size: cover; }
</style>
<?php
	}
?>

<?php
if(!is_front_page())
{
?>
<div class="page_caption">
	<div class="caption_inner">
		<div class="caption_header">
			<h1 <?php if(!empty($pp_page_bg)) { ?>class="withbg"<?php } ?>><span><?php the_title(); ?></span></h1>
			<?php
			$page_description = get_post_meta($current_page_id, 'page_description', true);
			
			if(!empty($page_description))
			{
			?>
			    <div class="page_description<?php if(!empty($pp_page_bg)) { ?> withbg<?php } ?>"><?php echo $page_description; ?></div>
			<?php
			}
			?>
		</div>
	</div>
	
	<hr class="hr_page_caption"/>
</div>
<br class="clear"/>

<!-- Begin content -->
<div id="content_wrapper">

    <div class="inner">
    
    	<!-- Begin main content -->
    	<div class="inner_wrapper">
    	
    		<div class="standard_wrapper">
<?php
}

} //End is not front page
?>
				
				<div class="sidebar_content">
					
				<?php
				$query_string ="post_type=post&paged=$paged";
				
				query_posts($query_string);
				$num_of_posts = $wp_query->post_count;
				$cur_post = 0;
				
				if (have_posts()) : while (have_posts()) : the_post();
					
					$cur_post++;
					$image_thumb = '';
												
					if(has_post_thumbnail(get_the_ID(), 'blog'))
					{
					    $image_id = get_post_thumbnail_id(get_the_ID());
					    $image_thumb = wp_get_attachment_image_src($image_id, 'blog', true);
					}
				?>	
						
					<!-- Begin each blog post -->
					<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> class="post_wrapper">
					
						<span class="post_color_date"><?php echo get_the_time('F d, Y'); ?></span>
					
						<h3 class="post_heading">
							<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
					    </h3>
					
						<?php
					        if(isset($image_thumb[0]))
					        {
					    ?>	
					    <a href="<?php the_permalink(); ?>">
					    	<div class="post_img_wrapper">
						    	<img src="<?php echo $image_thumb[0]; ?>" alt="" class="post_featured"/>
						    	<div class="post_img_overlay">
						    		<div class="overlay_icon_circle">
	        							<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon_link.png" alt="" class=""/>
	        						</div>
	        					</div>
						    </div>
					    </a>
					    
					    <?php
					    	}
					    ?>
					    
					    <br class="clear"/>
					    
					    <div class="post_header_wrapper">
					    	<div class="post_header">
					    		<div class="post_detail">
							    	<div class="post_detail_item">
										<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/post_icon_user.png" alt="" class="middle"/>
										<?php echo get_the_author(); ?>
							    	</div>
							    	<div class="post_detail_item">
							    		<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/post_icon_comment.png" alt="" class="middle"/>
										<a href="<?php the_permalink(); ?>"><?php comments_number(__( 'Leave a Comment', THEMEDOMAIN ), __( 'Comment: 1', THEMEDOMAIN ), __( 'Comment: %', THEMEDOMAIN )); ?></a>
							    	</div>
							    	<div class="post_detail_item">
							    		<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/post_icon_tags.png" alt="" class="middle"/>
										<?php the_tags(); ?>
							    	</div>
								</div>
					    	</div>
					    </div>
					    
					    <div class="post_excerpt">
					    <?php
					    	$pp_blog_display_full = get_option('pp_blog_display_full');
					    	
					    	if(!empty($pp_blog_display_full))
					    	{
					    		the_content();
					    	}
					    	else
					    	{
					    		the_excerpt();
					    ?>
					    		<br/>
					    		<a class="button" href="<?php the_permalink(); ?>"><?php echo _e( 'Read The Rest', THEMEDOMAIN ); ?> →</a>
					    
					    <?php
					    	}
						?>
						    
					    </div>
					    
					    <br class="clear"/>
					
					</div>
					
					<div class="thickline"></div>
					<br class="clear"/>
					
					<!-- End each blog post -->

					<?php endwhile; endif; ?>
					
					<?php
					if (function_exists("wpapi_pagination")) 
					{
			 			wpapi_pagination($wp_query->max_num_pages);
			 		}
			 		else
					{
					?>
						<div class="pagination"><p><?php posts_nav_link(' '); ?></p></div>
					<?php
					}
					?>
					</div>
					
					<div class="sidebar_wrapper">
					
					    <div class="sidebar_top"></div>
					
					    <div class="sidebar">
					    
					    	<div class="content">
					    
					    		<ul class="sidebar_widget">
					    		<?php dynamic_sidebar($page_sidebar); ?>
					    		</ul>
					    	
					    	</div>
					
					    </div>
					    <br class="clear"/>
					
					    <div class="sidebar_bottom"></div>
					</div>
			
			<?php
			if(!isset($hide_header) OR !$hide_header)
			{
			?>
				</div>
				<!-- End main content -->
					
				<br class="clear"/>
					
				</div>
				
			</div>	

			<?php get_footer(); ?>
		
			<?php
			}
			else
			{
			?>
			<br class="clear"/>
			<?php
			}
			?>