<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'wp_custom_post_title' ) ) {
	function wp_custom_post_title() {
		if(is_post_type_archive() || is_category() || is_tag() || is_day() || is_month() || is_year() || is_search()) return;
		?>
       <div class="parent-title-wrap">
            <h1 class="parent-title">
                <?php
				$title = single_post_title('', false);
				if($title == ""){
	            	$title = post_type_archive_title('', false);
				}
				echo $title;		
				?>
            </h1>
            <div class="clear"></div>
        </div>
        <?php		        
	}
}

if ( ! function_exists( 'wp_custom_blog_title' ) ) {
	function wp_custom_blog_title() {		
		$title = get_the_title( get_option('page_for_posts', true) );
		if(!$title || !is_home() || is_post_type_archive() || is_category() || is_tag()) return;
		?>
        <div class="parent-title-wrap">
            <h1 class="parent-title">
                <?php 
					echo $title;
				?>
            </h1>
            <div class="clear"></div>
        </div>
        <?php
	}
}

if ( ! function_exists( 'wp_custom_articles_wrap_start' ) ) {
	function wp_custom_articles_wrap_start(){
		?>
       	<div class="articles-wrap articles-wrap-inner">
        <?php
	}
}
if ( ! function_exists( 'wp_custom_articles_wrap_end' ) ) {
	function wp_custom_articles_wrap_end(){
		?>
      		<div class="clear"></div>
       	</div>
        <?php
	}
}

if ( ! function_exists( 'wp_custom_posts_sidebar' ) ) {
	function wp_custom_posts_sidebar(){
		if(!custom_check_for_sidebar()) return;
		global $aria_label_sidebar;
		?>
		<aside id="secondary" class="widget-area" role="complementary" aria-label="<?php echo $aria_label_sidebar ?>">
			<div id="sidebar">
				<?php if ( we_is_post_or_video() && is_active_sidebar( 'sidebar-widget-area' ) ) : ?>
					<ul id="sidebar-widget-area" class="widget-area">
						<?php dynamic_sidebar( 'sidebar-widget-area' ); ?>
					</ul>
				<?php elseif ( we_is_post_or_video(true) && is_active_sidebar( 'sidebar-video-widget-area' ) ) : ?>
					<ul id="sidebar-video-widget-area" class="widget-area">
						<?php dynamic_sidebar( 'sidebar-video-widget-area' ); ?>
					</ul>
				<?php endif; ?>
			</div>
       </aside>
       <?php		
	}
}

if ( ! function_exists( 'wp_custom_page_header' ) ) {
	/**
	 * Display the page header
	 *
	 */
	function wp_custom_page_header() {
		?>
		<header class="entry-header entry-main-title podstran-main-title">
			<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
		</header>
		<?php 
	}
}

if ( ! function_exists( 'wp_custom_page_content' ) ) {
	/**
	 * Display the post content with a link to the single post
	 *
	 * @since 1.0.0
	 */
	function wp_custom_page_content() {		
		global $page_content_links;
		
		/**
		 * Functions hooked to wp_custom_page_content_before_wrap action
		 */
		do_action('wp_custom_page_content_before_wrap');
		?>
		<div class="entry-content-wrap">
			<?php
			/**
			 * Functions hooked to wp_custom_page_content_before_content action
			 */
			do_action('wp_custom_page_content_before_content');
			?>
			<div class="entry-content">
				<?php 
				/**
				 * Functions hooked to wp_custom_page_content_before action
				 */
				do_action('wp_custom_page_content_before');

				custom_subpage_content();
			
				/**
				 * Functions hooked to wp_custom_page_content_after action
				 */
				do_action('wp_custom_page_content_after');

				wp_link_pages( array(
					'before' => '<div class="page-links">' . $page_content_links,
					'after'  => '</div>',
				) );
				?>
			</div>
			<?php
			/**
			 * Functions hooked to wp_custom_page_content_after_content action
			 */
			do_action('wp_custom_page_content_after_content');
			?>
        	<div class="clear"></div>
        </div>		
		<?php
		/**
		 * Functions hooked to wp_custom_page_content_after_wrap action
		 */
		do_action('wp_custom_page_content_after_wrap');
	}
}

if ( ! function_exists( 'wp_custom_page_footer' ) ) {
	function wp_custom_page_footer(){
	}
}

