<?php 

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if( !function_exists('hotelgalaxy_add_home_slider') ){
	
	add_action('hotelgalaxy_do_homeSlider','hotelgalaxy_add_home_slider');

	function hotelgalaxy_add_home_slider(){

		if( defined( 'HG_SLIDER_ADDON_VERSION' ) ){

			do_action('hotelgalaxy_get_premium_slider');

		}else{

			hotelgalaxy_construct_slider_one();
		}
		
	}	
}


if( ! function_exists('hotelgalaxy_construct_slider_one')){

	function hotelgalaxy_construct_slider_one(){

		$slider_details = hotelgalaxy_get_slider_details();		

		if( count( $slider_details ) > 0 ){
			?>

			<div id="hg-main-slider" <?php hotelgalaxy_add_class( 'slider_layout' ) ?>>				

				<?php 

				foreach( $slider_details as $index=>$slide ){
					?>

					<div class="item">

						<?php echo $slide['image']; ?>

						<div class="container">
							<div class="carousel-caption">
								<?php 

								echo wp_kses_post( wpautop( $slide['title'] ) ); 	

								echo wp_kses_post( wpautop( $slide['excerpt'] ) );

								?>
							</div>
						</div>
					</div>

					<?php
				}

				?>

			</div>

			<?php
		}
	}
}


if ( ! function_exists( 'hotelgalaxy_get_slider_details' ) ) {


	function hotelgalaxy_get_slider_details() {

		$output = array();

		$page_ids = array();

		for ( $i = 1; $i <= 5 ; $i++ ) {

			$page_id = get_theme_mod( "Page_slider_$i" );

			if ( absint( $page_id ) > 0 ) {

				$page_ids[] = absint( $page_id );
			}
		}

		if ( empty( $page_ids ) ) {
			return $output;
		}

		$args = array(
			'posts_per_page' => count( $page_ids ),
			'orderby'        => 'post__in',
			'post_type'      => 'page',
			'post__in'       => $page_ids,
			'post_status' => 'publish',
			'meta_query'     => array(
				array( 'key' => '_thumbnail_id' ),
			),
		);

		$posts = get_posts( $args );	

		$c = 0;

		foreach ( $posts as $post ) {		

			if ( has_post_thumbnail( $post->ID ) ) {

				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

				$output[$c]['image'] = sprintf('<img class="img-responsive" src="%1$s" title="%2$s" alt="%3$s" />',
					esc_url( $image[0] ),
					esc_html( $post->post_title),
					esc_html( $post->post_title)											
				);

				$output[$c]['title']     = 	sprintf('<h2>%1$s</h2>', $post->post_title);

				$output[$c]['excerpt']   = hotelgalaxy_trim_words($post->ID, $post->post_content );

				$c++;
			}
		}

		return $output;
	}

}

if(!function_exists('hotelgalaxy_trim_words')){	

	function hotelgalaxy_trim_words( $id, $content, $URL=null, $is_readmore = true ){			

		$slider_btn = hotelgalaxy_get_option('slider_button_text');		
		$excerpt_word = hotelgalaxy_get_option('slider_excerpt_word');		
		$target = hotelgalaxy_get_option('slider_button_target') ? '_blank' : '_self';		

		$args= '';
		
		if( !empty( $slider_btn ) && absint( $is_readmore ) == true){

			$args = apply_filters('hotelgalaxy_slider_more_button', sprintf(

				'<footer style="margin-bottom:20px;"><a id="read-more" href="%1$s" target="%2$s"><i class="icon"></i>%3$s</a></footer>',

				esc_url( (empty($URL)) ? get_permalink( $id ) : $URL ),

				esc_attr( $target ),

				esc_html( $slider_btn )

			) );

		}

		$html_trim = wp_trim_words( strip_shortcodes( wp_strip_all_tags( $content ) ), $excerpt_word );

		$html_out = sprintf('<p>%1$s</p>%2$s',$html_trim, $args);

		return $html_out;
	}
}


if( !function_exists('hotelgalaxy_slider_indicators') ){

	function hotelgalaxy_slider_indicators(){

		$page_ids = array();

		for ( $i = 1; $i <= 5 ; $i++ ) {

			$page_id = get_theme_mod( "Page_slider_$i" );

			if ( absint( $page_id ) > 0 ) {

				$page_ids[] = absint( $page_id );
			}
		}

		if( count( $page_ids ) > 1 ){

			echo '<ol class="carousel-indicators">';

			foreach ($page_ids as $index => $pageId) {

				$active = ( $index==0 ) ? 'active' : '';

				print '<li data-target="#carousel-example-generic" data-slide-to="'.$index.'" class="'.$active.'"></li>';
			}

			echo '</ol>';

		}

	}
}


?>
