<?php

if ( ! function_exists( 'gp_portfolio' ) ) {

	function gp_portfolio( $atts, $content = null ) {
	
		extract( shortcode_atts( array(
			'format' => 'portfolio-columns-3',
			'cats' => '',
			'orderby' => 'newest',
			'date_posted' => 'all',
			'date_modified' => 'all',
			'filter' => 'enabled',
			'per_page' => '12',	
			'offset' => '0',		
			'page_numbers' => 'enabled',
			'classes' => '',
		), $atts ) );
		
		gp_shortcode_options( $atts );
		gp_query_variables();
				
		// Unique Name
		STATIC $i = 0;
		$i++;
		$name = 'portfolio_wrapper_' . $i;
				
		$args = array(
			'post_type'      => 'gp_portfolio_item',
			'tax_query' 	 => array( 'relation' => 'OR', $GLOBALS['gp_portfolio_cats'] ),
			'orderby'        => $GLOBALS['gp_orderby'],
			'order'          => $GLOBALS['gp_order'],
			'posts_per_page' => $GLOBALS['gp_per_page'],
			'offset' 		 => $GLOBALS['gp_offset'],
			'paged'          => $GLOBALS['gp_paged'],
			'date_query' 	 => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
		);		
		
		ob_start(); $gp_query = new wp_query( $args ); ?>
	
		<?php if ( $filter == 'enabled' ) { ?>
			<div id="<?php echo sanitize_html_class( $name ); ?>-filters" class="portfolio-filters portfolio-shortcode">
				<ul>
				   <li><a href="#" data-filter="*" class="gp-active"><?php _e( 'All', 'gp_lang' ); ?></a></li>
					<?php 
					$terms = get_terms( 'gp_portfolios' );
					$cat_array = explode( ',', $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="<?php echo sanitize_html_class( $name ); ?>" class="portfolio-wrapper portfolio-shortcode <?php echo sanitize_html_class( $GLOBALS['gp_format'] ); ?> <?php echo sanitize_html_class( $classes ); ?>">

			<?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 if ( $page_numbers == 'enabled' ) { ?>
					<?php echo gp_pagination( $gp_query->max_num_pages ); ?>
				<?php } ?>

			<?php else : ?>

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

			<?php endif; wp_reset_postdata(); ?>	

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

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

			'use strict';

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

			var container = $( '#<?php echo sanitize_html_class( $name ); ?> .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 );
		
			});

			$( '#<?php echo sanitize_html_class( $name ); ?>-filters ul li a' ).click( function() {

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

				$( '#<?php echo sanitize_html_class( $name ); ?>-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;
				$( '#<?php echo sanitize_html_class( $name ); ?>-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 = '';
	
				$( '#<?php echo sanitize_html_class( $name ); ?> .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 ) {
					$( '#<?php echo sanitize_html_class( $name ); ?>-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

		$output_string = ob_get_contents();
		ob_end_clean(); 
		return $output_string;
	
	}
	
}
	
add_shortcode( 'portfolio', 'gp_portfolio' ); ?>