<?php
/**
 * The portion of the loop that shows the "gallery" post format.
 *
 * @package WordPress
 * @subpackage Liquorice
 */
?>
<div <?php post_class( 'post-wrapper' ); ?>>

	<?php if ( ! is_single() && get_the_title() != '' ) : ?>
		<h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
	<?php else: ?>
		<h1 class="single-title"><?php the_title(); ?></h1>
	<?php endif; ?>

	<div class="date">
		<small><?php liquorice_posted_on(); ?></small>
	</div><!-- .date -->

	<div class="entry">
		<?php if ( is_single() ) : ?>
			<?php the_content(); ?>
		<?php else : ?>
				<?php
					$pattern = get_shortcode_regex();
					preg_match( "/$pattern/s", get_the_content(), $match );
					$atts = shortcode_parse_atts( $match[3] );
					$images = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : false;

					if ( ! $images ) :
						$images = get_posts( array(
							'post_parent'    => $post->ID,
							'fields'         => 'ids',
							'post_type'      => 'attachment',
							'post_mime_type' => 'image',
							'orderby'        => 'menu_order',
							'order'          => 'ASC',
							'numberposts'    => 999,
							'suppress_filters' => false
						) );
					endif;

					if ( $images ) :
						$total_images = is_countable( $images ) ? count( $images ) : 0;
						$image = array_shift( $images );
						$image_img_tag = wp_get_attachment_image( $image, 'thumbnail' );
				?>
					<div class="gallery-wrap">
						<div class="gallery-thumb">
							<a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
						</div><!-- .gallery-thumb -->
						<p><strong><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, 'liquorice' ),
											'href="' . get_permalink() . '" title="' . esc_attr( sprintf( __( 'Permalink to %s', 'liquorice' ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"',
											number_format_i18n( $total_images ) ); ?>
						</em></strong></p>
					</div><!-- .gallery-wrap-->
					<?php the_excerpt(); ?>
				<?php else: ?>
					<?php the_excerpt(); ?>
				<?php endif; ?>
		<?php endif; ?>
		<?php edit_post_link( __( 'Edit', 'liquorice' ), '<span class="edit-link">', '</span>' ); ?>
	</div><!-- .entry -->

	<?php if ( is_single() ) : ?>
		<div class="post-meta">
			<p class="comments-num"><?php comments_popup_link( __( 'Leave a comment', 'liquorice' ), __( '1 Comment', 'liquorice' ), __( '% Comments', 'liquorice' ) ); ?></p>
			<?php liquorice_posted_in(); ?>
		</div><!-- .meta -->
	<?php endif; ?>

</div><!-- .post-wrapper -->