<?php
/*
Template Name: Portfolio
*/
get_header(); global $gp; ?>

<?php gp_page_header( get_the_ID() ); ?>

<div id="content-wrapper"<?php if ( $GLOBALS['gp_layout'] != 'gp-fullwidth' ) { ?> class="gp-container"<?php } ?>>

	<?php if ( $GLOBALS['gp_filter'] == 'enabled' ) { ?>
		<div id="portfolio-filters" class="portfolio-filters">
			<ul>
			   <li><a href="#" data-filter="*" class="gp-active"><?php echo __( 'All', 'gp_lang' ); ?></a></li>
				<?php 
				$terms = get_terms( 'gp_portfolios' );
				$cat_array = explode( ',', $GLOBALS['gp_cats'] );
				if ( ! empty( $terms ) ) {
					foreach( $terms as $term ) {
						if ( ! empty( $cat_array[0] ) ) {
							foreach( $cat_array as $cat ) {
								if ( $term->term_id == $cat ) {
									echo '<li><a href="#" data-filter=".' . sanitize_title( $term->slug ) . '">' . esc_attr( $term->name ). '</a></li>';
								}
							}	
						} else {
							echo '<li><a href="#" data-filter=".' . sanitize_title( $term->slug ) . '">' . esc_attr( $term->name ). '</a></li>';
						}	
					}	
				}
				?>
			</ul>
		</div><!-- END PORTFOLIO FILTERS -->
	<?php } ?>
	
	<div id="content">
	
		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>		
	
			<?php if ( get_post_meta( $post->post_parent, '_wp_page_template', true ) == 'hub-template.php' ) { ?>
				<header class="entry-header small-header">
					<h2 class="entry-title"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></h2>
				</header>
			<?php } ?>		
		
			<?php the_content(); ?>		

		<?php endwhile; endif; rewind_posts(); ?>			

		<?php
		
		gp_query_variables();
		
		$args = array(
			'post_status' => 'publish',
			'post_type'      => 'gp_portfolio_item',
			'tax_query'      => array( 'relation' => 'OR', $GLOBALS['gp_portfolio_cats'] ),
			'posts_per_page' => $GLOBALS['gp_per_page'],
			'orderby'        => $GLOBALS['gp_orderby_value'],
			'order'          => $GLOBALS['gp_order'],
			'paged'          => $GLOBALS['gp_paged'],
			'date_query' => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),	
		);

		$gp_query = new wp_query( $args ); ?>
		
		<div id="portfolio" class="portfolio-wrapper <?php echo sanitize_html_class( $GLOBALS['gp_format'] ); ?>">		

			<?php if ( $gp_query->have_posts() ) : ?>

				<div class="inner-loop">
				
					<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>

						<?php get_template_part( 'portfolio', 'loop' ); ?>

					<?php endwhile; ?>
				
				</div>
							
				<?php echo gp_pagination( $gp_query->max_num_pages ); ?>

			<?php else : ?>

				<strong class="no-items-found"><?php _e( 'No items found.', 'gp_lang' ); ?></strong>

			<?php endif; ?>
			
		</div><!-- END PORTFOLIO WRAPPER -->
				
	</div><!-- END CONTENT -->

	<?php get_sidebar(); ?>

</div><!-- END CONTENT WRAPPER -->

<script>		
jQuery( document ).ready( function( $ ) {

	'use strict';

	$( '#content' ).prepend( '<div class="loader portfolio-loader"><i class="fa fa-2x fa-circle-o-notch fa-spin"></i></div>' );

	var container = $( '#portfolio .inner-loop' );
	var element = container;

	if ( container.find( 'img' ).length == 0 ) {
		element = $( 'body' );
	}	

	imagesLoaded( element, function( instance ) {

		container.isotope({
			itemSelector: '.portfolio-item',
			filter: '*',
			masonry: {
				<?php if ( $GLOBALS['gp_format'] != 'portfolio-masonry' ) { ?>columnWidth: container.find( '.portfolio-item' )[0],
				gutter: 20<?php } ?>
			}
		});

		$( '.portfolio-loader' ).stop( true, true ).fadeOut( 300 );
		
		container.animate( { 'opacity': 1 }, 1300 );
		$( 'ul.page-numbers' ).animate( { 'opacity': 1 }, 1300 );
		
	});

	$( '#portfolio-filters ul li a' ).click( function() {

		var selector = $( this ).attr( 'data-filter' );
		container.isotope( { filter: selector } );

		$( '#portfolio-filters ul li a' ).removeClass( 'gp-active' );
		$( this ).addClass( 'gp-active' );

		return false;

	});

	
	/*--------------------------------------------------------------
	Remove portfolio filters not found on current page
	--------------------------------------------------------------*/
	
	if ( $( 'div' ).hasClass( 'portfolio-filters' ) ) {
	
		var isotopeCatArr = [];
		var $portfolioCatCount = 0;
		$( '#portfolio-filters ul li' ).each( function( i ) {
			if ( $( this ).find( 'a' ).length > 0 ) {
				isotopeCatArr[$portfolioCatCount] = $( this ).find( 'a' ).attr( 'data-filter' ).substring( 1 );	
				$portfolioCatCount++;
			}
		});
	
		isotopeCatArr.shift();
	
		var itemCats = '';
	
		$( '#portfolio .inner-loop > .portfolio-item' ).each( function( i ) {
			itemCats += $( this ).attr( 'data-portfolio-cat' );
		});
		itemCats = itemCats.split( ' ' );
	
		itemCats.pop();
		
		itemCats = $.unique( itemCats );
	
		var notFoundCats = [];
		$.grep( isotopeCatArr, function( el ) {
			if ( $.inArray(el, itemCats ) == -1 ) {
				notFoundCats.push( el  );
			}
		});
	
		if(notFoundCats.length != 0 ) {
			$( '#portfolio-filters ul li' ).each( function() {
				if ( $( this ).find( 'a' ).length > 0 ) {
					if( $.inArray( $( this ).find( 'a' ).attr( 'data-filter' ).substring( 1 ), notFoundCats ) != -1 ) {
						$( this ).hide();
					}
				}
			});
		}
	
	}
		
});			
</script>

<?php get_footer(); ?>