<?php
/*
Template Name: FlexSlider
*/
get_header(); global $gp;
	
gp_query_variables();

// Use old FlexSlider ID for 1.0 users
if ( get_post_meta( get_the_ID(), 'flexslider_id', true ) && ! get_post_meta( get_the_ID(), 'flexslider_cats', true ) ) {
	$GLOBALS['gp_cats'] = implode( ',', get_post_meta( get_the_ID(), 'flexslider_id', true ) );
}

// Use "Games" hub category for demo imported homepage
if ( isset( $GLOBALS['gp_cats'] ) && ! term_exists( $GLOBALS['gp_cats'] ) && $GLOBALS['gp_cats'] == 48 ) {
	$GLOBALS['gp_hub_cats'] = array( 'taxonomy' => 'gp_hubs', 'terms' => explode( ',', 'hub-games' ), 'field' => 'slug' );
}

$args = array(
	'post_status' => 'publish',
	'tax_query' => array( 'relation' => 'OR', $GLOBALS['gp_slide_cats'], $GLOBALS['gp_hub_cats'], $GLOBALS['gp_video_cats'], $GLOBALS['gp_post_cats'] ),
	'posts_per_page' => $GLOBALS['gp_per_page'],
	'meta_key' => '_thumbnail_id',
);

$gp_query = new wp_query( $args ); if ( $gp_query->have_posts() ) : ?>

	<div id="homepage-slider" class="gp-slider">

		 <ul class="slides">
	
			<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>
		
				<?php if ( has_post_thumbnail() ) {

				gp_get_hub_id( get_the_ID() );
				
				if ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' OR get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'review-template.php' ) {
					$GLOBALS['gp_display_site_rating'] = 1;
					$GLOBALS['gp_display_user_rating'] = 1;
					gp_ratings( $GLOBALS['gp_hub_id'] );
				}

				if ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' ) {
					$GLOBALS['gp_title_bg'] = redux_post_meta( 'gp', get_the_ID(), 'hub_title_bg' );
				} elseif ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'review-template.php' ) {
					$GLOBALS['gp_title_bg'] = redux_post_meta( 'gp', get_the_ID(), 'review_title_bg' );
				} else {
					$GLOBALS['gp_title_bg'] = '';
				}	
										
				if ( ! empty( $GLOBALS['gp_title_bg']['url'] ) ) {
				
					 $image = aq_resize( wp_get_attachment_url( gp_get_attachment_id_from_url( $GLOBALS['gp_title_bg']['url'] ) ), $GLOBALS['gp_slider_width'], $GLOBALS['gp_slider_height'], true, false, true );
					if ( $gp['retina'] == 'gp-retina' ) {
						$retina = aq_resize( wp_get_attachment_url( gp_get_attachment_id_from_url( $GLOBALS['gp_title_bg']['url'] ) ), $GLOBALS['gp_slider_width'] * 2, $GLOBALS['gp_slider_height'] * 2, true, true, true );
					} else {
						$retina = '';
					}
					
				} else {

					$image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $GLOBALS['gp_slider_width'], $GLOBALS['gp_slider_height'], true, false, true );
					if ( $gp['retina'] == 'gp-retina' ) {
						$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), $GLOBALS['gp_slider_width'] * 2, $GLOBALS['gp_slider_height'] * 2, true, true, true );
					} else {
						$retina = '';
					}
				
				}
																
				?>				
		
					<li class="<?php if ( isset( $GLOBALS['gp_rated_item_class'] ) ) { echo sanitize_html_class( $GLOBALS['gp_rated_item_class'] ); } ?>">
						
						<?php if ( ! empty( $gp['slide_link'] ) OR $post->post_type == 'post' OR $post->post_type == 'page' ) { ?><a href="<?php if ( $post->post_type == 'gp_slide' ) { echo esc_url( $gp['slide_link'] ); } else { the_permalink(); } ?>"<?php if ( $post->post_type == 'gp_slide' ) { ?> target="<?php echo esc_attr( $gp['slide_link_target'] ); ?>"<?php } ?>><?php } ?>
						
							<div class="slide-image" style="background-image: url(<?php echo esc_url( $image[0] ); ?>);" data-rel="<?php echo esc_url( $retina ); ?>">
				
								<?php if ( ! empty( $gp['slide_caption_title'] ) OR ! empty( $gp['slide_caption_text'] ) OR $post->post_type == 'post' OR $post->post_type == 'page' ) { ?>

									<div class="slide-caption">

										<?php if ( ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' OR get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'review-template.php' ) && ( $GLOBALS['gp_site_rating_enabled'] == true OR $GLOBALS['gp_user_rating_enabled'] == true ) ) { ?>
											<div class="rating-wrapper">
												<?php if ( $GLOBALS['gp_site_rating_enabled'] == true ) { get_template_part( 'lib/sections/site', 'rating' ); } ?>			
												<?php if ( $GLOBALS['gp_user_rating_enabled'] == true ) { get_template_part( 'lib/sections/user', 'rating' ); } ?>
											</div><!-- END RATING WRAPPER -->
										<?php } ?>
															
										<?php if ( ! empty( $gp['slide_caption_title'] ) OR $post->post_type == 'post' OR $post->post_type == 'page' ) { ?>
											<h2 class="slide-caption-title"><?php if ( $post->post_type == 'gp_slide' ) { echo esc_attr( $gp['slide_caption_title'] ); } else { echo gp_prefix_hub_title( get_the_ID() ); } ?></h2>
										<?php } ?>
					
										<?php if ( ! empty( $gp['slide_caption_text'] ) OR $post->post_type == 'post' OR $post->post_type == 'page' ) { ?>
											<h4 class="slide-caption-text"><?php if ( $post->post_type == 'gp_slide' ) { echo esc_attr( $gp['slide_caption_text'] ); } else { echo gp_excerpt( 200 ); } ?></h4>
										<?php } ?>
								
									</div>
					
								<?php } ?>
													
								<?php if ( $gp['title_bg_top_gradient_overlay'] == 'enabled' ) { ?>
									<div class="top-bg-gradient-overlay"></div>
								<?php } ?>
						
								<?php if ( $GLOBALS['gp_side_gradient_overlay'] == 'enabled' ) { ?>
									<div class="side-bg-gradient-overlay"></div>
								<?php } ?>	
						
								<?php if ( $GLOBALS['gp_bottom_gradient_overlay'] == 'enabled' ) { ?>
									<div class="bottom-bg-gradient-overlay"></div>
								<?php } ?>
							
							</div>
															
						<?php if ( ! empty( $gp['slide_link'] ) OR $post->post_type == 'post' OR $post->post_type == 'page' ) { ?></a><?php } ?>
			
					</li>
				
				<?php } ?>
						
			<?php endwhile; ?>
			
		</ul>

	</div>

