<?php
/*
Template Name: Services-Page
*/

global $post;
$metadata		= get_post_custom($post->ID);
$extraBodyClass	= get_the_title($post->post_parent);
$ancestorPostId	= $post->ID;
if (! empty($post->ancestors)){
	$ancestorPostId	= $post->ancestors[ count($post->ancestors) -1];
}
get_header();
?>
    <div id="content" role="main">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>		
      <?php
                $breadcrumb = '<span class="active">'.get_the_title().'</span>';
                foreach ($post->ancestors as $bc_ancestor_id){
                    $breadcrumb	= '<span><a href="'.htmlentities(get_page_link( $bc_ancestor_id )).'">'.get_the_title($bc_ancestor_id).'</a> &nbsp; &gt;</span> '.$breadcrumb;
                }
                $breadcrumb	= '<span><a href="'.home_url( '/' ).'">Home</a> &nbsp; &gt;</span> '.$breadcrumb.'';
                echo '<div class="breadcrumbs">'.$breadcrumb.'</div>';
            ?>
            <h1><?php the_title(); ?></h1>
            <div class="intro-wrapper">
        	  <?php
              $imageSrc		= wp_get_attachment_image_src( $metadata[thinkplace::$prefix.'image'][0], 'large' );
              $imageMetadata	= wp_get_attachment_metadata( $metadata[thinkplace::$prefix.'image'][0] );

              if ( !empty($metadata[thinkplace::$prefix.'image'][0])) {
                echo "<img src='".$imageSrc[0]."' height='".$imageSrc[2]."' width='".$imageSrc[1]."' alt='".$imageMetadata['image_meta']['title']."' />";
              }
              ?>
				<?php
					if ((isset($metadata[thinkplace::$prefix.'Overview'])) && (! empty($metadata[thinkplace::$prefix.'Overview'][0]) )){
							echo 
				'<div class="intro"><p>'.$metadata[thinkplace::$prefix.'Overview'][0].'</p></div>';
					}
				?>
				</div><!-- .intro-wrapper -->
			<div class="general-content">
			  <?php the_content(); ?>
			</div><!-- .general-content -->
          <?php
          global $post;     // if outside the loop
          if ( is_page() && $post->post_parent ) {
            // This is a subpage
          ?>
          
          <?php
          $pagelist = get_pages('sort_column=menu_order&sort_order=asc');
          $pages = array();
          $page_data = array();
          foreach ($pagelist as $page) {
             $pages[] += $page->ID;
             $page_data[$page->ID] = $page;
          }
          $current = array_search($post->ID, $pages);
          $prevID = $pages[$current-1];
          $nextID = $pages[$current+1];
          ?>

          <div class="service-navigation">
          <?php if (!empty($prevID)) { ?>
          <div class="alignleft">
          <a href="<?php echo get_permalink($prevID); ?>"
            title="<?php echo get_the_title($prevID); ?>">Previous: <?php echo $page_data[$prevID]->post_title; ?></a>
          </div>
          <?php }
          if (!empty($nextID)) { ?>
          <div class="alignright">
          <a href="<?php echo get_permalink($nextID); ?>" 
           title="<?php echo get_the_title($nextID); ?>">Next: <?php echo $page_data[$nextID]->post_title; ?></a>
          </div>
          <?php } ?>
          </div><!-- .navigation -->
          
          <?php
          } else { ?>
            <div id="page-nav">
            <?php
              $mypages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=asc');
              foreach($mypages as $page)
              { 
                $custom_fields = get_post_custom($page->ID);
              ?>
              <h2>
                <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
              </h2>
              <div class="descrip"><?php echo '<p>'.$custom_fields[thinkplace::$prefix.'Nav'][0].'</p>'; ?></div>
              <?php
              }
              ?>
            </div>
          <?php
          } ?>
          <br /
          ><br />
            
			<?php edit_post_link( __( 'Edit', 'thinkplace' ), '<span class="edit-link">', '</span>' ); ?>
            <?php endwhile; ?>
		</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>