<?php
global $kippis_is_smartphone;
get_header();
if (!$kippis_is_smartphone) get_sidebar('left');
?>
      <div id="content">
        <nav id="nav-single">
          <span class="nav-previous"><?php previous_image_link(false,__('&larr; Previous','kippis')); ?></span>
          <span class="nav-next"><?php next_image_link(false,__('Next &rarr;','kippis')); ?></span>
        </nav>
        <article>
          <?php the_post(); ?>
          <div id="post-<?php the_ID(); ?>" <?php post_class('article'); ?>>
            <header class="entry-header">
              <h1 class="entry-title"><?php the_title(); ?></h1>
              <div class="entry-meta">
                <?php
                  $metadata = wp_get_attachment_metadata();
                  printf(__('<span class="meta-prep meta-prep-entry-date">Published </span> <span class="entry-date"><abbr class="published" title="%1$s">%2$s</abbr></span> at <a href="%3$s" title="Link to full-size image">%4$s &times; %5$s</a> in <a href="%6$s" title="Return to %7$s" rel="gallery">%7$s</a>','kippis')
                    ,esc_attr(get_the_time())
                    ,get_the_date()
                    ,esc_url(wp_get_attachment_url())
                    ,$metadata['width']
                    ,$metadata['height']
                    ,esc_url(get_permalink($post->post_parent))
                    ,get_the_title($post->post_parent)
                  );
                ?>
                <?php edit_post_link(__('Edit','kippis'),'<span class="edit-link">','</span>'); ?>
              </div>
            </header>
            <div class="entry-content">
              <div class="entry-attachment">
                <div class="attachment">
                <?php
                  $attachments = array_values(get_children(array('post_parent'    => $post->post_parent
                                                                ,'post_status'    => 'inherit'
                                                                ,'post_type'      => 'attachment'
                                                                ,'post_mime_type' => 'image'
                                                                ,'order'          => 'ASC'
                                                                ,'orderby'        => 'menu_order ID'
                                                                )));
                  foreach ($attachments as $k => $attachment) {
                    if ($attachment->ID == $post->ID) break;
                  }
                  $k++;
                  if (count($attachments) > 1) {
                    if (isset($attachments[$k])) $next_attachment_url = get_attachment_link($attachments[$k]->ID);
                    else                         $next_attachment_url = get_attachment_link($attachments[ 0]->ID);
                  }
                  else $next_attachment_url = wp_get_attachment_url();
                  ?>
                  <a href="<?php echo esc_url($next_attachment_url); ?>" title="<?php echo esc_attr(get_the_title()); ?>" rel="attachment"><?php
                  if ($kippis_is_smartphone) $attachment_size = 'medium';
                  else                         $attachment_size = 'large';
                  echo wp_get_attachment_image($post->ID,$attachment_size);
                  ?></a>
                  <?php
                  if (!empty($post->post_excerpt)) {
                    echo '<div class="entry-caption">';
                    the_excerpt();
                    echo '</div>';
                  }
                ?>
                </div>
              </div>
              <div class="entry-description">
              <?php
                the_content();
                wp_link_pages(array('before' => '<div class="page-link"><span>' . __('Pages:','kippis') . '</span>','after' => '</div>'));
              ?>
              </div>
              <div class="exif-data">
                <table>
                <?php
                  $exifdata = kippis_get_exif_data(get_attached_file($post->ID));
                  if (!empty($metadata['image_meta']['focal_length' ])) $metadata['image_meta']['focal_length' ] = floor($metadata['image_meta']['focal_length' ] * 1000) / 1000;
                  if (!empty($metadata['image_meta']['shutter_speed'])) $metadata['image_meta']['shutter_speed'] = floor($metadata['image_meta']['shutter_speed'] * 1000) / 1000;
                  if (!empty($exifdata              ['Camera'       ])) echo '<tr><td>' . __('Camera'       ,'kippis') . '</td><td>' . $exifdata              ['Camera'             ] . '</td></tr>';
                  if (!empty($exifdata              ['Software'     ])) echo '<tr><td>' . __('Software'     ,'kippis') . '</td><td>' . $exifdata              ['Software'           ] . '</td></tr>';
                //if (!empty($exifdata              ['DateTime'     ])) echo '<tr><td>' . __('DateTime'     ,'kippis') . '</td><td>' . $exifdata              ['DateTime'           ] . '</td></tr>';
                                                                        echo '<tr><td>' . __('Image size'   ,'kippis') . '</td><td>' . $metadata['width'] . 'x' . $metadata['height'] . '</td></tr>';
                  if (!empty($exifdata              ['FileSize'     ])) echo '<tr><td>' . __('File size'    ,'kippis') . '</td><td>' . $exifdata              ['FileSize'           ] . '</td></tr>';
                  if (!empty($exifdata              ['MimeType'     ])) echo '<tr><td>' . __('Mime type'    ,'kippis') . '</td><td>' . $exifdata              ['MimeType'           ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['title'        ])) echo '<tr><td>' . __('Title'        ,'kippis') . '</td><td>' . $metadata['image_meta']['title'              ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['caption'      ])) echo '<tr><td>' . __('Caption'      ,'kippis') . '</td><td>' . $metadata['image_meta']['caption'            ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['aperture'     ])) echo '<tr><td>' . __('Aperture'     ,'kippis') . '</td><td>' . $metadata['image_meta']['aperture'           ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['focal_length' ])) echo '<tr><td>' . __('Focal length' ,'kippis') . '</td><td>' . $metadata['image_meta']['focal_length'       ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['shutter_speed'])) echo '<tr><td>' . __('Shutter speed','kippis') . '</td><td>' . $metadata['image_meta']['shutter_speed'      ] . '</td></tr>';
                //if (!empty($exifdata              ['ExposureTime' ])) echo '<tr><td>' . __('Exposure time','kippis') . '</td><td>' . $exifdata              ['ExposureTime'       ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['credit'       ])) echo '<tr><td>' . __('Credit'       ,'kippis') . '</td><td>' . $metadata['image_meta']['credit'             ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['copyright'    ])) echo '<tr><td>' . __('Copyright'    ,'kippis') . '</td><td>' . $metadata['image_meta']['copyright'          ] . '</td></tr>';
                  if (!empty($metadata['image_meta']['iso'          ])) echo '<tr><td>' . __('Iso'          ,'kippis') . '</td><td>' . $metadata['image_meta']['iso'                ] . '</td></tr>';
                  if (!empty($exifdata              ['Flash'        ])) echo '<tr><td>' . __('Flash'        ,'kippis') . '</td><td>' . $exifdata              ['Flash'              ] . '</td></tr>';
                ?>
                </table>
              </div>
            </div>
          </div>
          <?php comments_template(); ?>
        </article>
      </div><!-- content -->

