<?php
/*
Template Name: Gallery
*/
?>

<?php get_header(); ?> 

				<div id="theme-url" class="hidden" data-id="<?php echo get_template_directory_uri(); ?>/includes/gallery-overlay.php"></div>
                
                <!--BEGIN #primary .hfeed--> 
                <div id="primary" class="hfeed">
                	
                    <!--BEGIN #page-title-wrap--> 
                	<div id="page-title-wrap">
                    
                    
                    <?php if (have_posts()) : while (have_posts()) : the_post();  ?>
                    	<h1 class="page-title"><?php the_title(); ?><span> / </span></h1>
                        <?php the_content(); ?>
                    <?php endwhile; endif; ?>
                    
                    <!--END #page-title-wrap-->
                    </div>
                
                	<!--BEGIN #gallery-wrap--> 
					<div id="gallery-wrap" class="clearfix">
                
						<?php
						
						//Grab the thumbnail size option
						$thumb_size = get_option('tz_thumbnail_size');
						//Grab the thumbnail orderby option
						$order_by = get_option('tz_gallery_order');
						
						//Set ASC for title
						if($order_by == 'title') {
							$order = 'ASC';
						} else {
							$order = 'DESC';
						}
						
						//Give each size a name
						if($thumb_size == 'large' || $thumb_size == '') {
							$thumb_size = 'large-grid';
						} else if($thumb_size == 'medium') {
							$thumb_size = 'medium-grid';
						} else if($thumb_size == 'small') {
							$thumb_size = 'small-grid';
						}
						
						$args = array(
							'posts_per_page' => -1,
							'post_type' => 'Photo',
							'tax_query' => array(
								array(
									'taxonomy' => 'photo-type',
									'field' => 'id',
									'terms' => get_post_meta($post->ID, 'tz_photo_type', TRUE)
								)
							)
						);

						//Query the database
                        query_posts( $args );
                        ?>
                        
                     	<ul class="<?php echo $thumb_size; ?>">
                        
                    	<?php 
							if (have_posts()) : while (have_posts()) : the_post(); 
							$terms = get_the_terms( get_the_ID(), 'photo-type' );
							
							if($thumb_size == 'large-grid' || $thumb_size == '') {
								$image = get_post_meta(get_the_ID(), 'tz_upload_image_large', TRUE);
							} else if($thumb_size == 'medium-grid') {
								$image = get_post_meta(get_the_ID(), 'tz_upload_image_medium', TRUE);
							} else if($thumb_size == 'small-grid') {
								$image = get_post_meta(get_the_ID(), 'tz_upload_image', TRUE);
							}
						?>
                        
                             <li id="photo-<?php the_ID(); ?>" class="<?php foreach ($terms as $term) { echo 'term-'.$term->term_id.' '; } ?>visible">
								<?php
								if($image != '') : ?>
                                	<a href="<?php the_permalink(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" /></a>
								<?php
								else:
									if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) : ?>
									<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail($thumb_size); ?></a>
									<?php endif; ?>
                                <?php endif; ?> 
                             </li>
                                              
                    	<?php endwhile; endif; ?>
                        
                    	</ul>
                    
                    <!--END #gallery-wrap--> 
                    </div>
                
            <!--END #primary .hfeed-->
            </div>
            
<?php get_footer(); ?>