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

/**
*	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(); 
}

$page_style = get_post_meta($current_page_id, 'page_style', true);
$page_sidebar = get_post_meta($current_page_id, 'page_sidebar', true);
$caption_style = get_post_meta($current_page_id, 'caption_style', true);

if(empty($caption_style))
{
	$caption_style = 'Title & Description';
}

if(!isset($sidebar_home))
{
	$sidebar_home = '';
}

if(empty($page_sidebar))
{
	$page_sidebar = 'Blog Sidebar';
}
$caption_class = "page_caption";

if(!isset($add_sidebar))
{
	$add_sidebar = TRUE;
}

$sidebar_class = '';

if($page_style == 'Right Sidebar')
{
	$add_sidebar = TRUE;
	$page_class = 'sidebar_content';
}
elseif($page_style == 'Left Sidebar')
{
	$add_sidebar = TRUE;
	$page_class = 'sidebar_content';
	$sidebar_class = 'left_sidebar';
}
else
{
	$page_class = 'inner_wrapper';
}

$pp_title = get_option('pp_blog_title');

if(empty($pp_title))
{
	$pp_title = 'Blog';
}

$additional_style = '';

if(!isset($hide_header) OR !$hide_header)
{
?>		
	
		<div class="wrapper_shadow"></div>
		<div class="page_caption">
			<div class="caption_inner">
				<div class="caption_header">
					<h1 class="cufon"><?php echo $pp_title; ?></h1>
				</div>
			</div>
		</div>
		
		</div>

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

				<!-- Begin main content -->
				<div class="inner_wrapper">
				
				<div class="standard_wrapper small">
					<br class="clear"/>
				
<?php
}
else
{
	$additional_style = 'style="margin-top:10px"';
}
?>

					<?php 
						if($add_sidebar && $page_style == 'Left Sidebar')
						{
					?>
						<div class="sidebar_wrapper <?php echo $sidebar_class; ?>">
						
							<div class="sidebar <?php echo $sidebar_class; ?> <?php echo $sidebar_home; ?>">
							
								<div class="content">
							
									<ul class="sidebar_widget">
									<?php dynamic_sidebar($page_sidebar); ?>
									</ul>
								
								</div>
						
							</div>
							<br class="clear"/>
					
							<div class="sidebar_bottom <?php echo $sidebar_class; ?>"></div>
						</div>
					<?php
						}
					?>
				
					<div class="sidebar_content" <?php echo $additional_style; ?>>
					
<?php

global $more; $more = false; # some wordpress wtf logic

$query_string ="post_type=post&paged=$paged";

$cat_id = get_cat_ID(single_cat_title('', false));
if(!empty($cat_id))
{
	$query_string.= '&cat='.$cat_id;
}

query_posts($query_string);

if (have_posts()) : while (have_posts()) : the_post();

	$image_thumb = '';
	
	$post_img_align = get_post_meta(get_the_ID(), 'post_img_align', true);
	if(empty($post_img_align))
	{
		$post_img_align = 'Top';
	}
	
	$post_img_align = strtolower($post_img_align);
								
	if(has_post_thumbnail(get_the_ID(), 'large'))
	{
	    $image_id = get_post_thumbnail_id(get_the_ID());
	    $image_thumb = wp_get_attachment_image_src($image_id, 'full', true);
	    
	    if($post_img_align == 'top')
	    {
	  		$pp_blog_image_width = get_option('pp_blog_c_width');
			$pp_blog_image_height = get_option('pp_blog_c_height');
		}
		else
		{
			$pp_blog_image_width = get_option('pp_blog_lr_width');
			$pp_blog_image_height = get_option('pp_blog_lr_height');
		}
	}
?>
						
						
						<!-- Begin each blog post -->
						<div class="post_wrapper">
						
							<div class="post_header">
								<h3>
									<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
										<?php the_title(); ?>								
									</a>
								</h3>
								<div class="post_detail">
							<?php
								$pp_blog_meta_sort_data = unserialize(get_option('pp_blog_meta_sort_data'));
						
								if(is_array($pp_blog_meta_sort_data) && !empty($pp_blog_meta_sort_data))
								{
									foreach($pp_blog_meta_sort_data as $meta)
									{
										switch($meta)
										{
											case 1:
												echo the_category(',').'&nbsp;|&nbsp;';
											break;
											case 2:
												echo the_tags('Tags: ',',', '&nbsp;|&nbsp;');
											break;
											case 3:
												echo 'Posted by: '.get_the_author().'&nbsp;|&nbsp;';
											break;
											case 4:
												echo 'Date: '.get_the_time('d M Y').'&nbsp;|&nbsp;';
											break;
											case 5:
												echo comments_number('0 Comment', '1 Comment', '% Comments').'&nbsp;|&nbsp;';
											break;
										}
									}
								}
							?>
								</div>
							</div>
							<div class="circle_comment">
								<div><?php comments_number('0', '1', '%'); ?></div>
							</div>
							<br class="clear"/>
							
							<?php
								$pp_blog_display_image = get_option('pp_blog_display_image');

								if(!empty($pp_blog_display_image) && !empty($image_thumb))
								{
									if($post_img_align == 'top')
	   			 					{
							?>
									<br/>
									<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
										<img src="<?php echo get_stylesheet_directory_uri(); ?>/timthumb.php?src=<?php echo $image_thumb[0]; ?>&amp;h=<?php echo $pp_blog_image_height; ?>&amp;w=<?php echo $pp_blog_image_width; ?>&amp;zc=1" alt="" class="img_nofade frame"/>
									</a>
									<br class="clear"/><br/>
							<?php
									}
									else
									{
							?>
									<br/>
									<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
										<img src="<?php echo get_stylesheet_directory_uri(); ?>/timthumb.php?src=<?php echo $image_thumb[0]; ?>&amp;h=<?php echo $pp_blog_image_height; ?>&amp;w=<?php echo $pp_blog_image_width; ?>&amp;zc=1" alt="" class="img_nofade frame align<?php echo $post_img_align; ?>"/>
									</a>
							<?php
									}
								}
							?>
							
							<?php echo get_the_content_with_formatting(); ?>
							
							<br class="clear"/>
							
							<hr/>
						
							<br class="clear"/>
						</div>
						<!-- End each blog post -->
						



<?php endwhile; endif; ?>

						<div class="pagination"><p><?php posts_nav_link(' '); ?></p></div>
						
					</div>
					
					<?php
						if($add_sidebar && $page_style == 'Right Sidebar')
						{
					?>
						<div class="sidebar_wrapper <?php echo $sidebar_class; ?>">
						
							<div class="sidebar_top <?php echo $sidebar_class; ?>"></div>
						
							<div class="sidebar <?php echo $sidebar_class; ?> <?php echo $sidebar_home; ?>">
							
								<div class="content">
							
									<ul class="sidebar_widget">
									<?php dynamic_sidebar($page_sidebar); ?>
									</ul>
								
								</div>
						
							</div>
							<br class="clear"/>
					
							<div class="sidebar_bottom <?php echo $sidebar_class; ?>"></div>
						</div>
					<?php
						}
					?>
			
<?php
$pp_homepage_hide_right_sidebar = get_option('pp_homepage_hide_right_sidebar');

if(!isset($hide_header) OR !$hide_header)
{
?>
			</div>
			<!-- End main content -->
				
			<br class="clear"/>
				
			</div>
				

<?php get_footer(); ?>

<?php
}
?>