<?php else : ?>

	<div id="homepage-slider" class="no-slider"></div>

<?php endif; wp_reset_postdata(); ?>
	
<?php if ( ! is_admin() ) { ?>	
	<script>
	jQuery( document ).ready( function( $ ) {
		'use strict';
		$( '.page-id-<?php the_ID(); ?> #homepage-slider.gp-slider' ).flexslider( { 
			animation: 'fade',
			slideshowSpeed: <?php if ( $GLOBALS['gp_timeout'] != '' ) { echo absint( $GLOBALS['gp_timeout'] * 1000 ); } else { echo '9999999'; } ?>,
			animationSpeed: 600,
			directionNav: true,			
			controlNav: false,			
			pauseOnAction: true, 
			pauseOnHover: false,
			prevText: '',
			nextText: ''
		});

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

<?php if ( ! empty( $gp['header_ad'] ) ) { ?>
	<div id="header-area">
		<div class="gp-container">
			<?php echo do_shortcode( $gp['header_ad'] ); ?>
		</div>
	</div>			
<?php } ?>
		
<div id="content-wrapper"<?php if ( $GLOBALS['gp_layout'] != 'gp-fullwidth' ) { ?> class="gp-container"<?php } ?>>

	<div id="content">

		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>				
	
			<?php the_content(); ?>	
		
		<?php endwhile; endif; ?>		

	</div><!-- END CONTENT -->
	
	<?php get_sidebar(); ?>

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

<?php get_footer(); ?>