<?php

if ( !function_exists( 'shrk_get_section_data_attr' ) ) {
	function shrk_get_section_data_attr() {
		$output = array();
		$data_attr = array();

		if ( shrk_get_meta_option( 'section_fg_color' ) ) {
			$output[] = 'color:' . esc_attr( shrk_get_meta_option( 'section_fg_color' ) ) . ';';
		}
		
		if ( 'customheight' == shrk_get_meta_option( 'section_height' ) ) {
			if ( shrk_get_meta_option( 'custom_window_height_val' ) ) {
				$data_attr[] = 'data-section-height="' . esc_attr( ( shrk_get_meta_option( 'custom_window_height_val' ) ) ) . '"';
			}
		}
		
		$retval = '';
		if ( !empty($output) ) {
			$retval .= 'style="' . implode(' ',$output) . '" ';
		}

		if ( !empty($data_attr) ) {
			$retval .= implode(' ',$data_attr).' ';	
		}

		return $retval;
	}
}

if ( !function_exists( 'shrk_get_section_cont_attr' ) ) {
	function shrk_get_section_cont_attr() {
		if ( shrk_get_meta_option( 'custom_section_padding' ) !== false && ( 'windowheight' == shrk_get_meta_option( 'section_height' ) || 'customheight' == shrk_get_meta_option( 'section_height' ) ) ) {
			return 'style="padding-top:' . esc_attr( shrk_get_meta_option( 'custom_section_padding' ) ) . '; padding-bottom:' . esc_attr( shrk_get_meta_option( 'custom_section_padding' ) ) . ';"';
		}
	}
}

if ( !function_exists( 'shrk_get_section_class' ) ) {
	function shrk_get_section_class() {
		global $post;
		$output = '';

		if ( 'windowheight' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'windowheight'.' ';
		} elseif ( 'no-padding' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'no-padding'.' ';
		} elseif ( 'no-y-padding' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'no-y-padding'.' ';			
		} elseif ( 'small-padding' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'small-padding'.' ';
		} elseif ( 'medium-padding' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'medium-padding'.' ';
		} elseif ( 'large-padding' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'large-padding'.' ';
		} elseif ( 'customheight' == shrk_get_meta_option( 'section_height' ) ) {
			$output .= 'customheight'.' ';
		}

		if ( shrk_get_meta_option( 'section_v_align' ) && ('windowheight' == shrk_get_meta_option( 'section_height' ) || 'customheight' == shrk_get_meta_option( 'section_height' ) ) ) {
			
			/**
			 * FIXME
			 * display table + table-cell causes swiper-container to take full width of children which causes problematic resizing.
			 */
			$output .= 'section-v-' . shrk_get_meta_option( 'section_v_align' ) . ' ';
			
		}

		if ( 'on' == shrk_get_meta_option('section_full_width') ) {
			$output .= 'full-width-content'.' ';     
		}

		if ( shrk_get_meta_option('section_color_scheme') ) {
			$output .= shrk_get_meta_option( 'section_color_scheme' ).' ';     
		}

		if ( shrk_get_meta_option('section_shadow') ) {
			//shadow-upper-lower
			$output .= 'shadow-' . shrk_get_meta_option( 'section_shadow' ).' ';     
		}

		if ( shrk_get_meta_option('section_extra_class') ) {
			$output .= shrk_get_meta_option( 'section_extra_class' ).' ';     
		}

		return $output;
	}
}

