<?php if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) die ("Please don't do that.");?>

<div class="postSidebar">

	<div class="postMeta postSidebarBlock">
		<div class="byline">
			<strong style="margin-top:0;"><?php _e('By:',THEMENAME);?></strong>
				<a href="<?php echo get_author_posts_url(get_the_author_ID()); ?>"> <?php the_author();?></a>

			<strong><?php _e('Date',THEMENAME);?>:</strong>
				<?php the_date();?><br/>
				<?php the_time();?>
		</div>
		<div class="taxonomies">
		<strong><?php _e('Categories:',THEMENAME);?> </strong><?php
			the_category(', ');
			if (function_exists('the_tags'))
				echo the_tags('<div class="the-tags"><strong>'.__('Post tags',THEMENAME).': </strong>',', ','</div>');?>
		</div>

		<?php
		wp_link_pages(array('before' => '<div class="pageLinks">'.__('Select a page',THEMENAME).':&nbsp;', 'after' => '</div>', 'pagelink' => '<span>%</span>'));
		edit_post_link('<br/><br/>'.__('Edit',THEMENAME));
		?>
	</div>

	<?php // Feedback links
	if (comments_open() || get_comments_number()) {?>
	<div class="commentsLinks postSidebarBlock">
		<strong class="widgetTitle underline"><?php _e('Feedback',THEMENAME);?></strong>
		<ul>
		<li><a href="<?php comments_link(); ?>"> <?php comments_number();?></a></li><?php
		if (comments_open()) { ?>
			<li><a href="<?php the_permalink();?>#respond"><?php _e('Add your own comment',THEMENAME);?></a></li><?php
		}?>
		<?php if (function_exists('post_comments_feed_link')) {?>
		<li><?php post_comments_feed_link('<img src="'.get_bloginfo('template_directory').'/images/feed-icon-16x16.gif" alt="'.__('Comments feed',THEMENAME).'" title="'.__('Comments feed',THEMENAME).'" />&nbsp;'.__('Comments feed',THEMENAME));?></li>
		<?php }?>
		</ul>
	</div><?php
	}

	$theQuery = array(
					  'showposts' => 6,
					  'orderby' => 'post_date',
					  'caller_get_posts' => 1,									// Don't order by stickyness.
					  'post__not_in' => array(intval($post->ID))				// Exclude current post.
					);


	$postTags = get_the_tags($id);
	if (!empty($postTags)) {
		$inTags = array_filter((array)array_map(create_function('$a','if (intval($a->count) > 1)return $a->slug;'),$postTags));
		if (!empty($inTags))
			$theQuery ['tag_slug__in'] = $inTags;
	}

	if (empty($inTags)){
		$postCategories = get_the_category();
		$inCategories = array_map(create_function('$a','return (int) $a->cat_ID;'),$postCategories);
		$theQuery ['cat'] = implode(',',$inCategories);
	}
	wp_reset_query();
	$queryResults = new WP_Query($theQuery);

	if(is_object($queryResults) && $queryResults->have_posts()) {?>
	<div class="relationships postSidebarBlock">
		<strong class="widgetTitle underline"><?php _e('Related posts',THEMENAME);?>:</strong>
		<ul><?php
			foreach($queryResults->posts as $queryPost) {?>
			<li<?php echo $alt++ % 2 ? ' class="even"' : ' class="odd"'?>><a href="<?php echo get_permalink($queryPost->ID);?>"><?php echo $queryPost->post_title;?></a></li><?php
			}?>
			<li<?php echo $alt++ % 2 ? ' class="even"' : ' class="odd"'?>><?php previous_post_link("<strong>".__('Previous',THEMENAME).":</strong> %link","%title",true) ?></li>
			<li<?php echo $alt++ % 2 ? ' class="even"' : ' class="odd"'?>><?php next_post_link("<strong>".__('Next',THEMENAME).":</strong> %link","%title",true) ?></li>
		</ul>
	</div><?php
	}?>

</div>
