<?php 
$pageId = $post->ID;
$_SESSION['Wecare_page_id'] = $pageId;

$get_meta = get_post_custom($post->ID);
$weblusive_sidebar_pos = $get_meta["_weblusive_sidebar_pos"][0];
get_template_part( 'library/includes/page-head' ); 

$portfolio_type = get_post_meta($post->ID, "_portfolio_type", $single = false);
$paginationEnabled = (isset($portfolio_type) && !(empty ($portfolio_type))) ? $portfolio_type[0] : 0;
$page_template_name = get_post_meta($post->ID,'_wp_page_template',true); 


	$itemsize = 'column-three';	
	$itemlayout = 'column4';	
	$colnumber = 3;
	$thumbsize = 'portfolio-3-col';
	// Check which layout was selected
	switch($page_template_name)
	{
		case 'portfolio-template-3columns.php':
			$itemsize = 'column3';	
			$thumbsize = 'portfolio-3-col';
			$itemlayout = 'column4';
			$colnumber = 3;			
		break;
		
		case 'portfolio-template-4columns.php':
			$itemsize = 'column-four';	
			$thumbsize = 'portfolio-4-col';
			$itemlayout = 'column3';	
			$colnumber = 4;
		break;		
	}


if( get_post_meta($post->ID, "_page_portfolio_num_items_page", $single = true) != '' && $paginationEnabled ) 
{ 
	$items_per_page = get_post_meta($post->ID, "_page_portfolio_num_items_page", $single = false);
} 
else 
{ // else don't paginate
	$items_per_page[0] = 777;
}
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => $items_per_page[0])); 
?>

<div class="wrapper ">
	<div id="container">
		<?php echo getPageContent($pageId); ?>
		<ul id="filterOptions" class="filter-items<?php if (!$paginationEnabled):?><?php echo ' non-paginated' ?><?php endif?>">
			<?php if ($paginationEnabled):?>
				<li><a href="<?php echo get_page_link($pageId) ?>" class="active"><?php _e('Show All', 'Wecare')?></a></li>
				<?php 
					$cats = get_post_meta($post->ID, "_page_portfolio_cat", $single = true);
					$MyWalker = new PortfolioWalker2();
					$args = array( 'taxonomy' => 'portfolio_category', 'hide_empty' => '0', 'include' => $cats, 'title_li'=> '', 'walker' => $MyWalker, 'show_count' => '1');
					$categories = wp_list_categories ($args);
				?>
			<?php else: ?>
				<li><a href="#" class="active" data-filter="*"><?php _e('Show All', 'Wecare')?></a></li>
				<?php 
					$cats = get_post_meta($post->ID, "_page_portfolio_cat", $single = true);                                                 
					$MyWalker = new PortfolioWalker();
					$args = array( 'taxonomy' => 'portfolio_category', 'hide_empty' => '0', 'include' => $cats, 'title_li'=> '', 'walker' => $MyWalker, 'show_count' => '1');
					$categories = wp_list_categories ($args);
				?>
			<?php endif ?>
		</ul>
		<div  id="ourHolder" class=" projects-container <?php echo $itemsize?>">
			<?php if( $cats == '' ): ?>
				<p><?php _e('No categories selected. To fix this, please login to your WP Admin area and set
					the categories you want to show by editing this page and setting one or more categories 
					in the multi checkbox field "Portfolio Categories".', 'Wecare')?>
				</p>
			<?php else: ?>		
				<?php 
					// If the user hasn't set a number of items per page, then use JavaScript filtering
					if( $items_per_page == 777 ) : endif; 
					$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
					//  query the posts in selected terms
					$portfolio_posts_to_query = get_objects_in_term( explode( ",", $cats ), 'portfolio_category');
				 ?>
				 <?php if (!empty($portfolio_posts_to_query)):			
					$wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', 'post__in' => $portfolio_posts_to_query, 'paged' => $paged, 'showposts' => $items_per_page[0] ) ); 
					
					if ($wp_query->have_posts()):  ?>
					<?php while ($wp_query->have_posts()) : 							
						$wp_query->the_post();
						$custom = get_post_custom($post->ID);
							 
						// Get the portfolio item categories
						$cats = wp_get_object_terms($post->ID, 'portfolio_category');
											   
												
						if ($cats):
							$cat_slugs = '';
							foreach( $cats as $cat ) {$cat_slugs .= $cat->slug . " ";}
						endif;
						?>
			
						<?php $link = ''; $thumbnail = get_the_post_thumbnail($post->ID, $thumbsize); ?>
						
						<div class="item <?php echo $cat_slugs; ?>">
                                                    <div class="view view-two">
                                                        <?php if (!empty($thumbnail)): ?>
								<?php the_post_thumbnail($thumbsize, array('class' => 'cover')); ?>
							<?php else :?>
								 <img src="<?php echo get_template_directory_uri()?>/images/picture.jpg" alt="<?php _e ('No preview image', 'Wecare') ?>" />
							<?php endif?>
                                                        <div class="mask">
                                                             <?php if( !empty ( $custom['_portfolio_video'][0] ) ) : $link = $custom['_portfolio_video'][0]; ?>
									<a href="<?php echo $link ?>" class="button btn-icon fancybox-video fancybox.iframe"  data-fancybox-group="gallery1" title="<?php the_title() ?>">
										<i class="fa fa-film fa-3x"></i>
									</a>
								<?php elseif( isset($custom['_portfolio_link'][0]) && $custom['_portfolio_link'][0] != '' ) : ?>
									<a href="<?php echo $custom['_portfolio_link'][0] ?>" class="button btn-icon" data-fancybox-group="gallery1"  title="<?php the_title() ?>">
										<i class="fa fa-external-link fa-3x"></i>
									</a>
								<?php elseif(  isset( $custom['_portfolio_no_lightbox'][0] )  &&  $custom['_portfolio_no_lightbox'][0] !='' ) : $link = get_permalink(get_the_ID());  ?>
									<a href="<?php echo $link; ?>" class="button btn-icon" data-fancybox-group="gallery1" title="<?php the_title() ?>">
										<i class="fa fa-location-arrow fa-3x"></i>
									</a>
								<?php else : ?>
									<?php  
										$full_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full', false); 
										$link = $full_image[0];
									?>
									<a href="<?php echo $link ?>" class="button btn-icon"  data-fancybox-group="gallery1" title="<?php the_title() ?>">
										<i class="fa fa-plus-circle fa-3x"></i>
									</a>
								<?php   endif; ?>        
                                                        </div>
                                                    </div>
								
							<div class="port-span">
								<h3><?php the_title() ?></h3>                                    
								<span><?php echo $cat_slugs; ?></span>
							</div>
						</div>		
					<?php endwhile; ?>
				<?php endif;?>
			<?php endif;?>
		<?php endif?>	
		</div>
		<?php if ($paginationEnabled ):?>
			<?php if ( $wp_query->max_num_pages > 1 ): ?>
				<div class="pagination-list blog-pagination">
					<?php include(Wecare_PLUGINS . '/wp-pagenavi.php' ); wp_pagenavi(); ?> 
					<div class="clear"></div>
				</div>
			<?php endif?>
		<?php endif?>		
		<div class="clear"></div>
	</div>
</div>