if ( !function_exists( 'shrk_get_section_backgrounds' ) ) {
	function shrk_get_section_backgrounds() {

		$image_style = '';
		$output = '<div class="section-background-wrapper">';

		/**
		 * Solid Color
		 */
		if ( shrk_get_meta_option( 'section_bg_color' ) ) {
			$output .= '<div class="section-background color" style="background-color:' . esc_attr( shrk_get_meta_option( 'section_bg_color' ) ) . ';"></div>';
		}

		/**
		 * Gradient
		 */
		if ( shrk_get_meta_option( 'section_bg_grad_from' ) && shrk_get_meta_option( 'section_bg_grad_to' ) ) {
			$color_bg_grad_from = shrk_get_meta_option( 'section_bg_grad_from' );
			$color_bg_grad_to   = shrk_get_meta_option( 'section_bg_grad_to' );

			$output .= '<div class="section-background gradient" style="';
			$output .= 'background-color:' . esc_attr( $color_bg_grad_to ) . ';';
			$output .= 'background: -moz-radial-gradient(center center,circle cover,' . esc_attr( $color_bg_grad_from ) . '  0%, ' . esc_attr( $color_bg_grad_to ) . ' 100%);';
			$output .= 'background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,' . esc_attr( $color_bg_grad_from ) . ' ), color-stop(100%,' . esc_attr( $color_bg_grad_to ) . '));';
			$output .= 'background: -webkit-radial-gradient(center center,circle cover,  ' . esc_attr( $color_bg_grad_from ) . '  0%,' . esc_attr( $color_bg_grad_to ) . ' 100%);';
			$output .= 'background: -o-radial-gradient(center center,circle cover,  ' . esc_attr( $color_bg_grad_from ) . '  0%,' . esc_attr( $color_bg_grad_to ) . ' 100%);';
			$output .= 'background: -ms-radial-gradient(center center,circle cover,  ' . esc_attr( $color_bg_grad_from ) . '  0%,' . esc_attr( $color_bg_grad_to ) . ' 100%);';
			$output .= 'background: radial-gradient(center center,circle cover,  ' . esc_attr( $color_bg_grad_from ) . '  0%,' . esc_attr( $color_bg_grad_to ) . ' 100%);';
			$output .= '"></div>';
		}

		/**
		 * Pattern overlay
		 */
		if ( shrk_get_meta_option( 'section_image_overlay' ) ) {
			$image_style = 'background-image: url(\'' . esc_url( wp_get_attachment_url( shrk_get_meta_option( 'section_image_overlay' ) ) ) . '\'); background-size: auto; background-repeat: repeat;';
			$output .= '<div class="section-background pattern-overlay" style="' . $image_style . '"></div>';
		}

		/**
		 * Image background
		 */
		if ( shrk_get_meta_option( 'section_image' ) ) {
			$image_classes     = $image_data_attr = '';
			$image_pos_arr     = shrk_pagesections_meta_arr( 'background_image_pos_css' );
			$section_image_url = wp_get_attachment_url( shrk_get_meta_option( 'section_image' ) );
			$section_image_id  = shrk_get_meta_option( 'section_image' );
			$section_image_ar  = shrk_get_image_aspect( $section_image_id, 'shrk_section_bg' );
			$srcset            = !empty( $section_image_id ) ? wp_get_attachment_image_srcset( $section_image_id, 'shrk_section_bg' ) : '';

			if ( $section_image_id ) {
				$section_image_src = wp_get_attachment_image_src( $section_image_id, 'shrk_section_bg' );
				$section_image_url = $section_image_src[0];
			}

			if ( !empty( $srcset ) && !empty( $section_image_url ) && 'on' == shrk_get_meta_option('section_image_lazyload') ) {
				$image_data_attr .= ' data-image-bg="' . esc_url( $section_image_url ) . '"';
				$image_data_attr .= ' data-image-ar="' . esc_attr( $section_image_ar ) . '"';
				$image_data_attr .= ' data-srcset="' . esc_attr( $srcset ) . '"';
				$image_classes   .= ' lazyload-bg lazyload-on-visible';     
			}

			// Setup parallax
			if ( 'parallax' == shrk_get_meta_option( 'section_image_scroll_pos' ) ) {
				wp_enqueue_script( 'stellar.js' ); 
				$image_classes .= ' parallax-bg';

				if ( shrk_get_meta_option( 'section_image_parallax_speed' ) ) { $image_data_attr .= ' data-stellar-background-ratio="' . (intval(shrk_get_meta_option( 'section_image_parallax_speed' ))/100) . '"'; }
				if ( shrk_get_meta_option( 'section_image_parallax_offset' ) ) { $image_data_attr .= ' data-stellar-vertical-offset="' . (intval(shrk_get_meta_option( 'section_image_parallax_offset' ))) . '"'; }
			}

			if ( empty( $srcset ) || 'on' != shrk_get_meta_option('section_image_lazyload') ) {
				$image_style = 'background-image: url(\'' . esc_url( $section_image_url ) . '\');';
			}

			if ( shrk_get_meta_option( 'section_image_size' ) ) { $image_style .= ' background-size: ' . esc_attr( shrk_get_meta_option( 'section_image_size' ) ) . ';'; }
			if ( shrk_get_meta_option( 'section_image_repeat' ) ) { $image_style .= ' background-repeat: ' . esc_attr( shrk_get_meta_option( 'section_image_repeat' ) ) . ';'; }
			if ( shrk_get_meta_option( 'section_image_pos' ) && isset( $image_pos_arr[shrk_get_meta_option( 'section_image_pos' )] ) ) { $image_style .= ' background-position: ' . esc_attr( $image_pos_arr[shrk_get_meta_option( 'section_image_pos' )] ) . ';'; }
			if ( shrk_get_meta_option( 'section_image_scroll_pos' ) ) { 
				if ( shrk_get_meta_option( 'section_image_scroll_pos' ) == 'scroll' ) { $image_style .= ' background-attachment: scroll;'; }
				if ( shrk_get_meta_option( 'section_image_scroll_pos' ) == 'fixed' ) { $image_style .= ' background-attachment: fixed;'; }
			}

			$output .= '<div class="section-background image ' . esc_attr( $image_classes ) . '" style="' . $image_style . '" ' . $image_data_attr . '></div>';
		}

		/**
		 * Video background
		 */
		if ( shrk_get_meta_option( 'section_video_mp4' ) || shrk_get_meta_option( 'section_video_webm' ) ) {
			$tmp_w  = $tmp_h = '';
			$section_video_mp4_url   = shrk_get_meta_option( 'section_video_mp4' ) ? wp_get_attachment_url( shrk_get_meta_option( 'section_video_mp4' ) ) : '';
			$section_video_webm_url  = shrk_get_meta_option( 'section_video_webm' ) ? wp_get_attachment_url( shrk_get_meta_option( 'section_video_webm' ) ) : '';
			$section_video_fb_url    = shrk_get_meta_option( 'section_video_fallback_image' ) ? wp_get_attachment_url( shrk_get_meta_option( 'section_video_fallback_image' ) ) : '';
			$section_video_autostart = shrk_get_meta_option( 'section_video_autostart' ) == 'on' ? true : false;

			if ( shrk_get_meta_option( 'section_video_attributes' ) ) {
				$tmp = json_decode( shrk_get_meta_option( 'section_video_attributes' ), true );
				$tmp_w = $tmp['width'];
				$tmp_h = $tmp['height'];
			}
			
			$output .= '    <div class="section-background video ' . ( $section_video_autostart ? 'autostart' : '' ) . '">
			<video width="' . esc_attr( $tmp_w ) . '" height="' . esc_attr( $tmp_h ). '" autoplay loop poster="' . $section_video_fb_url . '" >';
				if ( !empty( $section_video_mp4_url ) ) { $output .= '<source src="' . esc_url( $section_video_mp4_url ) . '" type="video/mp4">'; }
				if ( !empty( $section_video_webm_url ) ) { $output .= '<source src="' . esc_url( $section_video_webm_url ) . '" type="video/webm">'; }
				if ( !empty( $section_video_fb_url ) ) { $output .= '<img src="' . esc_url( $section_video_fb_url ) . '" class="video-fallback-image" />'; }
				$output .= '</video>
			</div>'; 
		}
		
		/**
		 * Slider background
		 */
		$tmp = shrk_get_meta_option( 'section_slideshow' );
		if ( $tmp != '' && $tmp != 'default' && function_exists( 'shrk_getRevSlider' ) ) {
			$revslider_class = '';
			if ( shrk_get_meta_option( 'section_slideshow_nomouse' ) == 'on' ) {
				$revslider_class .= ' nomouse';
			}

			$output .= '<div class="section-background revslider ' . esc_attr( $revslider_class ) . '">';
			$output .= shrk_getRevSlider( $tmp );
			$output .= '</div>';
		}
		
		$output .= '</div> <!-- end section-background-wrapper -->';
		
		return $output;
		
	}
}

if ( !function_exists( 'shrk_get_the_section_title' ) ) {
	function shrk_get_the_section_title() {
		global $post;

		if ( 'on' == shrk_get_meta_option( 'section_show_title', $post->ID ) ) {
			echo '<h1 class="section-title"><span>' . get_the_title( $post ) . '</span></h1>';	
		}
	}
}

if ( !function_exists( 'shrk_get_section_edit_link' ) ) {
	function shrk_get_section_edit_link( $postID = null ) {
		if ( is_user_logged_in() && current_user_can('edit_pages') && !empty ( $postID ) ) {
			$edit_section_link = get_edit_post_link( $postID );

			if ( !empty ( $edit_section_link )) {
				return '<div class="section-edit-link"><a target="_blank" href="' . esc_url( $edit_section_link ) . '">' . esc_html__('Edit Section', 'dvl-pagesections') . '</a></div>';
			}
		}
	}
}

?>