<?php
if ($kippis_is_smartphone) get_sidebar('left');
get_sidebar('right');
get_footer();

function kippis_get_exif_data($fn)
{
  $exif = @exif_read_data($fn);
  if ($exif === false) return array('error' => 'Error retrieving EXIF data');
  if (!empty($exif['FileSize'])) $exif['FileSize'] = floor(($exif['FileSize'] / 1000)) . ' KB';
  if (!empty($exif['Make'])) $exif['Camera'] = $exif['Make'] . ' ' . $exif['Model'];
  else                       $exif['Camera'] = '';
  switch ($exif['Flash']) {
    case 0x0000: $exif['Flash'] = ''; /*'Flash did not fire';*/                                                            break;
    case 0x0001: $exif['Flash'] = 'Flash fired';                                                                           break;
    case 0x0005: $exif['Flash'] = 'Strobe return light not detected';                                                      break;
    case 0x0007: $exif['Flash'] = 'Strobe return light detected';                                                          break;
    case 0x0009: $exif['Flash'] = 'Flash fired, compulsory flash mode';                                                    break;
    case 0x000D: $exif['Flash'] = 'Flash fired, compulsory flash mode, return light not detected';                         break;
    case 0x000F: $exif['Flash'] = 'Flash fired, compulsory flash mode, return light detected';                             break;
    case 0x0010: $exif['Flash'] = 'Flash did not fire, compulsory flash mode';                                             break;
    case 0x0018: $exif['Flash'] = 'Flash did not fire, auto mode';                                                         break;
    case 0x0019: $exif['Flash'] = 'Flash fired, auto mode';                                                                break;
    case 0x001D: $exif['Flash'] = 'Flash fired, auto mode, return light not detected';                                     break;
    case 0x001F: $exif['Flash'] = 'Flash fired, auto mode, return light detected';                                         break;
    case 0x0020: $exif['Flash'] = 'No flash function';                                                                     break;
    case 0x0041: $exif['Flash'] = 'Flash fired, red-eye reduction mode';                                                   break;
    case 0x0045: $exif['Flash'] = 'Flash fired, red-eye reduction mode, return light not detected';                        break;
    case 0x0047: $exif['Flash'] = 'Flash fired, red-eye reduction mode, return light detected';                            break;
    case 0x0049: $exif['Flash'] = 'Flash fired, compulsory flash mode, red-eye reduction mode';                            break;
    case 0x004D: $exif['Flash'] = 'Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected'; break;
    case 0x004F: $exif['Flash'] = 'Flash fired, compulsory flash mode, red-eye reduction mode, return light detected';     break;
    case 0x0059: $exif['Flash'] = 'Flash fired, auto mode, red-eye reduction mode';                                        break;
    case 0x005D: $exif['Flash'] = 'Flash fired, auto mode, return light not detected, red-eye reduction mode';             break;
    case 0x005F: $exif['Flash'] = 'Flash fired, auto mode, return light detected, red-eye reduction mode';                 break;
  }
  return $exif;
}

