<?php
/**
 * Template Name: Services
 *
 * A custom page template that draws in People posts.
 * This template is based on the page.php template
 *
 * The "Template Name:" bit above allows this to be selectable
 * from a dropdown menu on the edit page screen.
 *
 * @package WordPress
 * @subpackage ThinkPlace
 * @since ThinkPlace 1.0
 */
global $post;
$metadata			= get_post_custom($post->ID);
$extraBodyClass	= get_the_title($post->post_parent);
$people				= thinkplace::getPeopleList();
$selectedPersonId	= (isset($_REQUEST['person_id']))	? $_REQUEST['person_id']	: 0;//key($people);
$pageId				= $post->ID;
$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	= '<a href="'.htmlentities(get_page_link( $bc_ancestor_id )).'">'.get_the_title($bc_ancestor_id).'</a> &gt; '.$breadcrumb;
                }
                $breadcrumb	= '<a href="'.home_url( '/' ).'">Home</a> &gt; '.$breadcrumb;
                echo '<div class="breadcrumbs">'.$breadcrumb.'</div>';
            ?>
			<?php edit_post_link( __( 'Edit', 'thinkplace' ), '<span class="edit-link">', '</span>' ); ?>
<?php
// And now for the people stuff

// If a person has been selected...
if (! empty($selectedPersonId)){

	$args	= array(
				'numberposts'	=> 1,
				'post_type' 	=> 'people',
				'p'				=> $selectedPersonId
				);

	$peopleQuery = new WP_Query($args);			
?>
<?php if ( $peopleQuery->have_posts() ) while ( $peopleQuery->have_posts() ) : $peopleQuery->the_post(); ?>
				  <div class="general-content">
                  <?php $personMetadata	= get_post_custom(get_the_ID()); ?>
					<h2><?php the_title();?> <span class="position"><?php echo $personMetadata[thinkplace::$prefix.'position'][0]; ?></span></h2>
					<?php
						
						$imageSrc		= wp_get_attachment_image_src( $personMetadata[thinkplace::$prefix.'image'][0], 'medium' );
						$imageMetadata	= wp_get_attachment_metadata( $personMetadata[thinkplace::$prefix.'image'][0] );
						
						echo "<img src='".$imageSrc[0]."' height='".$imageSrc[2]."' width='".$imageSrc[1]."' alt='".$imageMetadata['image_meta']['title']."' />";
						
				 ?>
					<?php the_content(); ?>
					</div>
			

<?php endwhile;

} else { ?>

            <h1><?php the_title(); ?></h1>
				<div class="intro">
					<?php
                        if ((isset($metadata[thinkplace::$prefix.'Overview'])) && (! empty($metadata[thinkplace::$prefix.'Overview'][0]) )){
                                echo '<p>'.$metadata[thinkplace::$prefix.'Overview'][0].'</p>';
                        }
                    ?>
                </div>
                <?php the_content(); ?>
                
	<?php // Show all the faces
	$facesPerRow	= 4;
	$a				= 0;
	foreach ($people as $person){
		$personMetadata	= get_post_custom($person->ID);
		
		$imageSrc		= wp_get_attachment_image_src( $personMetadata[thinkplace::$prefix.'image'][0], 'thumbnail' );
		$imageMetadata	= wp_get_attachment_metadata( $personMetadata[thinkplace::$prefix.'image'][0] );
		
		echo '<div class="thumbnail">
			
			<a href="'.htmlentities(add_query_arg(array('person_id'=>$person->ID), get_page_link( $pageId ) )).'">
			'."<img src='".$imageSrc[0]."' height='".$imageSrc[2]."' width='".$imageSrc[1]."' alt='".$imageMetadata['image_meta']['title']."' />".'
			
			<span class="caption">
			'.$person->post_title.'
			</span>
			</a>
			
		</div>';
		$a++;
	}
}
?>
	
			
<?php endwhile; ?>

		</div><!-- #content -->
		<div id="sidebar">
            <?php if ( is_page_template('about.php') ); ?>
				<div id="sub-nav">
                <ul>
                <?php
                    foreach ($people as $person){
                        $selected		= false;
                        if ($selectedPersonId == $person->ID){
                            $selected		= true;
                        }
                        echo '<li'.(($selected) ? ' class="selected"' : '').'>
                            <a href="'.htmlentities(add_query_arg(array('person_id'=>$person->ID), get_page_link( $pageId ) )).'">'.$person->post_title.'</a>
                        </li>';
                    }
                ?>
            	</ul>  
                </div>       
                    
                <?php
/**
*
*					if($post->post_parent)
*					$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&exclude=27,29,31,33");
*					if ($children) { ?>
*
*					<div id="sub-nav">
*                        <ul>
*                        <?php echo $children; ?>
*                        </ul>
*					</div><!--sub-nav-->
*                <?php } 
*/		?>
	
            
            <div id="tag-cloud">
                <h2>Tag cloud</h2>
                <?php wp_tag_cloud('smallest=8&largest=14&number=50'); ?>
            </div><!--tag-cloud-->
        </div><!--sidebar-->
	</div> <!--content-container-->
<?php get_footer(); ?>