<?php

$GLOBALS['gp_post_id'] = '';

if ( ! function_exists( 'gp_page_header' ) ) {

	function gp_page_header( $gp_post_id = '' ) {
		
		global $gp, $post;

		// Check if post parent ID exists
		if ( is_singular() ) {
			if ( $post->post_parent > 0 ) {
				$GLOBALS['gp_post_parent_id'] = $post->post_parent;
			} else {
				$GLOBALS['gp_post_parent_id'] = get_the_ID();
			}
		} else {
			$GLOBALS['gp_post_parent_id'] = '';
		}
						
		// Get hub parent page ID	
		if ( get_post_meta( $GLOBALS['gp_post_parent_id'], '_wp_page_template', true ) == 'hub-template.php' && ! is_page_template( 'review-template.php' ) && ! is_archive() && ! is_search() ) {
			$GLOBALS['gp_post_id'] = $GLOBALS['gp_post_parent_id'];
		} elseif ( is_singular( 'post' ) && get_post_meta( $gp_post_id, 'post_association', true ) && $gp['hub_header_posts'] == 'enabled' ) {
			$GLOBALS['gp_post_id'] = get_post_meta( $gp_post_id, 'post_association', true );
			$GLOBALS['gp_post_id'] = $GLOBALS['gp_post_id'][0];
		} elseif ( is_singular( 'gp_user_review' ) ) {
			$GLOBALS['gp_post_id'] = get_post_meta( $gp_post_id, '_hub_page_id', true );	
		} else {
			$GLOBALS['gp_post_id'] = $gp_post_id;
		}

		// Detect WooCommerce
		if ( function_exists( 'is_woocommerce' ) && ( is_shop() OR is_product_category() OR is_product_tag() ) ) {
			$woocommerce = true;
			$GLOBALS['gp_post_id'] = get_option( 'woocommerce_shop_page_id' ); // Get WooCommerce shop page ID	
		} else {
			$woocommerce = '';
		}
		
		// Detect BuddyPress
		if ( function_exists( 'bp_is_active' ) && ! bp_is_blog_page() ) {
			$buddypress = true;
		} else {
			$buddypress = '';
		}
				
		// Detect bbPress
		if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
			$bbpress = true;
		} else {
			$bbpress = '';
		}
					
		// Detect parent or child hub page
		if ( ( get_post_meta( $GLOBALS['gp_post_id'], '_wp_page_template', true ) == 'hub-template.php' OR ( is_singular( 'post' ) && get_post_meta( $gp_post_id, 'post_association', true ) && $gp['hub_header_posts'] == 'enabled' ) OR is_singular( 'gp_user_review' ) ) && ( ! is_archive() && ! is_search() ) ) {
			$GLOBALS['gp_hub_header'] = true;
		} else {
			$GLOBALS['gp_hub_header'] = false;
		}
			
		// Background image
		if ( $GLOBALS['gp_hub_header'] == true ) {
			$GLOBALS['gp_title_bg'] = get_post_meta( $GLOBALS['gp_post_id'], 'hub_title_bg', true );
		}		
		if ( ( is_singular() OR $woocommerce == true OR $bbpress == true OR is_search() OR is_author() ) && ! empty( $GLOBALS['gp_title_bg']['url'] ) ) {
			//echo 'Ind: Post, page, search, author and shop background from title header option';
			//echo ' Global: BuddyPress background from title header option';
			$GLOBALS['gp_title_bg_css'] = ' style="background-image: url(' . $GLOBALS['gp_title_bg']['url'] . ');"';
		} elseif ( ( is_singular() OR $woocommerce == true OR $bbpress == true ) && has_post_thumbnail( $GLOBALS['gp_post_id'] ) ) {
			//echo 'Ind: Post, page, search, author and shop background from featured image';
			//echo ' Global: BuddyPress background from featured image';
			$GLOBALS['gp_title_bg_css'] = ' style="background-image: url(' . wp_get_attachment_url( get_post_thumbnail_id( $GLOBALS['gp_post_id'] ) ) . ');"';
		} elseif ( is_archive() && ! is_search() && ! is_author() && ! empty( $GLOBALS['gp_title_bg'] ) ) {	
			//echo 'Ind: Category background image';
			$GLOBALS['gp_title_bg_css'] = ' style="background-image: url(' . $GLOBALS['gp_title_bg'] . ');"';
		} elseif ( ( ( $woocommerce == true OR $bbpress == true ) && is_archive() ) && ! empty( $GLOBALS['gp_title_bg']['url'] ) ) {
			//echo 'Global: WooCommerce product categories or bbPress forums/topics global image';
			$GLOBALS['gp_title_bg_css'] = ' style="background-image: url(' . $gp['title_bg']['background-image'] . ');"';
		} elseif ( ! empty( $gp['title_bg']['background-image'] ) ) {
			//echo 'Global background image';
			$GLOBALS['gp_title_bg_css'] = ' style="background-image: url(' . $gp['title_bg']['background-image'] . ');"';
		} else {
			//echo 'Empty';		
			$GLOBALS['gp_title_bg_css'] = '';		
		}

		// Parallax effect
		if ( $gp['title_parallax'] == 'enabled' ) {
			$GLOBALS['gp_parse_parallax_scrolling'] = ' data-stellar-background-ratio="0.6"';
			$GLOBALS['gp_parallax_class'] = 'gp-parallax';
		} else {
			$GLOBALS['gp_parse_parallax_scrolling'] = '';
			$GLOBALS['gp_parallax_class'] = '';
		}
		
		// Video header classes
		if ( $GLOBALS['gp_hub_header'] == true ) {
			$GLOBALS['gp_teaser_video_bg'] = get_post_meta( $GLOBALS['gp_post_id'], 'hub_title_teaser_video_bg', true );
			$GLOBALS['gp_full_video_bg'] = get_post_meta( $GLOBALS['gp_post_id'], 'hub_title_full_video_bg', true );
		}
		if ( ! empty( $GLOBALS['gp_teaser_video_bg'] ) OR ! empty( $GLOBALS['gp_full_video_bg'] ) ) { 		
			wp_enqueue_script( 'gp-video-header' );
			$GLOBALS['gp_video_header_class'] = 'video-header';
		} else {
			$GLOBALS['gp_video_header_class'] = '';
		}
		if ( ! empty( $GLOBALS['gp_teaser_video_bg'] ) ) {
			$GLOBALS['gp_teaser_video_header_class'] = 'teaser-video-header';
		} else {
			$GLOBALS['gp_teaser_video_header_class'] = '';
		}

		// Hub page options
		if ( $GLOBALS['gp_hub_header'] == true ) {
			$gp_global = get_option( 'gp' );
			if ( get_post_meta( $GLOBALS['gp_post_id'], 'hub_title', true ) ) {
				$GLOBALS['gp_title'] = get_post_meta( $GLOBALS['gp_post_id'], 'hub_title', true );
			} else {
				$GLOBALS['gp_title'] = $gp_global['hub_title'];
			}				
			$GLOBALS['gp_title_text'] = $gp['hub_title_text'];
			$GLOBALS['gp_subtitle'] = '';
			$GLOBALS['gp_custom_title'] = $gp['hub_custom_title'];
			$GLOBALS['gp_display_site_rating'] = $gp['hub_header_display_rating']['site_rating'];
			$GLOBALS['gp_display_user_rating'] = $gp['hub_header_display_rating']['user_rating'];
			$GLOBALS['gp_user_rating_box'] = redux_post_meta( 'gp', $GLOBALS['gp_post_id'], 'hub_user_rating' ) == 'default' ? $gp_global['hub_user_rating'] : redux_post_meta( 'gp', $GLOBALS['gp_post_id'], 'hub_user_rating' );
			gp_ratings( $GLOBALS['gp_post_id'] );	
			if ( is_page_template( 'hub-template.php' ) ) {
				$GLOBALS['gp_site_rich_snippets'] = true;	
				if ( $GLOBALS['gp_site_rating_enabled'] != true ) {
					$GLOBALS['gp_user_rich_snippets'] = true;
				} else {
					$GLOBALS['gp_user_rich_snippets'] = false;
				}
			}
		}
		
		if ( is_page_template( 'hub-template.php' ) OR $GLOBALS['gp_hub_header'] == true ) {
			$header_class = 'hub-header ';
			$review_header_class = '';
		} elseif ( is_page_template( 'review-template.php' ) ) {
			$header_class = 'hub-header';
			$review_header_class = 'review-header';
		} else {
			$header_class = 'page-header';
			$review_header_class = '';
		}
											
		if ( $GLOBALS['gp_title'] == 'gp-large-title' ) { ?>
	
			<header class="entry-header large-header <?php echo sanitize_html_class( $header_class ); ?> <?php echo sanitize_html_class( $review_header_class ); ?> <?php echo sanitize_html_class( $GLOBALS['gp_parallax_class'] ); ?> <?php echo sanitize_html_class( $GLOBALS['gp_video_header_class'] ); ?> <?php echo sanitize_html_class( $GLOBALS['gp_teaser_video_header_class'] ); ?>"<?php echo wp_kses_post( $GLOBALS['gp_parse_parallax_scrolling'] ); ?><?php echo wp_kses_post( $GLOBALS['gp_title_bg_css'] ); ?>>
											
				<?php if ( $gp['title_bg_top_gradient_overlay'] == 'enabled' && ( ! empty( $GLOBALS['gp_title_bg'] ) OR ! empty( $gp['title_bg']['background-image'] ) OR ! empty( $GLOBALS['gp_teaser_video_bg'] ) ) ) { ?>
					<div class="top-bg-gradient-overlay"></div>
				<?php } ?>
		
				<?php if ( $gp['title_bg_overlay'] == 'enabled' && ( ! empty( $GLOBALS['gp_title_bg'] ) OR ! empty( $gp['title_bg']['background-image'] ) OR ! empty( $GLOBALS['gp_teaser_video_bg'] ) ) ) { ?>
					<div class="bg-overlay-light"></div>
				<?php } ?>

				<?php if ( ! empty( $GLOBALS['gp_teaser_video_bg'] ) OR ! empty( $GLOBALS['gp_full_video_bg'] ) ) {

					// YouTube or Vimeo ID
					$GLOBALS['gp_full_video_bg'] = str_replace( 'www.', '', $GLOBALS['gp_full_video_bg'] );
					if ( preg_match( '/http:\/\/vimeo/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace('http://vimeo.com/', '', $GLOBALS['gp_full_video_bg'] );
						$provider = 'vimeo';
					} elseif ( preg_match( '/https:\/\/vimeo/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace('https://vimeo.com/', '', $GLOBALS['gp_full_video_bg'] );
						$provider = 'vimeo';
					} elseif ( preg_match( '/http:\/\/youtube.com/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace('http://youtube.com/watch?v=', '', $GLOBALS['gp_full_video_bg'] );
						$provider = 'youtube';
					} elseif ( preg_match( '/https:\/\/youtube.com/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace('https://youtube.com/watch?v=', '', $GLOBALS['gp_full_video_bg'] );
						$provider = 'youtube';
					} elseif ( preg_match( '/http:\/\/youtu.be/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace( 'http://youtu.be/', '', $GLOBALS['gp_full_video_bg'] );	
						$provider = 'youtube';		
					} elseif ( preg_match( '/https:\/\/youtu.be/', $GLOBALS['gp_full_video_bg'] ) ) {
						$id = str_replace( 'https://youtu.be/', '', $GLOBALS['gp_full_video_bg'] );	
						$provider = 'youtube';														
					} else {
						$id = $GLOBALS['gp_full_video_bg'];
						$provider = 'html5';
					}
										
					?>
	
					<div class="header-video">
						<span class="header-video-media" data-video-src="<?php echo esc_attr( $id ); ?>" data-teaser-source="<?php echo esc_url( $GLOBALS['gp_teaser_video_bg'] ); ?>" data-provider="<?php echo esc_attr( $provider ); ?>"></span>
						<div class="close-video-button"></div>
					</div>
				
				<?php } ?>
							
				<div class="gp-container">
				
					<?php if ( isset( $GLOBALS['gp_title_text'] ) && $GLOBALS['gp_title_text'] != 'disabled' ) { ?>
					
						<?php if ( $GLOBALS['gp_hub_header'] == true ) {
		
							get_template_part( 'lib/sections/hub', 'header' );

						} elseif ( is_page_template( 'review-template.php' ) ) {
		
							get_template_part( 'lib/sections/review', 'header' );
				
						} elseif ( is_singular( 'gp_portfolio_item' ) ) { ?>
					
							<h1 class="entry-title"><?php echo get_the_title( $gp_post_id ); ?></h1>

							<?php if ( get_post_meta( get_the_ID(), 'portfolio_item_link', true ) ) { ?>
								<a href="<?php echo get_post_meta( get_the_ID(), 'portfolio_item_link', true ); ?>" class="button portfolio-link" target="<?php echo esc_attr( $GLOBALS['gp_link_target'] ); ?>"><?php echo esc_url( $GLOBALS['gp_link_text'] ); ?></a>
							<?php } ?>
							
						<?php } elseif ( is_singular() OR $woocommerce == true OR $buddypress == true OR $bbpress == true ) { ?>

							<?php if ( $woocommerce == true ) {
								$GLOBALS['gp_post_id'] = get_option( 'woocommerce_shop_page_id' ); // Get WooCommerce shop page ID	
							} ?>

							<h1 class="entry-title<?php if ( ! empty( $GLOBALS['gp_subtitle'] ) ) { ?> has-subtitle<?php } ?>"><?php if ( ! empty( $GLOBALS['gp_custom_title'] ) ) { echo esc_attr( $GLOBALS['gp_custom_title'] ); } else { echo get_the_title( $gp_post_id ); } ?></h1>

							<?php if ( ! empty( $GLOBALS['gp_subtitle'] ) ) { ?>
								<h3 class="subtitle"><?php echo esc_attr( $GLOBALS['gp_subtitle'] ); ?></h3>
							<?php } ?>
																																	
						<?php } elseif ( is_search() ) { global $wp_query, $s; ?>
					
							<h1 class="entry-title">
								<?php if ( isset( $_GET['s'] ) && ( $_GET['s'] != '' ) ) { ?>
									<?php echo absint( $wp_query->found_posts ); ?> <?php _e( 'search results for', 'gp_lang' ); ?> "<?php echo esc_html( $s ); ?>"
								<?php } else { ?>
									<?php _e( 'Search', 'gp_lang' ); ?>
								<?php } ?>	
							</h1>
					
						<?php } elseif ( is_category() OR is_tag() OR is_tax() ) { ?>
					
							<h1 class="entry-title"><?php single_cat_title(); ?></h1>
					
						<?php } elseif ( is_author() ) { ?>

							<?php echo get_avatar( get_the_author_meta( 'ID', get_query_var( 'author' ) ), 80 ); ?>
							<div class="author-meta">
								<h1 class="entry-title"><?php echo get_the_author_meta( 'display_name', get_query_var( 'author' ) ); ?></h1>
								<h3 class="subtitle"><?php echo get_the_author_meta( 'description', get_query_var( 'author' ) ); ?></h3>
							</div>
					
						<?php } elseif ( is_archive() ) { ?>
					
							<h1 class="entry-title"><?php if ( ! function_exists( '_wp_render_title_tag' ) && ! function_exists( 'gp_render_title' ) ) { _e( 'Archives', 'gp_lang' ); } else { the_archive_title(); } ?></h1>
							
						<?php } else { ?>
					
							<h1 class="entry-title"><?php bloginfo( 'name' ); ?></h1>
					
						<?php } ?>
					
					<?php } ?>
										
					<?php if ( ! empty( $GLOBALS['gp_full_video_bg'] ) ) { ?>
						<div class="play-video-button-wrapper">
							<a href="<?php echo esc_url( $GLOBALS['gp_full_video_bg'] ); ?>" class="play-video-button"></a>
						</div>	
					<?php } ?><!-- END PLAY VIDEO BUTTON -->
							
				</div>
											
			</header>
			
			<?php if ( $GLOBALS['gp_title'] == 'gp-large-title' ) { get_template_part( 'lib/sections/hub', 'tabs' ); } ?>
		
		<?php } ?>
		
		<?php if ( function_exists( 'yoast_breadcrumb' ) ) { yoast_breadcrumb( '<div id="breadcrumbs"><div class="gp-container">', '</div></div>' ); } ?>
		
		<?php if ( ! empty( $gp['header_ad'] ) ) { ?>
			<div id="header-area">
				<div class="gp-container">
					<?php echo do_shortcode( $gp['header_ad'] ); ?>
				</div>
			</div>			
		<?php } ?>
	
	<?php	
								
	}

}

?>