<?php

global  $theme_version,
        $wpdb;
global  $page, $numpages, $multipage;


get_header();

$page_comment = get_option("gn_page_comment");

if ( comments_open() )
{
	//
	// extract from comments-template.php
	//
	/** @todo Use API instead of SELECTs. */
	if ( $user_ID )
	{
		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date", $post->ID, $user_ID));
	}
	else
	{
		if ( empty($comment_author) )
		{
			$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post->ID));
		}
		else
		{
			$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date", $post->ID, $comment_author, $comment_author_email));
		}
	}
	
	// keep $comments for legacy's sake (remember $table*? ;) )
	$comments = $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
	$wp_query->comment_count = count($wp_query->comments);
	update_comment_cache($comments);
}



echo "\n" . '  <div id="content" class="narrowcolumn">' . "\n\n";

if ( have_posts() )
{
	while ( have_posts() )
	{
		the_post();
		
    echo '    <div id="content-post-' . get_the_ID() . '" ';
    if ( function_exists("post_class") )
    { 
      post_class();
      echo '>' . "\n";
    } 
    else
    {
      echo ' class="post">' . "\n";
    } 
		the_title( '<h1>', '</h1>');
		echo '			<p><br /></p>' . "\n";
		echo '			<div class="entry">' . "\n";
		the_content('<p class="serif">' . __('Read the rest of this page &raquo;', 'german_newspaper') . '</p>');
		wp_link_pages(array('before' => '<p><strong>' . __('Pages:', 'german_newspaper') . '</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));

		$sub_pages = wp_list_pages( array('sort_column' => 'menu_order', 'depth' => '0', 'title_li' => '', 'echo' => '0', 'child_of' => $id) );
		
		if ( $sub_pages <> "" )
		{
			echo '				<p class="meta">' . __('<br />Please see also:', 'german_newspaper') . '</p>' . "\n";
			echo '				<ul>' . "\n";
			echo $sub_pages;
			echo '				</ul>' . "\n";
		}

    if ( $multipage ) 
    {
      $gn_last_page = false;
      if ($page == $numpages) 
      {
        // this is the last page
        $gn_last_page = true;
      }
    } 
    else 
    {
      //  this is only one page - the last page
      $gn_last_page = true;
    }
    
    //  if is last page - print attachments
    if ($gn_last_page)
    {
      /* print out the attachments */
      $args = array(
              'post_type' => 'attachment',
              'numberposts' => null,
              'post_status' => null,
              'post_parent' => $post->ID
              );

      $attachments = get_posts($args);
      if ($attachments) 
      {
        echo "\n" . '<br />' . "\n";
        echo "\n" . '<i>' . __('the attachments to this page:', 'german_newspaper') . '</i>' . "\n";
        echo "\n" . '<br />' . "\n";
    
        foreach ($attachments as $attachment) 
        {
          echo "\n" . '<p>' . "\n";
          echo wp_get_attachment_link( $attachment->ID );
          echo "\n" . '<br />' . apply_filters('the_title', $attachment->post_title) . '<br />' . "\n";
          echo "\n" . '</p>' . "\n";
        }
      }
    }


		echo '			</div>' . '  <!-- DIV entry END -->' . "\n";
		echo '		</div>' .  '  <!-- DIV content-post END -->' ."\n";
	}
}
else
{
	echo '		<p>' . __('Sorry, no posts matched your criteria.') . '</p>' . "\n";
}

edit_post_link(__('Edit this entry.', 'german_newspaper'), '<p>', '</p>');
	
echo '		<p align="center">' . "\n";
posts_nav_link(' - ','&#171; Prev','Next &#187;');
echo '		</p>' . "\n";


if ( comments_open() )
{
  comments_template();
}


echo '	</div>' . '  <!-- DIV content END -->' . "\n\n";




get_footer();



?>