<?php 
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if (!class_exists('VC_PW_text')) {
	class VC_PW_text extends WPBakeryShortCode_VC_Posts_Grid {
		public  $pw_title,
				$pw_query,
				$pw_link_target,
				$pw_post_layout,
				$pw_text_carousel_min_slides,
				$pw_text_carousel_slide_width,
				$pw_text_align,
				$pw_carousel_pre_view,
				$pw_excerpt_length,
				$pw_speed,
				$pw_slider_hide_pagination_control,
				$pw_slider_hide_prev_next_buttons,
				$pw_slider_loop,
				$pw_hide_date,
				$pw_date_format,

				$pw_back_color,
				
				
				$pw_title_font_family,
				$pw_title_font_size,
				$pw_link_color,
				$pw_link_hover_color,
				$pw_meta_font_family,
				$pw_meta_font_size,
				$pw_meta_color,
				$pw_excerpt_font_family,
				$pw_excerpt_font_size,
				$pw_excerpt_color,
				$pw_txtcar_id	
				;
				
		function __construct($pw_title,
							$pw_query,
							$pw_link_target,
							$pw_post_layout,
							$pw_text_carousel_min_slides,
							$pw_text_carousel_slide_width,	
							$pw_text_align,
							$pw_carousel_pre_view,
							$pw_excerpt_length,
							$pw_speed,
							$pw_slider_hide_pagination_control,
							$pw_slider_hide_prev_next_buttons,
							$pw_slider_loop,
							$pw_hide_date,
							$pw_date_format,
			
							$pw_back_color,
							
							$pw_title_font_family,
							$pw_title_font_size,
							$pw_link_color,
							$pw_link_hover_color,
							$pw_meta_font_family,
							$pw_meta_font_size,
							$pw_meta_color,
							$pw_excerpt_font_family,
							$pw_excerpt_font_size,
							$pw_excerpt_color
				
						) {
			$this->pw_title =$pw_title;
			$this->pw_query =$pw_query;
			$this->pw_link_target =$pw_link_target;
			$this->pw_post_layout=$pw_post_layout;
			
			$this->pw_text_carousel_min_slides=$pw_text_carousel_min_slides;
			$this->pw_text_carousel_slide_width=$pw_text_carousel_slide_width;

			$this->pw_text_align=$pw_text_align;
			$this->pw_carousel_pre_view=$pw_carousel_pre_view;
			$this->pw_excerpt_length=$pw_excerpt_length;
			$this->pw_speed=$pw_speed;
			$this->pw_slider_hide_pagination_control=$pw_slider_hide_pagination_control;
			$this->pw_slider_hide_prev_next_buttons=$pw_slider_hide_prev_next_buttons;
			$this->pw_slider_loop=$pw_slider_loop;	
			$this->pw_hide_date=$pw_hide_date;
			$this->pw_date_format=$pw_date_format;																														
			
			$this->pw_back_color = $pw_back_color;
			
			$this->pw_title_font_family=$pw_title_font_family;
			$this->pw_title_font_size=($pw_title_font_size==0 ? 14:$pw_title_font_size);
			$this->pw_link_color = $pw_link_color;
			$this->pw_link_hover_color = $pw_link_hover_color;
			$this->pw_meta_font_family=$pw_meta_font_family;
			$this->pw_meta_font_size=($pw_meta_font_size==0 ? 12:$pw_meta_font_size);
			$this->pw_meta_color = $pw_meta_color;
			$this->pw_excerpt_font_family=$pw_excerpt_font_family;
			$this->pw_excerpt_font_size=($pw_excerpt_font_size==0 ? 11:$pw_excerpt_font_size);
			$this->pw_excerpt_color = $pw_excerpt_color;
			$this->pw_front_end();
			
			$this->pl_txtcarousel_custom_font();
			$this->pl_txtcarousel_custom_color();
		}
		
		function pw_front_end()
		{
			global $VCExtendAddonClass_CAROUSEL,$output;
			$loop=$this->pw_query;
			$grid_link = $grid_layout_mode = $title = $filter= '';
			$posts = array();
			if(empty($loop)) return;
			$this->getLoop($loop);
			$my_query = $this->query;
			$args = $this->loop_args;
			$img_id=array();
			$images='';
			$contents='';
			$this->pw_txtcar_id = $rand_id = rand(4000,5000);
			$output .= '<h2 class="pl-itemtitle">'.$this->pw_title.'</h2>';
			$output .= '<ul class="pl-bxslider pl-car-car pl-carousel-layout" id="txtslider_'.$rand_id.'" >';
			$img_counter = 0;
			$images='';
			$contents='';
			
			while ( $my_query->have_posts() ) {
				$my_query->the_post(); // Get post from query
				$post = new stdClass(); // Creating post object.
				$post->id = get_the_ID();
				$post->link = get_permalink($post->id);
				$this->setLinkTarget($this->pw_link_target);
				
				$post_type = get_post_type( $post->id );
				$post_taxonomies = get_object_taxonomies($post_type);
				$tax_counter = 0;
				
				$output .= '<li class="pl-col">
								<div class="pl-car-item">';
									$output .= '<div class="pl-detailcnt">
												<h4 class="pl-title '.$this->pw_text_align.'-txt">';
												if (isset($this->pw_hide_date) && ($this->pw_hide_date=='off')){
													$output .= '<span class="pl-date">'. get_the_date($this->pw_date_format).'</span>';
												}
												$output .= '<a href="'. $post->link .'" target="'. $this->pw_link_target  .'" >'. get_the_title().'</a></h4>
											</div>
										 <div class="pl-detailcnt">
											<div class="pl-postmeta '.$this->pw_text_align.'-txt">';

									$output .= pw_get_post_meta($post->id , $post_taxonomies);
											$output.= '</div>
										</div>';
									$output .= '<div class="pl-detailcnt">
												<p class="pl-text '.$this->pw_text_align.'-txt">'. $VCExtendAddonClass_CAROUSEL->excerpt(get_the_excerpt(),$this->pw_excerpt_length).'</p>
										  </div>';
									
			   $output .=  '</div>
			   			</li>';
				
				
			}
			wp_reset_query();
			$output .= '</ul>';
			$output .="<script type='text/javascript'>
            /* <![CDATA[  */ 
                jQuery(document).ready(function() {
					
					slider" . $rand_id ." =
					 jQuery('#txtslider_" . $rand_id ."').bxSlider({ 
						  mode : '".($this->pw_post_layout=='vertical_carousel_text' ? 'vertical' : 'horizontal' )."' ,
						  touchEnabled : true ,
						  adaptiveHeight : true ,
						  slideMargin : 10 , 
						  wrapperClass : 'pl-bx-wrapper pl-car-car ' ,
						  infiniteLoop:".($this->pw_slider_loop=='yes'?'true':'false') .",
						  pager:".($this->pw_slider_hide_pagination_control=='yes'?'false':'true').",
						  controls:".($this->pw_slider_hide_prev_next_buttons=='yes'?'false':'true').",".
						  ($this->pw_post_layout=='horizontal_carousel_text' ? 'slideWidth:'.$this->pw_text_carousel_slide_width.',' : 'slideWidth:5000,' ).
						  ($this->pw_post_layout=='horizontal_carousel_text' ? 'minSlides:'.$this->pw_text_carousel_min_slides.',' : 'minSlides:'.$this->pw_carousel_pre_view.',' )."
						  maxSlides: ". $this->pw_carousel_pre_view.",
						  moveSlides: 1,
						  auto: true,
						  pause : ". $this->pw_speed."	,
						  autoHover  : true , 
 						  autoStart: true
					 });
					 jQuery('.pl-bx-wrapper .pl-bx-controls-direction a').click(function(){
						  slider" . $rand_id .".startAuto();
					 });
					 jQuery('.pl-bx-pager a').click(function(){
						 var i = jQuery(this).data('slide-index');
            			 slider" . $rand_id .".goToSlide(i);
						 slider" . $rand_id .".stopAuto();
						 restart=setTimeout(function(){
							slider" . $rand_id .".startAuto();
							},1000);
						 return false;
					 });
                });	
            </script>";	
		}
		
		function pl_txtcarousel_custom_font() {
			wp_enqueue_style('pw-pl-custom-font-family', PW_PS_PL_URL_CAROUSEL . '/css/custom.css', array() , null); 
			
			$imported_font = $title_family = $meta_family = $excerpt_family = array('inherit'); 
	
			if ($this->pw_title_font_family!='inherit') {
				$imported_font[] = $this->pw_title_font_family; 
				$title_family = explode(':',str_replace('+',' ',$this->pw_title_font_family));
			} 
			if ($this->pw_meta_font_family!='inherit') {
				$imported_font[] = $this->pw_meta_font_family; 
				$meta_family = explode(':',str_replace('+',' ',$this->pw_meta_font_family));
			} 
			if ($this->pw_excerpt_font_family!='inherit') {
				$imported_font[] = $this->pw_excerpt_font_family; 
				$excerpt_family = explode(':',str_replace('+',' ',$this->pw_excerpt_font_family));
			} 
			$imported_font= array_filter(array_unique($imported_font));
			$sep='|';$font_family='';
			foreach ( $imported_font as $font ){
				if ($font_family==''){$sep='';}
				if ($font!='inherit')
					$font_family .= $sep . $font;
				$sep='|';
			}
			$custom_css='';
			if (($font_family!='inherit') && ($font_family!='')){
				$custom_css .= '
						@import url(http://fonts.googleapis.com/css?family='. $font_family.');';
			}
			
			wp_add_inline_style( 'pw-pl-custom-font-family', $custom_css );
		}
		function pl_txtcarousel_custom_color() {
			
			wp_enqueue_style('pw-pl-custom-style', PW_PS_PL_URL_CAROUSEL . '/css/custom.css', array() , null); 
			
			$imported_font = $title_family = $meta_family = $excerpt_family = array('inherit'); 
	
			if ($this->pw_title_font_family!='inherit') {
				$imported_font[] = $this->pw_title_font_family; 
				$title_family = explode(':',str_replace('+',' ',$this->pw_title_font_family));
			} 
			if ($this->pw_meta_font_family!='inherit') {
				$imported_font[] = $this->pw_meta_font_family; 
				$meta_family = explode(':',str_replace('+',' ',$this->pw_meta_font_family));
			} 
			if ($this->pw_excerpt_font_family!='inherit') {
				$imported_font[] = $this->pw_excerpt_font_family; 
				$excerpt_family = explode(':',str_replace('+',' ',$this->pw_excerpt_font_family));
			} 
			$imported_font= array_filter(array_unique($imported_font));
			$sep='|';$font_family='';
			foreach ( $imported_font as $font ){
				if ($font_family==''){$sep='';}
				if ($font!='inherit')
					$font_family .= $sep . $font;
				$sep='|';
			}
			
			$box_back_color = $this->pw_back_color;
			$link_color = $this->pw_link_color;
			$link_hover_color = $this->pw_link_hover_color;
			$meta_color = $this->pw_meta_color;
			$excerpt_color = $this->pw_excerpt_color;
			
			
			$custom_css = '
				
				#txtslider_'.$this->pw_txtcar_id.' .pl-title {
					';
					$custom_css .=($this->pw_title_font_family!='inherit')?'font-family:"'.$title_family[0].'";':'';
					$custom_css .='  
					font-size:'.$this->pw_title_font_size.'px;
				}
				#txtslider_'.$this->pw_txtcar_id.' .pl-title .pl-date ,#txtslider_'.$this->pw_txtcar_id.' .pl-postmeta {
					';
					$custom_css .=($this->pw_meta_font_family!='inherit')?'font-family:"'.$meta_family[0].'";':'';
					$custom_css .='  
					font-size:'.$this->pw_meta_font_size.'px;
				}
				#txtslider_'.$this->pw_txtcar_id.' .pl-text {
					';
					$custom_css .=($this->pw_excerpt_font_family!='inherit')?'font-family:"'.$excerpt_family[0].'";':'';
					$custom_css .='  
					font-size:'.$this->pw_excerpt_font_size.'px;
				}
				
				#txtslider_'.$this->pw_txtcar_id.' li {
					background : '.$box_back_color.';
				}
				#txtslider_'.$this->pw_txtcar_id.' .pl-title a  {
					color : '.$link_color.';
				} 
					#txtslider_'.$this->pw_txtcar_id.' .pl-title a:hover   {
						color : '.$link_hover_color.';
					} 
				#txtslider_'.$this->pw_txtcar_id.' .pl-title .pl-date  {
					background : '.$link_hover_color.';
				} 
				#txtslider_'.$this->pw_txtcar_id.' .pl-postmeta , #txtslider_'.$this->pw_txtcar_id.' .pl-postmeta a {
					color : '.$meta_color.';
				}
					#txtslider_'.$this->pw_txtcar_id.' .pl-postmeta a:hover   { 
						background:'.$link_hover_color.'; 
						color:#fff;
					}
				#txtslider_'.$this->pw_txtcar_id.' .pl-text , #txtslider_'.$this->pw_txtcar_id.' .pl-text span {
					color : '.$excerpt_color.';
				}
				';
			
			wp_add_inline_style( 'pw-pl-custom-style', $custom_css );
		}		
		
	}
}

?>
