<?php 

if ( ! function_exists( 'gp_videos' ) ) {

	function gp_videos( $atts, $content = null ) {	
		
		extract( shortcode_atts( array(
			'widget_title' => '',
			'cats' => '', 
			'format' => 'blog-standard',
			'size' => 'blog-small-size',
			'orderby' => 'newest',
			'date_posted' => 'all',
			'date_modified' => 'all',				
			'filter' => 'disabled',
			'filter_cats' => '',
			'filter_date' => '',
			'filter_title' => '',					
			'filter_comment_count' => '',
			'filter_views' => '',
			'filter_date_posted' => '',
			'filter_date_modified' => '',
			'filter_cats_id' => '',
			'per_page' => '5',
			'offset' => '0',
			'featured_image' => 'enabled',
			'image_width' => '75',
			'image_height' => '75',
			'hard_crop' => true,
			'image_alignment' => 'image-wrap-left',
			'title_position' => 'title-next-to-thumbnail',
			'content_display' => 'excerpt',
			'excerpt_length' => '0',
			'meta_author' => '',
			'meta_date' => '',
			'meta_views' => '',
			'meta_comment_count' => '',
			'meta_cats' => '',
			'meta_tags' => '',
			'read_more_link' => 'enabled',
			'page_numbers' => 'disabled',
			'see_all' => 'enabled',
			'see_all_link' => '',
			'see_all_text' => __( 'See All Videos', 'gp_lang' ),
			'classes' => '',
		), $atts ) );
						
		global $gp;

		// Detect shortcode
		$GLOBALS['gp_shortcode'] = 'videos';
				
		gp_shortcode_options( $atts );
		gp_query_variables();
		
		// Unique Name	
		STATIC $i = 0;
		$i++;
		$name = 'gp_videos_wrapper_' . $i;
			
		// Get videos page permalink if none specified
		if ( $see_all == 'enabled' && empty ( $see_all_link ) && get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' ) {
			$gp_pages = get_pages( 'child_of=' . get_the_ID() );
			foreach ( $gp_pages as $gp_page ) {
				if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'videos-template.php' ) {
					$see_all_link = get_permalink( $gp_page->ID );
				}
			}	
		}			

		// Get video posts associated with hub
		$GLOBALS['gp_meta_query'] = '';
		if ( get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' ) {
			$GLOBALS['gp_meta_query'] = array( array( 'key' => 'post_association', 'value' => get_the_ID(), 'compare' => 'LIKE' ) );	
			$GLOBALS['gp_meta_key'] = 'post_association';
		} 
		
		$args = array(
			'post_status'       => 'publish',
			'post_type'    		=> 'post',
			'tax_query'    		=> array(
				'relation' 		=> 'AND',
				$GLOBALS['gp_video_cats'],
				array(
					'taxonomy'  => 'post_format',
					'field'     => 'slug',
					'terms'     => array( 'post-format-video' ),
				)			
			),
			'orderby'           => $GLOBALS['gp_orderby_value'],
			'order'             => $GLOBALS['gp_order'],
			'meta_query' 		=> $GLOBALS['gp_meta_query'],
			'meta_key'          => $GLOBALS['gp_meta_key'],
			'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 ); ?>
		
		<div id="<?php echo sanitize_html_class( $name ); ?>" class="blog-wrapper videos-shortcode <?php echo sanitize_html_class( $GLOBALS['gp_format'] ); ?> <?php echo sanitize_html_class( $size ); ?> <?php echo sanitize_html_class( $classes ); ?>"<?php echo gp_data_properties( 'videos' ); ?>>
		
			<?php if ( $widget_title OR $see_all == 'enabled' ) { ?>
				<div class="element-title">
					<?php if ( $widget_title ) { ?><h3><?php echo esc_attr( $widget_title ); ?></h3><?php } ?>
					<?php if ( $see_all == 'enabled' ) { ?>
						<div class="see-all-link"><a href="<?php echo esc_url( $see_all_link ); ?>"><?php echo esc_attr( $see_all_text ); ?><i></i></a></div>
					<?php } ?>
					<div class="element-title-line"></div>
				</div>
			<?php } ?>
			
			<?php get_template_part( 'lib/sections/filter' ); ?>
								
			<?php if ( $gp_query->have_posts() ) : ?>

				<div class="inner-loop <?php echo sanitize_html_class( $gp['ajax'] ); ?>">
								
					<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>

						<?php get_template_part( 'post', '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 BLOG WRAPPER -->

		<?php if ( $GLOBALS['gp_format'] == 'blog-masonry' && ( ! is_admin() OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) ) { ?>

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

				'use strict';

				if ( $( '.blog-wrapper' ).hasClass( 'blog-masonry' ) ) {
	
					var container = $( '.blog-masonry .inner-loop' );
					var element = container;

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

					imagesLoaded( element, function( instance ) {

						container.isotope({
							itemSelector: '.post-item',
							masonry: {
								columnWidth: container.find( '.post-item' )[0],
								gutter: 20
							}
						});

						container.animate( { 'opacity': 1 }, 1300 );
						$( 'ul.page-numbers' ).animate( { 'opacity': 1 }, 1300 );

					});
				
				}
	
			});	
			</script>	
		
		<?php } ?>
						
		<?php

		$output_string = ob_get_contents();
		ob_end_clean(); 
		$GLOBALS['gp_shortcode'] = null;
		return $output_string;

	}

}

add_shortcode( 'videos', 'gp_videos' );
	
?>