if ( ! function_exists( 'wp_custom_post_header' ) ) {
	/**
	 * Display the post header with a link to the single post
	 *
	 * @since 1.0.0
	 */
	function wp_custom_post_header() {
		?>
			<header class="entry-header">
				<?php			
				if ( 'post' == get_post_type() && !is_archive() && !is_home() && !is_search() ) {
					the_title( '<h1 class="alpha entry-title custom-single-post-title">', '</h1>' );
					wp_custom_author();
					wp_custom_posted_on();
				}
				elseif('videos' == get_post_type()){
					the_title( '<h1 class="alpha entry-title custom-single-post-title">', '</h1>' );					
				}
				elseif(is_archive() || is_category() || is_tag() || is_home() || is_search()){
					the_title( sprintf('<h1 class="alpha entry-title custom-article-post-title"><a href="%s" title="%s">', esc_url( get_permalink() ), esc_url(get_the_title())), '</a></h1>' );
				}
				else {
					the_title( sprintf( '<h2 class="alpha entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );			
				}
				?>
			</header>
		<?php
	}
}

if ( ! function_exists( 'wp_custom_post_content' ) ) {
	/**
	 * Display the post content with a link to the single post
	 *
	 * @since 1.0.0
	 */
	function wp_custom_post_content() {
		global $post_content_links;
		if(get_post_type() == 'videos') return; 
		
		/**
		 * Functions hooked to wp_custom_post_content_before_wrap action
		 */
		do_action('wp_custom_post_content_before_wrap');
		?>
        <div class="entry-content">
            <?php
    
            /**
             * Functions hooked in to wp_custom_post_content_before action.
             */
            do_action( 'wp_custom_post_content_before' );
			
			the_content(
				sprintf(
					__( 'Continue reading %s', '' ),
					'<span class="screen-reader-text">' . get_the_title() . '</span>'
				)
			);
			
            /**
             * Functions hooked in to wp_custom_post_content_after action.
             */
            do_action( 'wp_custom_post_content_after' );
    
            wp_link_pages( array(
                'before' => '<div class="page-links">' . $post_content_links,
                'after'  => '</div>',
            ) );
            ?>
            <div class="clear"></div>
        </div>
        <?php
        /**
         * Functions hooked to wp_custom_post_content_after_wrap action
         */
        do_action('wp_custom_post_content_after_wrap');
        ?>
        <div class="clear"></div>
		<?php
	}
}

if ( ! function_exists( 'wp_custom_post_excerpt' ) ) {
	function wp_custom_post_excerpt(){
		global $post_excerpt_readmore;
		if(get_post_type() == 'videos') return;
		
		/**
         * Functions hooked to wp_custom_post_excerpt_before_wrap action
         */
        do_action('wp_custom_post_excerpt_before_wrap');
        ?>
		<div class="entry-content entry-excerpt">
			<?php
			/**
			 * Functions hooked to wp_custom_post_excerpt_before action
			 */
			do_action('wp_custom_post_excerpt_before');

			//the_excerpt();
			$readmore = !is_singular() ? '<a href="'.get_permalink().'" class="post-readmore">'.$post_excerpt_readmore.'</a>' : "";
			custom_excerpt_max_charlength(140, false);
			echo $readmore;
			
			/**
			 * Functions hooked to wp_custom_post_excerpt_after action
			 */
			do_action('wp_custom_post_excerpt_after');
			?>
			<div class="clear"></div>
		</div>
		<?php
		/**
         * Functions hooked to wp_custom_post_excerpt_after_wrap action
         */
        do_action('wp_custom_post_excerpt_after_wrap');
	}
}

if ( ! function_exists( 'wp_custom_post_thumbnail' ) ) {
	/**
	 * Display post thumbnail
	 *
	 * @var $size thumbnail size. thumbnail|medium|large|full|$custom
	 * @uses has_post_thumbnail()
	 * @uses the_post_thumbnail
	 * @param string $size the post thumbnail size.
	 * @since 1.5.0
	 */
	 /*
	 $title = get_post(get_post_thumbnail_id())->post_title; //The Title
	 $caption = get_post(get_post_thumbnail_id())->post_excerpt; //The Caption
	 $description = get_post(get_post_thumbnail_id())->post_content; // The Description
	 
	 , $home = false
	 */
	function wp_custom_post_thumbnail( $size = 'medium', $echo = true, $outside_img_id = 0 ) {
		
		$id = get_the_ID();
		/*if(is_home()){
			$id = get_option('page_for_posts');
		}*/
		if($outside_img_id > 0){
			$id = $outside_img_id;
		}		
		if ( !has_post_thumbnail($id) || is_search() ) return;
		
		$image_orientation = wp_custom_image_orientation($id, $size);
		
		if(is_single()){
			$size = 'fp-blog';
		}
		elseif(is_home() || is_archive() || is_category() || is_tag()){
			$size = 'big-blog';
		}
		
		$url = get_permalink($id);
		$title = get_post(get_post_thumbnail_id($id))->post_title; //The Title

		$image = '<div class="entry-image-wrap">';
		if(!is_page()) $image .= '<a href="'.$url.'" class="entry-image-link" title="'.$title.'" '.(is_single()?'data-rel="lightbox"':'').'>'; 
		$image .= get_the_post_thumbnail($id, $size, $image_orientation ); 
		if(!is_page()) $image .= '</a>';
		$image .= '</div>';		
		
		if($echo)
			echo $image;
		else
			return $image;
	}
}

if ( ! function_exists( 'wp_custom_image_orientation' ) ) {
	function wp_custom_image_orientation($post_id, $size = 'post-thumbnail')
	{
		if($size == "") $size = 'medium';
		$orientation_classes = array('class' => "attachment-".$size." size-".$size." lazyload");
		if(!$post_id)
			return $orientation_classes;
			
		$tn_id = get_post_thumbnail_id($post_id);
	  	$img = wp_get_attachment_image_src( $tn_id, 'full' );
	  	$width = $img[1];
	  	$height = $img[2];
		if ($width > $height){
			$orientation_classes = array('class'  => "attachment-".$size." size-".$size." wp-custom-landscape lazyload");
		} else {
			$orientation_classes = array('class'  => "attachment-".$size." size-".$size." wp-custom-portrait lazyload");
		}
		return $orientation_classes;
	}
}

if ( ! function_exists( 'wp_custom_post_footer' ) ) {
	function wp_custom_post_footer(){		
		?>
        <footer>
            <div class="entry-footer clearfix">
                <div class="entry-footer-wrap">
                    <?php wp_custom_page_footer(); ?>
                </div>                
            </div>
        </footer>
    <?php		
	}
}

if ( ! function_exists( 'wp_custom_posted_on' ) ) {
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 */
	function wp_custom_posted_on($label = true, $format = 'F Y', $echo = true) {
		global $posted_on_label;
		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
		
		if(!$format || $format == ''){
			$format = 'F Y';
		}
		
		$time_string = sprintf( $time_string,
			esc_attr( get_the_date( 'c' ) ),
			esc_html( get_the_date( $format ) )
		);
		
		$label_html = '<span class="entry-date-label">'.$posted_on_label.'</span>';
		
		$posted = wp_kses( apply_filters( 'wp_custom_single_post_posted_on_html', '<span class="posted-on entry-date">' . ($label?$label_html:'') . $time_string . '</span>', $time_string ), array(
			'span' => array(
				'class'  => array(),
			),
			'a'    => array(
				'href'  => array(),
				'title' => array(),
				'rel'   => array(),
			),
			'time' => array(
				'datetime' => array(),
				'class'    => array(),
			),
		) );
		
		if($echo)
			echo $posted;
		else
			return $posted;
	}
}

if ( ! function_exists( 'wp_custom_author' ) ) {
	/**
	 * Prints HTML with meta information for the current post author.
	 */
	function wp_custom_author()
	{
		$author = CFS()->get('blog_author');
		if($author) echo '<div class="entry-author">'.__("By <strong>$author</strong> - ", 'weexplain').'</div>';
	}
}

if( !function_exists( 'wp_custom_posts' ) )
{
	function wp_custom_posts()
	{
		?>
		<div class="article-wrap entry-post">
			<?php
			if(get_post_type() == 'videos'){				
				wp_custom_post_video(true);				
			}
			else {
				wp_custom_post_thumbnail();
				wp_custom_post_header();
				wp_custom_post_excerpt();
				wp_custom_post_footer();
			}
			?>
			<div class="clear"></div>
		</div>		
	   <?php
	}
}

if( !function_exists( 'get_last_entry' ) )
{
	function get_last_entry($post_type = 'post', $orderby = 'date', $limit = 1, $sidebar = false, $additional = '')
	{
		$html = '';
		
		$naslov = get_the_title();
		$ajdi = get_the_ID();
		
		$shortcode_video = false;
		
		if($post_type == 'blog'){
			$post_type = 'post';
		}
		elseif($post_type == 'video_shortcode'){
			$post_type = 'videos';
			$shortcode_video = true;
			$html .= check_video_page();
			if(is_front_page()) $shortcode_video = false;			
		}
		$class = (is_front_page())?'fp-':"";
		$img_class = $class;
		if($sidebar){
			$img_class = 'fp-';
		}
		$ul_class = 'clearfix entry-ul entry-ul-'.$class.$post_type;
		if($sidebar){
			$ul_class = 'sidebar-'.$class.$post_type.' entry-ul-sidebar-'.$class.$post_type;
		}
		if(is_array($additional) && array_key_exists('sitemap', $additional))
			$ul_class .= ' ul-sitemap';
		
		global $post;
		$args = array(
			'post_type' => $post_type,
			'post_status' => 'publish',
			'posts_per_page' => $limit,			
			'orderby' => $orderby,
			'paged' => (get_query_var('paged')?get_query_var('paged'):1),
		);
		if($post_type == 'post' && $sidebar && is_singular('post')){
			// check if on single post and exclude from posts
			$args['post__not_in'] = array($ajdi);
		}
		if($post_type == 'testimonials' && get_query_var('tpage')){		
			$post_ids = array(get_query_var('tpage'));
			$post_ids_query = new WP_Query( array (
				'orderby' => $orderby,
				'fields' => 'ids',
				'suppress_filters' => true,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
				'posts_per_page' => ($limit>-1?($limit-1):$limit),				
			));
			$all_post_ids = array_merge($post_ids,$post_ids_query->posts);
				
			$args['post__in'] = $all_post_ids;
			$args['orderby'] = 'post__in';
		}
		if(is_array($additional) && array_key_exists('sitemap', $additional)){			
			$args['post__not_in'] = $additional['sitemap'];
		}
		
		$all_posts = new WP_Query($args);
		$c = 0;
		$s = $l = $t = 1;
		$others = '';
		
		$total_posts = $all_posts->found_posts;
		
		if($all_posts->have_posts()){
			if(is_front_page() && $post_type == 'logos'){
				$html .= '<div id="logoSlideshow" class="fp-logos-slideshow logos-slideshow cycle-slideshow" 
				data-cycle-fx="carousel"
				data-cycle-timeout="4000"
				data-cycle-carousel-visible=8
				
				>';
			}
			elseif(!is_front_page() && $post_type == 'testimonials'){
				$html .= '<div class="testimonials-subpage-wrap">';
				$html .= '<div class="testimonials-slideshow cycle-slideshow" 
				data-cycle-timeout="6000"
				data-cycle-slides="> div.testimonials-wrap"
				data-cycle-speed="600"
				data-cycle-pause-on-hover="true"
				data-cycle-auto-height="container"
				>
					<div class="cycle-prev"><span></span></div>
					<div class="cycle-next"><span></span></div>';
			}
			elseif(!is_front_page() && $post_type == 'logos'){
				$html .= '<div class="logo-subpage-slideshow logos-slideshow cycle-slideshow" 
				data-cycle-timeout="4000"
				data-cycle-slides="> div.logo-wrap"
				data-cycle-pager=".logos-pager"
				data-cycle-auto-height="calc"
				>';
				if(!is_front_page()){
					$html .= '<div class="logo-wrap">';	
				}				
			}
			else {
				if($t == 1 && $post_type == 'team'){
					$html .= '<div class="wp-custom-blue">';
				}
				$html .= '<ul class="'.$ul_class.'">';
			}
			while($all_posts->have_posts()){
				$all_posts->the_post();
				
				if($post_type == 'post'){					
					$html .= '<li>';
					if(is_array($additional) && array_key_exists('sitemap', $additional)){						
						$html .= '<a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">'.get_the_title().'</a>';
					}
					else {
						$image_orientation = wp_custom_image_orientation(get_the_ID(), $img_class.'blog');
						$html .= '<div class="'.$class.'blog-single-wrap">
								<div class="'.$class.'blog-img">
									<a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">'.get_the_post_thumbnail(get_the_ID(), ''.$img_class.'blog', $image_orientation).'</a>
								</div>';
						if($sidebar){
							$html .= '<div class="blog-info">';
						}
						$blog_tag = is_front_page()?'h3':'h1';
						$html .= '<'.$blog_tag.' class="'.$class.'blog-title">
									<a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">'.get_the_title().'</a>
								</'.$blog_tag.'>';
						if(!$sidebar){
							$html .= '<div class="'.$class.'blog-content">
									'.custom_excerpt_max_charlength(135).'
								</div>';
						}
						if($sidebar){
							$html .= wp_custom_posted_on(false, 'F Y', false);
						}
						$html .= '<a href="'.get_permalink().'" class="'.$class.'blog-readmore" title="'.esc_attr(get_the_title()).'">'.__('Read more', 'weexplain').'</a>';
						if($sidebar){
							$html .= '</div>';
						}
						$html .= '<div class="clear"></div></div>';
					}
					$html .= '</li>';				
				}
				elseif($post_type == 'testimonials'){
					$company_link = CFS()->get('testimonials_website');
					$company = CFS()->get('testimonials_company');
					if(is_front_page()){
						$html .= '<li>
							<div class="'.$class.'testimonials-wrap">
								<div class="'.$class.'testimonials-content">
									<div class="'.$class.'testimonials-content-inner">
										<a href="'.add_query_arg('tpage', get_the_ID(), get_permalink(get_page_by_path('services-different-types-explainer-video-animations/testimonials'))).'#testimonial">
										'.custom_excerpt_max_charlength(100).'
										</a>
									</div>
								</div>
								<div class="'.$class.'testimonials-title">
									'.get_the_title().'
								</div>
								<div class="'.$class.'testimonials-company">
									'.($company_link?'<a href="'.$company_link.'" title="'.esc_attr($company).'">':'').'
									'.$company.'
									'.($company_link?'</a>':'').'
								</div>
							</div>
						</li>';
					}
					else {						
						$testimonial = '<div class="'.$class.'testimonials-wrap">
							<div class="testimonials-content-wrap">
								<div class="testimonials-content-wrap-inner">
									<div class="'.$class.'testimonials-content">
										<div class="'.$class.'testimonials-content-inner">
											'.get_the_content().'
										</div>
									</div>									
								</div>							
							</div>
							<div class="testimonials-info">
								<div class="'.$class.'testimonials-title">
									'.get_the_title().'
								</div>
								<div class="'.$class.'testimonials-company">
									'.($company_link?'<a href="'.$company_link.'" title="'.esc_attr($company).'">':'').'
									'.$company.'
									'.($company_link?'</a>':'').'
								</div>
							</div>
						</div>';
						if(get_query_var('tpage')){
							if(get_query_var('tpage') != "" && get_the_ID() == get_query_var('tpage')){
								// get selected testimonial
								$html .= '<a name="testimonial"></a>';
								$html .= $testimonial;
							}
							else {
								$others .= $testimonial;						
							}							
						}
						else {
							$html .= $testimonial;							
						}
					}
						
				}
				elseif($post_type == 'videos'){
					if(is_array($additional) && array_key_exists('sitemap', $additional)){
						$html .= '<li><a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">'.get_the_title().'</a></li>';
					}
					else {
						if($shortcode_video){
							$html .= wp_custom_post_video();
						}
						else {
							$video_data = explode('=', CFS()->get('videos_url'));
							if(isset($video_data[1])){
								$html .= '<li><div class="video-wrap"><div class="video-left">';
								$html .= '<div class="video-iframe-height">'.do_shortcode('[lyte id="'.$video_data[1].'" /]').'</div>';							
								$html .= '</div></div></li>';
							}
						}
					}
				}
				elseif($post_type == 'logos'){					
					if($l%9==0 && !is_front_page()){
						$html .= '</div><div class="logo-wrap">';
					}
					$image_orientation = wp_custom_image_orientation(get_the_ID(), 'logos');
					$html .= get_the_post_thumbnail(get_the_ID(), 'logos', $image_orientation);
					$l++;
				}
				elseif($post_type == 'steps'){
					// we're on front page					
					$steps_tag = (is_front_page())?'h3':'h2';					
					$image_orientation = wp_custom_image_orientation(get_the_ID(), 'thumbnail');
					
					$html .= '<li>'.get_the_post_thumbnail(get_the_ID(), 'thumbnail', $image_orientation).'<br><div class="steps-text"><'.$steps_tag.'>'.get_the_title().'</'.$steps_tag.'>'.(!is_front_page()?'<div class="steps-content">'.get_the_content().'</div>':'').'</div></li>';
				}
				elseif($post_type == 'team'){
					$image_orientation = wp_custom_image_orientation(get_the_ID(), 'thumbnail');
					$html .= '<li>'.get_the_post_thumbnail(get_the_ID(), 'thumbnail', $image_orientation).'<br><div class="steps-text"><h2>'.get_the_title().'</h2><div class="steps-content">'.get_the_content().'</div></div></li>';
					
					if($t==3&&$total_posts>3){
						$html .= '</ul>';
						$html .= '</div>';
						$html .= '</div>';
						if($additional != ""){
							$img_id = attachment_url_to_postid( $additional );
							if($img_id){
								$html .= '<div class="parallax-wrap-outer we_parallax_shortcode_img_last">'.do_shortcode('[nk_awb awb_type="image" awb_stretch="true" awb_image="'.$img_id.'" awb_image_size="full" awb_image_background_size="cover" awb_image_background_position="0 0" awb_parallax="scroll" awb_parallax_speed="0.5" awb_parallax_mobile="true"]<div class="parallax-overlay"><div class="parallax-overlay-inner"></div></div>[/nk_awb]').'<div class="clear"></div></div>';
							}														
						}
						$html .= '<div class="other-team-members-wrap">';						
						$html .= '<ul class="'.$ul_class.' other-team-members">';
					}
					$t++;					
				}
				elseif($post_type == 'faq'){
					$html .= '<li><div class="tab"><input id="tab-'.$c.'" type="checkbox" name="tabs">
					<label for="tab-'.$c.'">'.get_the_title().'</label>
					<div class="tab-content">'.get_the_content().'</div></div></li>';
					$c++;
				}
			}
			if($shortcode_video) $html .= wp_pagenavi(array('query' => $all_posts, 'echo' => false));
			if($post_type == 'testimonials' && get_query_var('tpage') != "") $html .= $others;
			
			if(is_front_page() && $post_type == 'logos' || !is_front_page() && in_array($post_type, array('testimonials','logos'))){
				if($post_type == 'logos'&&!is_front_page()){
					$html .= '</div>';
				}
				$html .= (!in_array($post_type, array('logos', 'testimonials'))?'<div class="clear"></div>':'').'</div>';
				if($post_type == 'logos'&&!is_front_page()){
					$html .= '<div class="logos-pager"></div>';
				}
			}
			else {
				$html .= '</ul>';												
			}
			if(!is_front_page() && $post_type == 'testimonials'){
				$html .= '</div>';
			}
		}
		wp_reset_postdata();
		return $html;
	}
}

function custom_subpage_content(){	
	the_content();
}

function we_page_after_main()
{
	if(is_page_template('page-templates/services.php')){
		we_services();
	}
	else {
		if(!is_front_page()){
			echo we_parallax_shortcode();
			echo we_after_main_content();
			echo we_parallax_shortcode(false);
		}	
	}		
}

function we_parallax_shortcode($first = true){
	$html = $text_html = $title = $href = "";
	
	//$key = ($first?'we_parallax_shortcode_img_first':'we_parallax_shortcode_img_last');	
	$key = ($first?'we_parallax_img_first':'we_parallax_img_last');
	$img = CFS()->get($key);
	
	if($img != ""){
		if(!$first){			
			// get fields if last image
			$text = CFS()->get('we_parallax_img_last_text');
			$url = CFS()->get('we_parallax_img_last_url');
			$readmore = CFS()->get('we_parallax_img_last_readmore');			
			if($text != "" && $url != "" && $readmore != ""){
				$title = '<h2>'.$text.'</h2>';
				$href = '<a href="'.$url.'" class="page-readmore" title="'.esc_html(esc_attr($text)).'">'.$readmore.'</a>';
			}			
		}
		
		if(!$first && $title != "" && $href != '' || $first){
			// generate awb shortcode with adjustments
			$shortcode = '[nk_awb awb_type="image" awb_stretch="true" awb_image="'.$img.'" awb_image_size="full" awb_image_background_size="cover" awb_image_background_position="0 0" awb_parallax="scroll" awb_parallax_speed="0.5" awb_parallax_mobile="true"]';
			$shortcode .= '<div class="parallax-overlay">';
			$shortcode .= '<div class="parallax-overlay-inner">';
			if(!$first){
				$shortcode .= $title;
				$shortcode .= $href;
			}
			$shortcode .= '</div>';
			$shortcode .= '<div class="clear"></div></div>';
			$shortcode .= '[/nk_awb]';			
			
			$html = '<div class="parallax-wrap-outer '.$key.'">';			
			$html .= do_shortcode($shortcode);
			$html .= '<div class="clear"></div></div>';	
		}		
	}
	return $html;
}

function we_after_main_content()
{
	$html = '';
	$text = CFS()->get('we_parallax_text');	
	$blue_background = CFS()->get('we_parallax_blue_background');
	if($text != ""){
		$html .= '<div class="subpage-entry-wrap">';
		if($blue_background){
			$html .= '<div class="wp-custom-blue"><div class="wp-custom-subpage-wrap clearfix">';
		}		
		$html .= do_shortcode($text);
		if($blue_background){
			$html .= '</div></div>';
		}		
		$html .= '<div class="clear"></div></div>';
	}	
	return $html;
}

function custom_check_for_sidebar()
{
	if(is_active_sidebar( 'sidebar-widget-area' ) && we_show_sidebar() ) {		
		return true;
	}
	return false;
}

function show_page_footer()
{
	if(is_front_page()){
		return true;
	}
	return false;
}

// opposite of page footer
function we_show_sidebar(){	
	if(is_page('explainer-videos') || is_singular(array('post','videos')) || is_archive() || is_home() || is_category() || is_tag() || is_date() || is_year() || is_month() || is_day()){		
		return true;
	}
	return false;
}

function we_is_post_or_video($is_video = false)
{
	if($is_video)
	{
		// check if is video
		if(is_singular('videos') || is_tax('videos_category') || is_page('explainer-videos')){			
			return true;
		}		
	}
	else {
		if(is_category() || is_tag() || is_singular('post') || is_post_type_archive('post') || is_home() || is_date() ){
			// check if is post			
			return true;
		}
	}
	return false;
}

function custom_widget_shortcode($sidebar_name){
	ob_start();
    if ( is_active_sidebar( ''.$sidebar_name.'-widget-area' ) ) :
		echo '<div class="'.$sidebar_name.'-widget-wrap">';
		dynamic_sidebar( ''.$sidebar_name.'-widget-area' );
		echo '</div>';
	endif;
    return ob_get_clean();
}

function custom_social_buttons($social = array('facebook', 'twitter', 'googleplus'), $turl = false, $share_title = 'Share on', $return = false) // array('title' => '', 'url' => '')
{
	//array('facebook', 'twitter', 'googleplus', 'google', 'google+')
	if(empty($social)) return;
	
	$title = urlencode( html_entity_decode( get_the_title(), ENT_COMPAT, 'UTF-8' ) );
	$url = urlencode( get_permalink() );
	if($turl != false && is_array($turl)){
		$title = isset($turl['title'])?$turl['title']:"";
		$url = isset($turl['url'])?$turl['url']:"";
	}

	if($return) ob_start();
	?>
	<div class="custom-social-sharing-buttons">
       	<div class="cssb-title"><?php _e(esc_html($share_title), ''); ?></div>
       	<br />
        <?php foreach($social as $o) {
        	switch($o) {
				case 'twitter':
        			?><a rel="external nofollow" class="cssb cssb-twitter" href="https://twitter.com/intent/tweet/?text=<?php echo $title; ?>&url=<?php echo $url; ?>" target="_blank">
						<span class="cssb-icon cssb-icon-twitter"></span>
						<span class="cssb-icon-title"><?php echo $o; ?></span>
					</a> <?php
        		break;

        		case 'facebook':
				// https://www.facebook.com/sharer/sharer.php?s=100&p[url]=<?php echo $url; &p[title]=<?php echo $title;
        			?><a rel="external nofollow" class="cssb cssb-facebook" href="https://www.facebook.com/sharer.php?u=<?php echo $url; ?>" target="_blank" >
						<span class="cssb-icon cssb-icon-facebook"></span>
						<span class="cssb-icon-title"><?php echo $o; ?></span>
       				</a> <?php
        		break;

        		case 'googleplus':
        		case 'google':
        		case 'google+':
				
        			?><a rel="external nofollow" class="cssb cssb-googleplus" href="https://plus.google.com/share?url=<?php echo $url; ?>" target="_blank" >
						<span class="cssb-icon cssb-icon-googleplus"></span>
						<span class="cssb-icon-title"><?php echo $o; ?></span>
					</a> <?php
        		break;
        	}
        } ?>
    </div>
   <?php
  	if($return){		
		return ob_get_clean();
	}
}

function social_icons()
{
	$widget = custom_widget_shortcode('social');
	$html = '';
	if($widget){
		$html = '<div class="social-icons-wrap">
		<div class="social-icons">';
		$html .= $widget;
		$html .= '</div></div>';	
	}	
	return $html;
	// <a href="https://www.facebook.com/WeExplain1/" target="_blank" class="socials fb">Facebook</a>
	// <a href="https://www.linkedin.com/company/we-explain" target="_blank" class="socials linkedin">LinkedIn</a>
	// <a href="https://www.youtube.com/channel/UCcjx2NAPdULe5ZmJ9BHsLjg/featured" target="_blank" class="socials youtube">Youtube</a>		
	// <a href="" class="socials fb">Instagram</a>	
}

if(!function_exists('wp_custom_breadcrumbs')){
	function wp_custom_breadcrumbs()
	{
		if(is_front_page()) return;
		
		if(function_exists('bcn_display')){
			?>
			<div class="breadcrumbs-wrap">
				<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
					<?php bcn_display(); ?>
				</div>
			</div>
			<?php
		}
	}
}

function we_newsletter(){	
	$newsletter = custom_widget_shortcode('newsletter');
	if($newsletter != ""){
		return '<div class="'.(is_front_page()?'fp-':'sidebar-').'newsletter">'.$newsletter.'</div>';
	}
	return '';
}

function we_follow_us(){
	$social = custom_widget_shortcode('social');
	if($social != ""){
		return '<div class="'.(is_front_page()?'fp-':'sidebar-').'social">'.$social.'</div>';
	}
	return '';
}

function we_other_posts(){	
	return get_last_entry('blog', 'rand', 2, true);
}

function we_videos($limit = -1, $orderby = 'date', $post_type = 'video_shortcode'){
	return get_last_entry($post_type, $orderby, $limit);
}

function we_tag_cloud($format = 'flat'){
	return wp_tag_cloud( array(
   		'smallest' => 1, // size of least used tag
   		'largest'  => 1, // size of most used tag
   		'unit'     => 'em', // unit for sizing the tags
   		'number'   => 12, // displays at most 45 tags
   		'orderby'  => 'name', // order tags alphabetically
		'order'    => 'ASC', // order tags by ascending order
   		'taxonomy' => 'post_tag', // you can even make tags for custom taxonomies
		'echo'		=> false,
		'format'	=> $format,
		//'separator' => " | \n",
		'separator' => "\n",
	) );
}

function we_video_embed()
{	
	if(get_post_type() != 'videos') return;
	
	$html = '<div class="video-embed-wrap">';
	$html .= wp_custom_post_video(false, true);
	$html .= '<div class="clear"></div></div>';	
	
	echo $html;
}

function we_show_tags()
{
	$posttags = get_the_tags();
	$tags = array();
	if ($posttags) {
		echo '<div class="blog-tags">';
		echo __('Tags: ', 'weexplain');
	  	foreach($posttags as $tag) {
			$tags[] = '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name . '</a>'; 
	  	}
		echo implode(', ', $tags);
		echo '</div>';
	}
}

function we_video_categories($post_id, $taxonomy = 'videos_category', $link = true)
{	
	if(!$post_id) return;
	
	$terms = get_the_terms($post_id, $taxonomy);
	$html = '';	
	if(is_array($terms) && count($terms)>0){
		foreach($terms as $term){
			if($link){
				$html .= '<a class="single-term" href="'.get_term_link($term->slug, $taxonomy).'" title="'.esc_attr($term->name).'">';				
			}
			$html .= '<span class="single-term">'.$term->name.'</span>';
			if($link){
				$html .= '</a>';
			}
		}
	}	
	return $html;
}

function wp_custom_post_video($echo = false, $nowrap = false){	
	$html = '';	
	$video_title = CFS()->get('videos_company');
	if(!$echo){
		if(!$nowrap)
			$html .= '<li><a name="'.sanitize_title(get_the_title()).'"></a>';
	}
	$html .= '<div class="video-wrap"><div class="video-left">';
	$video_data = explode('=', CFS()->get('videos_url'));	
	$html .= '<div class="video-iframe-height">'.do_shortcode('[lyte id="'.$video_data[1].'" /]').'</div>';
	$html .= '<div class="video-right"><h2 class="videos-company">'.$video_title.'</h2>';
	if(!$nowrap) $html .= '<h3 class="videos-title">'.get_the_title().'</h3>';	
	$html .= '<div class="videos-categories">'.we_video_categories(get_the_ID()).'</div>';
	$html .= '<div class="videos-applications">'.we_video_categories(get_the_ID(), 'videos_applications', false).'</div>';
	$html .= '</div></div></div>';	
	if(!$echo){
		if(!$nowrap)
			$html .= '</li>';
	}
	
	if($echo)
		echo $html;
	else
		return $html;
}

function check_video_page(){
	return "<script>
	var tag = document.createElement('script');
	tag.src = \"https://www.youtube.com/iframe_api\";
	var firstScriptTag = document.getElementsByTagName('script')[0];
	firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

	//var lyte_host = '".(is_front_page()?'https://www.youtube.com':'https://www.youtube-nocookie.com')."';
	var lyte_host = 'https://www.youtube-nocookie.com';
	//var lyte_host = 'https://www.youtube.com';
	var players = [];			

	jQuery(document).ready(function($){
		$(document).on('DOMSubtreeModified', '.lyte-wrapper', function() {
			var lyte_iframe = $(this).find('iframe');
			if(lyte_iframe.length){
				lyte_iframe.each(function(){
					var lyframe = $(this);
					//var lyframe = lyte_iframe;
					var lyte_id = lyframe.attr('id');
					var lyte_src_api = lyframe.attr('src')+'&enablejsapi=1&origin=https://www.weexplain.com';
					lyframe.attr('src', lyte_src_api);

					players.push(new YT.Player(lyte_id, {
						videoId: lyte_id,
						host: lyte_host,
						playerVars: { 'wmode':'opaque' },
						events: {
							'onReady': function (event) {
								// set volume to 70%
								event.target.setVolume(70);
							},
							'onStateChange': function(event) {
								if (event.data == YT.PlayerState.PLAYING) {
									$.each(players, function(k, v) {
										//checks for only players that are playing
										if (this.getPlayerState() == YT.PlayerState.PLAYING
											  && this.getIframe().id != event.target.getIframe().id) { 
											this.pauseVideo();
										}
									});
								}
							}
						}
					}));
				});
			}
		});	
	});

	window.onYouTubeIframeAPIReady = function() {
		jQuery('.promo-video .lyte-wrapper iframe').ready(function(){	
			jQuery('.lyte-remove').remove();		
		});
	};
	</script>";
}

function custom_sitemap(){
	$html = '';
	
	$html .= '<div class="html-sitemap">';
	$html .= '<h2>'.__('Pages', 'weexplain').'</h2>';
	$html .= '<ul class="ul-sitemap">';
	// Add pages you'd like to exclude in the exclude here 	
	$exclude_pages = array(9);
	
	$menu_object = wp_get_nav_menu_items( 'main' );		
	$menu_parents = array();
	if(count($menu_object)>0){
		foreach($menu_object as $object){
			$id = get_post_meta( $object->ID, '_menu_item_object_id', true );
			if($object->menu_item_parent == 0){
				$menu_parents[] = $id;					
			}
			$single = get_post($id);				
			// list only pages
			if($single->post_type == 'page'){
				//if(!in_array($object->menu_item_parent, $menu_parents) && $object->menu_item_parent > 0){						
				//	$html .= '<ul class="children">';
				//}
				$single_id = $single->ID;
				$exclude_pages[] = $single_id;
				$html .= '<li'.((!in_array($object->menu_item_parent, $menu_parents) && $object->menu_item_parent > 0)?' class="sitemap-menu-child"':"").'><a href="'.get_permalink($single_id).'" title="'.esc_attr(esc_html(get_the_title($single_id))).'">'.$object->title.'</a></li>';
			}
		}	
	}
	$html .= wp_list_pages(
		array( 
			'exclude' => implode(",",$exclude_pages),
    		'title_li' => '',
			'echo' => false,
  		)
	);
	$html .= '</ul>';
	$html .= '<h2>'.__('Blog', 'weexplain').'</h2>';
	$exclude_posts = array();
	$html .= get_last_entry('blog', 'date', -1, false, array('sitemap' => $exclude_posts));
	$html .= '<h2>'.__('Blog categories', 'weexplain').'</h2>';
	$cat_args = array(
		'title_li' => '',
		'echo' => false,
	); 
	// We wrap it in unordered list 
	$html .= '<ul class="ul-sitemap">';
	$html .= wp_list_categories($cat_args);

	// Add categories you'd like to exclude in the exclude here
	//$cats = get_categories('exclude=');
	//foreach ($cats as $cat) {
	//	$html .= "<li><h3>".$cat->cat_name."</h3></li>";
  	//}
	$html .= "</ul>";
	$html .= '<h2>'.__('Tags', 'weexplain').'</h2>';
	$html .= we_tag_cloud('list');
	$html .= '<h2>'.__('Explainer videos', 'weexplain').'</h2>';
	$exclude_videos = array();
	$html .= get_last_entry('videos', 'date', -1, false, array('sitemap' => $exclude_videos));
	$html .= '<h2>'.__('Video categories', 'weexplain').'</h2>';
	$html .= do_shortcode('[we-arhiv kategorija="videos_category"]');
	//$html .= '<h2>'.__('Video applications', 'weexplain').'</h2>';
	$html .= '</div>';
	
	return $html;
}

function remove_youtube_showinfo( $html, $url, $args ) {	
	return str_replace( '?feature=oembed', '?feature=oembed&showinfo=0&rel=0&enablejsapi=1&origin=https://www.weexplain.com', $html );	
}

function we_services()
{
	$rows = CFS()->get('we_services_row');
	$i = 0;
	if($rows){		
		foreach($rows as $row){
			$title = ($row['we_services_row_title'] != "" ? '<h2>'.$row['we_services_row_title'].'</h2>' : '');
			$content = $row['we_services_row_content'];
			$slika = $row['we_services_row_img'];
			?>
			<div class="services-wrap<?php echo ($i%2==0?' wp-custom-blue':' services-wrap-right'); ?>">
				<?php 
				if($slika != ""){ 
					$img_atts = wp_get_attachment_image_src($slika, 600);
					if($img_atts){
				?>
				<div class="entry-img" style="background-image: url(<?php echo $img_atts[0]; ?>);"></div>
				<?php } } ?>
				<div class="entry-services">
					<div class="entry-services-inner-wrap">
						<div class="entry-services-inner">
							<?php echo $title; ?>
							<?php echo $content; ?>
						</div>
					</div>
				</div>
				<div class="clear"></div>
			</div>
			<?php
			$i++;
		}
	}	
}

// define the post_thumbnail_html callback 
function filter_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) { 
    $html = str_replace('src=', 'data-src=', $html);
    return $html; 
}; 