<?php

class Intense_Custom_Post extends Intense_Shortcode {

	function __construct() {
		Intense_Shortcode::__construct();

		global $intense_visions_options;

		$this->title = __( 'Custom Post Type', 'intense' );
		$this->description = __( 'The custom post type shortcode allows you to list posts from any custom post type. You can use layouts like those found on the Blog shortcode page, or you can create your own templates with the Templates Custom Post Type.
			<br><br>
			With the Custom Post Types shortcode, you can set the post type, taxonomy, category(s), author(s), order, posts per page, animation, slider, and many other options.', 'intense' );
		$this->category = __( 'Posts', 'intense' );
		$this->show_preview = true;
		$this->map_visual_composer = false;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options, $post, $page, $wp_query, $intense_custom_post, $intense_post_types, $paged;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$saved_post = $post;
		$saved_page = $page;
		$output = '';
		$category_list = '';

		if ( $is_slider ) {
			$infinite_scroll = false;
			$show_filter = false;
		}

		if ( $infinite_scroll ) {
			intense_add_style( 'intense_infinite_scroll' );
			intense_add_script( 'ias_infinite_scroll' );
			intense_add_style( 'intense_flexslider' );
			intense_add_script( 'intense_flexslider' );
			intense_add_style( 'intense_video_js_css' );
			intense_add_script( 'intense_video_js' );
			intense_add_script( 'intense_fitvids' );
		}

		if ( !empty( $type ) ) {
			$template = $type;
		}

		if ( !empty( $timeline_order ) ) {
			$order = $timeline_order;
		}

		$is_timeline = stristr( $template, "timeline" );

		$padding_top = $intense_visions_options['intense_layout_row_default_padding']['padding-top'];
		$padding_bottom = $intense_visions_options['intense_layout_row_default_padding']['padding-bottom'];
		$margin_top = $intense_visions_options['intense_layout_row_default_margin']['margin-top'];
		$margin_bottom = $intense_visions_options['intense_layout_row_default_margin']['margin-bottom'];

		$layout_style = '';

		if ( !empty( $padding_top ) ) {
			$layout_style .= 'padding-top: ' . $padding_top . '; ';
		}

		if ( !empty( $padding_bottom ) ) {
			$layout_style .= 'padding-bottom: ' . $padding_bottom . '; ';
		}

		if ( !empty( $margin_top ) ) {
			$layout_style .= 'margin-top: ' . $margin_top . '; ';
		}

		if ( !empty( $margin_bottom ) ) {
			$layout_style .= 'margin-bottom: ' . $margin_bottom . '; ';
		}

		$no_layout_style = "padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;";

		if ( !empty( $animation_type ) ) {
			$animation_wrapper_start = intense_get_animation_wrapper_start( $animation_type, $animation_trigger, $animation_scroll_percent, $animation_delay );
			$animation_wrapper_end = intense_get_animation_wrapper_end();
		} else {
			$animation_wrapper_start = null;
			$animation_wrapper_end = null;
		}

		$intense_post_type = $intense_post_types->get_post_type( $post_type );

		if ( $is_timeline && isset( $timeline_readmore ) && $timeline_readmore != 'Read More' ) {
			$read_more_text = $timeline_readmore;
		}

		$intense_custom_post = array(
			'plugin_layout_style' => $layout_style,
			'cancel_plugin_layout_style' => $no_layout_style,
			'post_type' => $intense_post_type,
			'hover_effect_type' => ( !isset( $hover_effect_type ) && isset( $hover_effect ) && is_numeric( $hover_effect ) ? 'effeckt' : $hover_effect_type ),
			'animation_trigger' => ( isset( $animation_trigger ) ? $animation_trigger : null ) ,
			'animation_scroll_percent' => ( isset( $animation_scroll_percent ) ? $animation_scroll_percent : null ),
			'animation_delay' => ( isset( $animation_delay ) ? $animation_delay : null ),
			'animation_wrapper_start' => $animation_wrapper_start,
			'animation_wrapper_end' => $animation_wrapper_end,
			'category_list' => $category_list,
		);

		$intense_custom_post = array_merge( $this->get_shortcode_defaults( null, $atts ), $atts, $intense_custom_post );

		$page_query_var = get_query_var( 'page' );
		$paged_query_var =  get_query_var( 'paged' );

		if ( !empty( $page_query_var ) ) {
			$paged = $page_query_var;
		} else if ( !empty( $paged_query_var ) ) {
			$paged = $paged_query_var;
		} else {
			$paged = 1;
		}

		$iscat_array = false;
		$cat_array = array();
		$blog_cat_array = array();
		$timeline_events = '';
		$mixid = 'mix_list_' . rand();
		$blogid = 'cpt_list_' . rand();
		$filter_output = '';

		$args = array(
			'post_type' => $post_type,
			'orderby' => $order_by,
			'order' => $order,
			'paged'=> $paged,
			'nopaging' => ( $show_all == 0 ? false : true ),
			'tax_query' => ''
		);

		if ( 'inline' == $sticky_mode || 'ignore' == $sticky_mode ) {
			$args['ignore_sticky_posts'] = 1;
		}

		if ( $post_type == "intense_portfolio" && $order_by == "meta_value" ) {
			$args["meta_key"] = 'intense_portfolio_date';
		}

		if ( $post_type == "intense_project" && $order_by == "meta_value" ) {
			$args["meta_key"] = 'intense_project_date';
		}

		if ( $post_type == "intense_books" && $order_by == "release_date" ) {
			$args["meta_key"] = 'intense_book_release_date';
		}

		if ( $post_type == "intense_music" && $order_by == "release_date" ) {
			$args["meta_key"] = 'intense_music_release_date';
		}

		if ( $post_type == "intense_coupons" ) {
			if ( $order_by == "start_date" ) {
				 $args["meta_key"] = 'intense_coupon_start_date';
			} else if ( $order_by == "end_date" ) {
				$args["meta_key"] = 'intense_coupon_end_date';
			}
		}

		if ( $post_type == "intense_events" ) {
			if ( $order_by == "start_date" ) {
				 $args["meta_key"] = 'intense_event_start_date';
			} else if ( $order_by == "end_date" ) {
				$args["meta_key"] = 'intense_event_end_date';
			}
		}

		if ( $post_type == "intense_jobs" ) {
			if ( $order_by == "post_date" ) {
				 $args["meta_key"] = 'intense_job_post_date';
			} else if ( $order_by == "expire_date" ) {
				$args["meta_key"] = 'intense_job_expire_date';
			}
		}

		if ( $post_type == "intense_movies" ) {
			if ( $order_by == "theatrical_release" ) {
				 $args["meta_key"] = 'intense_movie_release_date';
			} else if ( $order_by == "dvd_release" ) {
				$args["meta_key"] = 'intense_movie_dvd_release_date';
			}
		}

		if ( $post_type == "intense_news" && $order_by == "news_date" ) {
			$args["meta_key"] = 'intense_news_date';
		}

		if ( $authors != '' ) {
			$args['author'] = $authors;
		}

		if ( $categories != '' && isset( $taxonomy ) && $taxonomy != '' ) {
			$iscat_array = true;

			if ( strpos( $categories, ',' ) ) {
				$cat_array = explode( ',', str_replace( ' ', '', $categories ) );

				$args['tax_query'] = array(
					array(
						'taxonomy' => $taxonomy,
						'field'  => 'slug',
						'terms'  => $cat_array
					)
				);
			} else {
				$cat_array = array( $categories );
				$args['tax_query'] = array(
					array(
						'taxonomy' => $taxonomy,
						'field'  => 'slug',
						'terms'  => array( $categories )
					)
				);
			}
		}

		if ( $include_post_ids != '' ) {
			if ( strpos( $include_post_ids, ',' ) ) {
				$id_array = explode( ',', $include_post_ids );
				$args['post__in'] = $id_array;
			} else {
				$args['post__in'] = array( $include_post_ids );
			}
		}

		$args['category__not_in'] = array();

		if ( $exclude_categories != '' ) {
			$ex_array = array();
			if ( strpos( $exclude_categories, ',' ) ) {
				$ex_array = explode( ',', $exclude_categories );
			} else {
				$ex_array = array( $exclude_categories );
			}

			if ( is_array( $ex_array ) ) {
				$ex_cat_array = array();

				foreach ( $ex_array as $ex_cat ) {
					$term = get_term_by( 'slug', $ex_cat, $taxonomy );

					if ( !empty( $term ) ) {
						$ex_cat_array[] = $term->term_id;

						if ( !empty( $taxonomy ) ) {
							$args['tax_query'][] = array(
								array(
									'taxonomy' => $taxonomy,
									'field'  => 'slug',
									'terms'  => $ex_cat,
									'operator'  => 'NOT IN'
								)
							);
						}
					}
				}

				if ( is_array( $ex_cat_array ) ) {
					$args['category__not_in'] = $ex_cat_array;
				}
			}
		}

		if ( $exclude_post_ids != '' ) {
			if ( strpos( $exclude_post_ids, ',' ) ) {
				$id_array = explode( ',', $exclude_post_ids );
				$args['post__not_in'] = $id_array;
			} else {
				$args['post__not_in'] = array( $exclude_post_ids );
			}
		}

		if ( $post_count != '' && is_numeric( $post_count ) ) {
			$args2 = $args;
			$post_in_array = array();
			$count = 0;
			$args2['nopaging'] = true;

			$theposts = get_posts( $args2 );

			$found_posts = count( $theposts );

			foreach( $theposts as $post ) {
				$post_in_array[] = $post->ID;
				$count++;

				if ( $count == $post_count ) {
				 	break;
				}
			}

			wp_reset_postdata();

			if ( is_array( $post_in_array ) ) {
				$args['post__in'] = $post_in_array;
			}
		}

		$args['posts_per_page'] = $posts_per_page;

		$blog_category = get_terms( $taxonomy, array( 'exclude' => $args['category__not_in'] ) );

		if ( $iscat_array ) {
			if ( is_array( $blog_category ) ) {
				$cat_array = $this->addParents( $blog_category, $cat_array, $taxonomy );
			}

			$cat_array = $this->addChildren( $cat_array, $taxonomy );
		}

		if ( empty( $use_current_wpquery ) ) {
			$post_query = new WP_Query( $args );
		} else if ( $use_current_wpquery ) {
			$post_type = $wp_query->query_vars['post_type'];			
			$new_query_vars = array();			
			
			if ( !empty( $posts_per_page ) ) $new_query_vars['posts_per_page'] = $posts_per_page;
			if ( !empty( $order_by ) ) $new_query_vars['orderby'] = $order_by;	
			if ( !empty( $order ) ) $new_query_vars['order'] = $order;	

			if ( count( $new_query_vars ) > 0 ) {
				foreach ($new_query_vars as $key => $value) {
					$wp_query->set( $key, $value );
				}

				$wp_query->set( 'post_type', $post_type );
				$wp_query = new WP_Query($wp_query->query_vars);
			}
			
			$post_query = $wp_query;
		}

		if ( !isset( $found_posts ) ) $found_posts = $post_query->found_posts;

		$intense_custom_post['found_posts'] = $found_posts;

		$output .= '<div class="intense custom-post' . ( !empty( $class ) ? ' ' . $class : '' ) . '">';

		if ( $show_list_title ) {
			if ( count( $cat_array ) == 1 ) {
				$term = get_term_by( 'slug', $categories, $taxonomy );
				$term_link = get_term_link( $term, $taxonomy );

				if ( empty( $list_title_text ) ) $list_title_text = $term->name;
				if ( empty( $list_title_link ) ) $list_title_link = $term_link;
			}

			if ( empty( $list_title_text ) ) $list_title_text = '&nbsp;';

			if ( !empty( $list_title_link ) ) $list_title_text = '<a href="' . $list_title_link . '">' . $list_title_text . '</a>';

			$post_count_text = '';

			if ( !empty( $show_total_count ) ) {
				$post_count_text = intense_run_shortcode( 'intense_badge', array(
					'color' => 'muted',
					'border_radius' => '3px',
					'class' => 'post-count' ), $found_posts );
			}

			$output .= intense_run_shortcode( 'intense_heading', array(
	            'tag' => 'h3',
	            'margin_bottom' => '5px',
	            'padding_bottom' => '10px',
	            'class' => 'custom-post-heading'
	        ), $list_title_text . $post_count_text );
		}

		$output .= "<div id='" . $mixid . "' class='intense row' style='$no_layout_style'><div class='intense col-lg-12 col-md-12 col-sm-12 col-xs-12'>";

		//Add filter to output
		if ( !$is_timeline && !$infinite_scroll && ( ( $show_filter && $categories == '' ) || ( $iscat_array && $show_filter ) ) ) {
			$filter_output = $this->setup_filters( $no_layout_style, $blog_category, $filter_all_text, $iscat_array, $cat_array );
			$output .= $filter_output;
		}

		if ( $categories != '' ) {
			$intense_custom_post['category_list'] = $categories;
		} else {
			$intense_custom_post['category_list'] = implode( ', ', $blog_cat_array );
		}

		if ( $infinite_scroll ) {
			$blogid = 'intense_infinite';
		}

		$output .= "<div id='" . $blogid . "' class='intense row' style='" . $layout_style . " padding-left: 15px; padding-right: 15px; overflow-y: inherit;'>";

		$i = 0;

		$slider = '';

		if ( $is_slider ) {
			$output = ''; //kill the output
			$slider .= '[intense_slider type="owl-carousel"
    			items="' . $items . '"
            	items_desktop="' . $items_desktop . '"
            	items_desktop_small="' . $items_desktop_small . '"
            	items_tablet="' . $items_tablet . '"
            	items_mobile="' . $items_mobile . '"
            	navigation_position="' . $navigation_position . '"
            	navigation_prev_icon_source="' . $navigation_prev_icon_source . '"
            	navigation_prev_icon="' . $navigation_prev_icon . '"
            	navigation_next_icon_source="' . $navigation_next_icon_source . '"
            	navigation_next_icon="' . $navigation_next_icon . '"
            	navigation_size="' . $navigation_size . '"
            	navigation_prev_text="' . $navigation_prev_text . '"
            	navigation_next_text="' . $navigation_next_text . '"
            	navigation_color="' . $navigation_color . '"
            	navigation_font_color="' . $navigation_font_color . '"
            	navigation_prev_border_radius="' . $navigation_prev_border_radius . '"
            	navigation_next_border_radius="' . $navigation_next_border_radius . '"
            	navigation_pagination_type="' . $navigation_pagination_type . '"
            	mousedrag="' . $mousedrag . '"
            	touchdrag="' . $touchdrag . '"
				autoheight="' . $autoheight . '"
			]';
		}

		$masonry = '';

		if ( $is_masonry ) {
			$output = ''; //kill the output
			$masonry .= '[intense_masonry id="' . $blogid  . '" gutter="' . $gutter . '"]';
		}

		$collapsible = '';

		if ( $show_collapsible ) {
			$collapsible .= '[intense_collapsibles]';
		}

		while ( $post_query->have_posts() ) {
			$post_query->the_post();
			$item_classes = '';
			$editlink = '';
			$portfolio_categories = '';
			$item_cats = get_the_terms( $post->ID, $taxonomy );
			$intense_custom_post['hover_effect'] = '';
			$intense_custom_post['index'] = $i;
			$margintop = '0px';

			if ( !empty( $intense_post_type ) ) {
				$intense_custom_post['template_link_url'] = $intense_post_type->get_link( $template_link );
			} else {
				$intense_custom_post['template_link_url'] = get_permalink( $post->ID );
			}

			if ( $post_type == "intense_portfolio" ) {
				$portfolio_date = get_field( 'intense_portfolio_date' );
			}

			if ( $post_type == "intense_project" ) {
				$portfolio_date = get_field( 'intense_project_date' );
			}

			if ( has_post_thumbnail() ) {
				$intense_custom_post['image_shadow'] = intense_coalesce( $image_shadow, get_field( 'intense_image_shadow' ), '0' );
				$intense_custom_post['hover_effect_type'] = intense_coalesce( $hover_effect_type, get_field( 'intense_hover_effect_type' ), 'effeckt' );
				$intense_custom_post['subtle_effect'] = intense_coalesce( $subtle_effect, get_field( 'intense_subtle_hover_effect' ) );
				$intense_custom_post['hover_effect'] = intense_coalesce( $hover_effect, get_field( 'intense_hover_effect' ) );
				$intense_custom_post['hover_effect_color'] = intense_coalesce( $hover_effect_color, get_field( 'intense_effect_color' ) );
				$intense_custom_post['hover_effect_opacity'] = intense_coalesce( $hover_effect_opacity, get_field( 'intense_effect_opacity' ) );
			}

			$terms = get_the_terms( $post->ID, $taxonomy );

			if ( $terms && !is_wp_error( $terms ) ) {
				$categories_list = array();

				foreach ( $terms as $term ) {
					$item_classes .= $term->slug . ' ';
					$categories_list[] = $term->name;
				}

				$intense_custom_post['categories'] = join( ", ", $categories_list );
			}

			if ( current_user_can( 'manage_options' ) ) {
				$intense_custom_post['edit_link'] = "&nbsp;" . intense_run_shortcode( 'intense_button', array( 'size' => 'mini', 'color' => 'inverse', 'class' => 'post-edit-link' , 'link' => get_edit_post_link( $post->ID ), 'title' => __( "Edit Post", "intense" ) ), __( "Edit", "intense" ) );
			} else {
				$intense_custom_post['edit_link'] = '';
			}

			if ( !$is_timeline && !$infinite_scroll && ( ( $show_filter && $categories == '' ) || ( $iscat_array && $show_filter ) ) ) {
				$item_classes .= 'intense mix ';
			}

			$wp_post_classes = get_post_class( $item_classes );
			$intense_custom_post['post_classes'] = implode(' ', $wp_post_classes);

			// if the template is numeric then it is a templates that they have saved in WordPress
			if ( is_numeric( str_replace("template_", "", $template ) ) ) {
				$post_content = intense_get_template( $template );
			} else {
				$post_content = intense_get_template( array(
					'/custom-post/' .  get_post_type() . '/' . $template . '.php',
					'/custom-post/post/' . $template . '.php',
					'/custom-post/post/one.php'
				));
			}

			if ( $is_timeline ) {
				$timeline_events .= "[intense_timeline_event animation_type='$animation_type' animation_trigger='$animation_trigger' animation_scroll_percent='$animation_scroll_percent' animation_delay='$animation_delay' type='blog_post' featured='" . ( is_sticky() && 'ignore' != $sticky_mode ? "1" : "0" ) . "' date='" . esc_html( get_the_date( 'Y-m-d' ) ) . "' column_mode='" . $timeline_mode . "' color='" . $timeline_color . "' image_border_radius='" . $image_border_radius . "' event_border_radius='" . $timeline_border_radius . "' use_content='1']" . $post_content . "[/intense_timeline_event]";
			} else if ( $is_slider ) {
				$slider .= '[intense_slide]<div style="padding: 0 ' . $slide_padding_right . 'px 0 0">' . $post_content . '</div>[/intense_slide]';
			} else if ( $is_masonry ) {
				$masonry .= '[intense_masonry_item ' .
				'size="' . ( is_sticky() && 'ignore' != $sticky_mode ? $masonry_sticky_size : $masonry_size ) . '" ' .
				'medium_size="' . ( is_sticky() && 'ignore' != $sticky_mode ? $masonry_sticky_medium_size : $masonry_medium_size ) . '" ' .
				'small_size="' . ( is_sticky() && 'ignore' != $sticky_mode ? $masonry_sticky_small_size : $masonry_small_size ) . '" ' .
				'extra_small_size="' . ( is_sticky() && 'ignore' != $sticky_mode ? $masonry_sticky_extra_small_size : $masonry_extra_small_size ) . '" ' .
				'width="' . ( is_sticky() && 'ignore' != $sticky_mode ? $masonry_sticky_width : $masonry_width ) . '"]' . $post_content . '[/intense_masonry_item]';
			} else if ( $show_collapsible ) {
				$collapsible .= '[intense_collapse title="' . the_title_attribute( 'echo=0' ) . '" class="intense col-lg-12 col-md-12 col-sm-12 col-xs-12 ' . $intense_custom_post['post_classes'] . '"]' . $post_content . '[/intense_collapse]';
			} else {
				$output .= $post_content;
			}

			$i++;
		}

		if ( $is_timeline ) {
			$output .= do_shortcode( "[intense_timeline show_year='" . $timeline_showyear . "' column_mode='" . $timeline_mode . "' order='" . $order . "']" . $timeline_events . "[/intense_timeline]" );
		} else if ( $is_slider ) {
			$slider .= '[/intense_slider]';
	    	$output = '<div class="intense custom-post' . ( !empty( $class ) ? ' ' . $class : '' ) . '">';
	    	$output .= do_shortcode( $slider );
	    } else if ( $show_collapsible ) {
			$collapsible .= '[/intense_collapsibles]';
	    	$output .= do_shortcode( $collapsible );
		} else if ( $is_masonry ) {
			$masonry .= '[/intense_masonry]';

			$output = '<div class="intense custom-post' . ( !empty( $class ) ? ' ' . $class : '' ) . '">';
	    	$output .= do_shortcode( $masonry );
	    	$output .= intense_return_content_nav( 'nav-below', null, $post_query );

	    	if ( $infinite_scroll ) {
	    		intense_add_script( 'intense.animated' );

	    		$output .= "<script> jQuery(function($){";
				$output .= " $(document).ready(function() {";
				$output .= "
					var lastVisibleYear = null;
					var scrollTop = null;

					jQuery(window).scroll(function() {
						scrollTop = jQuery(window).scrollTop();
					});

					jQuery.ias({
			            container : '#intense_infinite',
			            item: '.item',
			            pagination: '.intense.pagination',
						triggerPageThreshold: 100,
			            next: 'a.pagination-next',
			            loader: '<img src=\"" . INTENSE_PLUGIN_URL . "/assets/img/loader.gif\" />',
			            onRenderComplete: function(items) {
			            	intenseSetupAnimations();

						    jQuery.each(items, function(i, el) {
								if ( lastVisibleYear && jQuery(el).hasClass('year') && jQuery(el).text() == lastVisibleYear ) {
									jQuery(el).next().removeClass('offset-first');
									jQuery(el).remove();
								}
						    });
							jQuery('#intense_infinite').packery('destroy');
							var packery = intenseInitMasonry();
							jQuery(window).scrollTop(scrollTop);
							setTimeout(function() {
								packery.packery();
							}, 500);
							packery.packery();
						},
						beforePageChange: function(scrollOffset, nextPageUrl) {

							lastVisibleYear = jQuery('.year').last().text();
						},
						onPageChange: function() {
							var top = ($('#intense_infinite').height()) + 'px';
							var left = (($('#intense_infinite').width() / 2) - 25) + 'px';

							$('.ias_loader, .ias_trigger').css({
								position: 'absolute',
								top: top,
								width: '50px',
								height: '50px',
								left: left
							});
						}
			        });";
				$output .= " });";
				$output .= "}); </script>";
			}
		}

		if ( !$is_slider && !$is_masonry ) {

			$output .= "</div>";

			$output .= "<script> jQuery(function($){";
			$output .= " $(document).ready(function() {";

			if ( ( $show_filter && $categories == '' && !$is_timeline && !$infinite_scroll ) || ( $iscat_array && $show_filter && !$is_timeline && !$infinite_scroll ) ) {
				// mixitup jquery plugin
				intense_add_script( 'mixitup' );

				$output .= "  $('#" . $blogid . "').mixitup({";

				if ( strlen( $filter_effects ) > 0 ) {
					$output .= "   effects: ['" . str_replace( ',', '\',\'', $filter_effects ) . "'],";
				} else {
					$output .= "   effects: ['fade','scale'],";
				}

				if ( strlen( $filter_easing ) > 0 ) {
					$output .= "   easing: '" . $filter_easing . "',";
				} else {
					$output .= "   easing: 'smooth',";
				}

				$output .= "	filterSelector: '#" . $mixid . " .filter'";

				$output .= "  }); $('.intense.mix').css('visibility', 'visible');";

				$GLOBALS['IntensePluginInlineStyle']['#' . $blogid] = "
					#" . $blogid . " {
						overflow-y: hidden; }
					#" . $blogid . " .mix {
					  	opacity: 0;
					  	display: none; }
					#" . $blogid . " h3, #" . $blogid . " .widget-title, #" . $blogid . " h1 {
					  	line-height: normal !important; }
					#" . $blogid . " div.mix {
					  	vertical-align: top; }";
			}

			if ( $infinite_scroll ) {
				$output .= "
					var lastVisibleYear = null;
					jQuery.ias({
			            container : '#intense_infinite',
			            item: '.intense.intense_post',
			            pagination: '.intense.pagination',
			            triggerPageThreshold: 100,
			            next: 'a.pagination-next',
			            loader: '<img src=\"" . INTENSE_PLUGIN_URL . "/assets/img/loader.gif\" />',
			            onRenderComplete: function(items) {
			            	intenseSetupAnimations();

						    jQuery.each(items, function(i, el) {
								if ( lastVisibleYear && jQuery(el).hasClass('year') && jQuery(el).text() == lastVisibleYear ) {
									jQuery(el).next().removeClass('offset-first');
									jQuery(el).remove();
								}
						    });
						},
						beforePageChange: function(scrollOffset, nextPageUrl) {
							lastVisibleYear = jQuery('.year').last().text();
						}
			        });";
			}

			$output .= " });";
			$output .= "}); </script>";
			$output .= "</div></div>";
			if ( !is_numeric( $post_count ) ) {
				$output .= intense_return_content_nav( 'nav-below', null, $post_query );
			}
			$output .= "<div style='clear:both;'></div>";
		}

		if ( !empty( $use_current_wpquery ) && $use_current_wpquery ) {
			wp_reset_postdata();
		}

		$output .= '</div><!-- end custom post -->';

		$page = $saved_page;
		$post = $saved_post;

		return $output;
	}

	function setup_filters( $no_layout_style, $blog_category, $filter_all_text, $iscat_array, $cat_array ) {
		$filter_output  = '';

		//Setup filters
		if ( is_array( $blog_category ) ) {
			$filter_output .= "<div class='intense row' style='$no_layout_style'><div class='intense col-lg-12 col-md-12 col-sm-12 col-xs-12'>";
			$filter_output .= "<nav role='navigation' id='nav-below-filter' class='filter-post'>";
			$filter_output .= "<ul class='intense pager " . ( is_rtl() ? "rtl " : "" ) . "'>";
			$filter_output .= "<li><a class='filter btn-mini active' data-filter='all' href='#allfilter'>" . $filter_all_text . "</a></li>";

			foreach ( $blog_category as $blog_cat ) {
				if ( $iscat_array ) {
					if ( in_array( $blog_cat->slug, $cat_array ) ) {
						$filter_output .= "<li><a class='filter btn-mini' data-filter='" . $blog_cat->slug . "' href='#" . $blog_cat->slug . "filter'>" . esc_html( $blog_cat->name ) . "</a></li>";
					}
				} else {
					$filter_output .= "<li><a class='filter btn-mini' data-filter='" . $blog_cat->slug . "' href='#" . $blog_cat->slug . "filter'>" . esc_html( $blog_cat->name ) . "</a></li>";
				}
				$blog_cat_array[] = $blog_cat->slug;
			}

			$filter_output .= "</ul>";
			$filter_output .= "</nav>";
			$filter_output .= "</div></div>";
		}

		return $filter_output;
	}

	function addParents( $blog_category, $cat_array, $taxonomy ) {
		foreach ( $blog_category as $blog_cat ) {
			$parent = get_term( $blog_cat->parent, $taxonomy ); // get parent term

			if ( !is_wp_error( $parent ) ) {
				if ( in_array( $parent->slug, $cat_array ) && !in_array( $blog_cat->slug, $cat_array ) ) {
					array_push( $cat_array, $blog_cat->slug );
				}
			}
		}

		return $cat_array;
	}

	function addChildren( $cat_array, $taxonomy ) {
		foreach ( $cat_array as &$value ) {
			$value_id = get_term_by( 'slug', $value, $taxonomy );
			$children = get_term_children( $value_id->term_id, $taxonomy ); // get children

			if ( sizeof( $children ) > 0 ) {
				foreach ( $children as $child_cat ) {
					$term = get_term_by( 'id', $child_cat, $taxonomy );
					if ( !in_array( $term->slug, $cat_array ) ) {
						array_push( $cat_array, $term->slug );
					}
				}
			}
		}

		return $cat_array;
	}
}
