<?php
if ( ! function_exists( 'wp_body_open' ) ) {

	/**
	 * Shim for wp_body_open, ensuring backward compatibility with versions of WordPress older than 5.2.
	 *
	 * @since Twenty Twenty 1.0
	 */
	function wp_body_open() {
		/** This action is documented in wp-includes/general-template.php */
		do_action( 'wp_body_open' );
	}
}
function texas_setup() {
	
	$lang = get_template_directory(). '/languages';
	load_theme_textdomain('texas', $lang);
	

	get_template_part('/custom/inc');		
	get_template_part('/custom/theme-config');
				
	get_template_part('/share/breadcrumb-trail');
	get_template_part('/share/tgm-plugin');
			
	if ( !function_exists('texas_themes_pro')) { 			
    	require_once( trailingslashit( get_template_directory() ) . 'share/customizer-pro/class-customize.php' );
	}
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	
	add_theme_support('post-thumbnails');
	$args = array();
	$header_args = array( 
	    'default-image'          => '',
		'default-repeat' => 'no-repeat',
        'default-text-color'     => '2C2C2C',
		'url'                    => '',
        'width'                  => 1920,
        'height'                 => 89,
        'flex-height'            => true,
     );
	add_theme_support( 'custom-background', $args );
	add_theme_support( 'custom-header', $header_args );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support('woocommerce');
		
	register_nav_menus(
					   array(
						'header-menu' => __( 'Header Menu', 'texas' ) 
					   )
					   );	
	
	
	add_theme_support( "title-tag" );
	
	add_theme_support( 'custom-logo', array(
			   'height'      => 64,
			   'width'       => 256,
			   'flex-width' => true,
			) );	
	
	
	add_editor_style("css/editor-style.css");

}
add_action( 'after_setup_theme', 'texas_setup' );


function texas_check_kirki() {
	return is_plugin_active( 'kirki/kirki.php' );
}	
	
function texas_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'texas_content_width', 1170 );
}
add_action( 'after_setup_theme', 'texas_content_width', 0 );	

function texas_theme_stylesheets() {
	$theme_info = wp_get_theme();

	wp_enqueue_style('texas-base',  get_template_directory_uri() .'/css/base.css', false, '1.0.1', false);	
	wp_enqueue_style('bootstrap',  get_template_directory_uri() .'/css/bootstrap/css/bootstrap.css', false, '3.3.7', false);
	wp_enqueue_style('font-awesome',  get_template_directory_uri() .'/css/font-awesome/css/font-awesome.css', false, '4.7.0', false);

	if(is_front_page()&& !is_home() ){
		wp_enqueue_style('animate',  get_template_directory_uri() .'/custom/css/animate.css', false,"3.5.1", false);
	}		
	wp_enqueue_style('texas-main', get_stylesheet_uri(), array(), $theme_info->get( 'Version' ) );
}

add_action( 'wp_enqueue_scripts', 'texas_theme_stylesheets' );


function texas_styles_method() {
	wp_enqueue_style(
		'live-style',
		get_template_directory_uri() . '/css/custom_script.css'
	);
	
	$custom_css = '';

	//header css
	
	get_template_part('/share/Mobile_Detect');
	$detect = new Mobile_Detect;	
	if ( $detect->isMobile() ) {
		$custom_css .='.mobile-nav-ico{ display: block;}#search-toggle a.search-icon,.nav-wrap{ display: none;}	.main-nav,.search-wrap,.nav-wrap{height: auto;}.mobile-nav-ico,.search-wrap,.logo-container{ float:none; width:100%; text-align:center;}.search-main{ display:block;}#search-toggle .search-main {position: relative;left: 0;top: 0;	height: 36px;transform: translateY(-50%);opacity: 1;transition: opacity 0.2s ease-in-out, visibility 0.2s ease-in-out;margin:30px auto 10px auto;}.mobile-nav-ico{ color:#00c3b6; cursor:pointer;}#search-toggle  .search-form label {width: 100%;}.mobile-nav-ico {display: block;}#gotoTop {  bottom: 30px;    margin-right: 20px;}';
	}

	if ( get_header_textcolor() !='' && get_header_textcolor()!= '2C2C2C' ){
	
		$custom_css .='.blogdescription,.blogname,.nav-wrap li>a,.nav-wrap ul.menu li.menu-item-has-children ul li.menu-item-has-children a:after, .nav-wrap ul.menu li.menu-item-has-children a:after,#search-toggle a.search-icon{color:#'.esc_attr(get_header_textcolor()).';}';	

	}else{
		
		
		$custom_css .='.blogdescription,.blogname,.nav-wrap li>a,.nav-wrap ul.menu li.menu-item-has-children ul li.menu-item-has-children a:after, .nav-wrap ul.menu li.menu-item-has-children a:after,#search-toggle a.search-icon{color:#2C2C2C;}';			
		
		}
	
	if ( get_background_color() !=''  ){
		$custom_css .='html body{background-image:none;}';	
	}
	
	$header_image = get_header_image();	
	//echo '-----888-------';		
	if ( ! empty( $header_image ) ){

		$custom_css .='header {
			background-image: url('.esc_url($header_image).');	background-repeat: no-repeat;background-position: 50% 50%;	-webkit-background-size: cover;	-moz-background-size:  cover;
			-o-background-size:      cover;	background-size:         cover;
		}
		.main-nav,.nav-wrap ul.menu li.menu-item-has-children { background-color:transparent;}
		@media screen and (min-width: 59.6875em) {
			body:before {
				background-image: url('.esc_url($header_image).');	background-repeat: no-repeat;background-position: 100% 50%;	-webkit-background-size: cover;	-moz-background-size: cover;
				-o-background-size:  cover;	background-size:   cover;		border-right: 0;
			}
		}';	

	}			
	
	//siderbar			
	if( get_theme_mod( 'siderbar_layout','right-sidebar') == 'left-sidebar'){$custom_css .= '.main-body .col-md-8{float: right;}';}

	// Has the text been hidden?
	 if ( ! display_header_text() ){
	
		$custom_css .= '
		 .logo-text{
			 display: none;
		 clip: rect(1px, 1px, 1px, 1px);
		 position: absolute;
		 }
		';
	 }
	//slider css	
	$i=1;
	$key = 'slider';
	
	$default_content = texas_section_content_default($key);
  
	$slider_title_typography_default     = array(
		'font-family'    => 'Roboto',
		'variant'        => '100',
		'font-size'      => '56px',
		'color'          => '#ffffff',
		'text-transform' => 'Uppercase',
		'text-align'     => 'center'
	);
	$slider_description_typography_default     = array(
		  'font-family'    => 'Roboto',
		  'variant'        => 'regular',
		  'font-size'      => '20px',
		  'color'          => '#ffffff',
		  'text-transform' => 'none',
		  'text-align'     => 'center'
	  );	
	$slider_button_typography_default     = array(
		  'font-family'    => 'Roboto',
		  'variant'        => '300',
		  'font-size'      => '20px',
		  'color'          => '#ffffff',
		  'text-transform' => 'Uppercase',
		  'text-align'     => 'center'
	  );
	  
	$slider_title_typography = texas_get_typography( 'slider_title_typography', $slider_title_typography_default );
	$slider_description_typography = texas_get_typography( 'slider_description_typography', $slider_description_typography_default );  
	$slider_button_typography = texas_get_typography( 'slider_button_typography', $slider_button_typography_default );  
	
	$slider_button_background = get_theme_mod( 'slider_button_background', '#0066ff' );  
	
	$slider_button_background_hover = texas_change_color($slider_button_background,0.8);;
  
  
	$j=0;
	//$custom_css ='';  
  
	$custom_css .=".ct_slider_warp .carousel-caption h1.slider_title{ $slider_title_typography font-weight: lighter;}.ct_slider_warp .carousel-caption p.ct_slider_text{  $slider_description_typography font-weight: lighter;}.ct_slider .ct_slider_warp  a.btn{ $slider_button_typography background-color: $slider_button_background; border-color:$slider_button_background_hover;border-radius: 4px;font-weight: lighter; }.ct_slider .ct_slider_warp a:hover.btn{ background-color:$slider_button_background_hover;}";
  
	
	$repeater_value = get_theme_mod( 'repeater_slider',$default_content);	
	if ( ! empty( $repeater_value ) ) :	
	  foreach ( $repeater_value as $row ) : 
		if ( isset( $row[ 'slider_image' ] ) && !empty( $row[ 'slider_image' ] ) ) :
		  $image_id = wp_get_attachment_url( $row[ 'slider_image' ] );
		  if ( $image_id )
		  {
			$slide_image = esc_url( $image_id );
		  } else {
			$slide_image = esc_url( $row[ 'slider_image' ] );
		  }
  
		  $custom_css .=".ct_slider_item_".($j+1)."{background-image: url(".$slide_image.");background-size:auto 100%;background-position: center;}.ct_slider_item_".($j+1).":after {content: '';position: absolute;width: 100%;height: 100%;top: 0;left: 0;background-color: rgba(37, 46, 53, 0.5);}";
  
   
		endif;
		
		$j++;
	  endforeach;	
	endif;
	//slider css end 
	
	//===blog
	$i=10;
	$key = 'blog';

	//--------------public css set-------------------
	$sections = texas_get_section_default(); 
	
	$default = $sections[$key];
		
	// section title hr	  
	$title_typography_value = get_theme_mod( $key.'_title_typography', texas_get_default_title_font($key) );
	$title_bottom_hr_color  = texas_change_color($title_typography_value['color'],0.5);
	
	$custom_css .='section.ct_section_'.$i.' .section-title-hr { border-top: 1px solid '.$title_bottom_hr_color.';}
	section.ct_section_'.$i.'  .section-title-hr:after {  border-top: 10px solid '.$title_bottom_hr_color.';}';
	
   
	//background color and  opacity  
	$section_background_color     = get_theme_mod( $key.'_section_background_color',$default['color']); 
	$section_background_opacity     = get_theme_mod( $key.'_section_background_opacity',1); 
  
	$background                    = texas_get_background( $section_background_color , $section_background_opacity );	
	$custom_css .='section.ct_section_'.$i.' {'.$background.' background-size: 100% auto;}';  
	
	// background_image 
	$section_background_image     = get_theme_mod( $key.'_section_background_image',$default['img']); 
	if ( $section_background_image != '' ){$custom_css .='section.ct_section_'.$i.' {background-image:url('.$section_background_image.');}';  } 
	
	//padding 
	$padding_default = array( 'top' => $default['padding_top'] ,'bottom' => $default['padding_bottom'] ,'left' => '0' ,'right' => '0' );
	$section_padding     = get_theme_mod( $key.'_section_padding',$padding_default);
	  
	$custom_css .='section.ct_section_'.$i.' .section_content{padding:'.$section_padding['top'].' '.$section_padding['right'].' '.$section_padding['bottom'].' '.$section_padding['left'].';}'; 
  
	$blog_button_color_hover = get_theme_mod( 'blog_button_color_hover','#0066ff');
	
	$custom_css .= '.post_readmore_bttn { color: '.$blog_button_color_hover.'; border: 1px solid '.$blog_button_color_hover.';} 
	.post_readmore_bttn:hover,.post_readmore_bttn:active { background-color: '.$blog_button_color_hover.';  }';
	//===blog end

	//===tool
	$i=13;
	$key = 'tool';
  
	//--------------public css set-------------------
	$sections = texas_get_section_default(); 
	$default = $sections[$key];
	
	$default_content = texas_section_content_default($key);
		
	//background color and  opacity  
	$section_background_color     = get_theme_mod( $key.'_section_background_color',$default['color']); 
	//echo '----555----';
	$section_background_opacity     = get_theme_mod( $key.'_section_background_opacity',1); 
  
	$background                    = texas_get_background( $section_background_color , $section_background_opacity );	
	$custom_css .='section.ct_section_'.$i.' {'.$background.' background-size: 100% auto;}';  
	
	// background_image 
	$section_background_image     = get_theme_mod( $key.'_section_background_image',$default['img']); 
	if ( $section_background_image != '' ){$custom_css .='section.ct_section_'.$i.' {background-image:url('.$section_background_image.');}';  } 
	
	//padding 
	$padding_default = array( 'top' => $default['padding_top'] ,'bottom' => $default['padding_bottom'] ,'left' => '0' ,'right' => '0' );
	$section_padding     = get_theme_mod( $key.'_section_padding',$padding_default);
	  
	$custom_css .='section.ct_section_'.$i.' .section_content{padding:'.$section_padding['top'].' '.$section_padding['right'].' '.$section_padding['bottom'].' '.$section_padding['left'].';}'; 
	//===tool end
	
	wp_add_inline_style( 'live-style', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'texas_styles_method' );

function texas_theme_scripts()
{
	
	if( is_page_template( 'template-home.php' ) && texas_check_kirki() ){
		wp_enqueue_script( 'bootstrap', get_template_directory_uri().'/js/bootstrap.js', array( 'jquery' ), '3.3.7', true );
		wp_enqueue_script( 'waypoints', get_template_directory_uri().'/custom/js/jquery.waypoints.js', array( 'jquery' ), '4.0.0', true );	
	}
	wp_enqueue_script( 'texas-script', get_template_directory_uri().'/js/custom.js', array( 'jquery' ), '', true );
	wp_localize_script( 'texas-script', 'ct_params', array(
		'ajaxurl'        => admin_url('admin-ajax.php'),
		'themeurl' => get_template_directory_uri(),
		
	)  );

	if( is_page_template( 'template-home.php' ) && texas_check_kirki() ){
		wp_add_inline_script( 'texas-script', texas_script_method() );
	}
}
add_action( 'wp_enqueue_scripts', 'texas_theme_scripts' );

function texas_script_method() {	
	$custom_js = 'jQuery(document).ready(function($){';

	$custom_js .= '
		var waypoint = new Waypoint({
		  element: jQuery(".ct_blog"),
		  handler: function(direction) {
			jQuery(".ct_blog .ct_post_img").addClass("animated rotateIn"); 
		  },
		  offset: "80%"
		});	
		';
	$custom_js .= '});';
	return $custom_js;
}

////////////////////////////////////////////////////////////////////
// Social links
////////////////////////////////////////////////////////////////////
if ( !function_exists( 'texas_social_links' ) ) :
	function texas_social_links() {
		$ct_social_links	 = array( 
			'ct_social_facebook'	 => esc_html__( 'Facebook', 'texas' ),
			'ct_social_twitter'	 => esc_html__( 'Twitter', 'texas' ),
			'ct_social_google'		 => esc_html__( 'Google-Plus', 'texas' ),
			'ct_social_youtube'	 => esc_html__( 'YouTube', 'texas' ),			
			'ct_social_instagram'	 => esc_html__( 'Instagram', 'texas' ),
			'ct_social_pin'		 => esc_html__( 'Pinterest', 'texas' ),
			'ct_social_reddit'		 => esc_html__( 'Reddit', 'texas' ),
		);
		$s_social_links_default = array(
		   __( '#Facebook', 'texas' ),
		   __( '#Twitter', 'texas' ),
		   __( '#Google-Plus', 'texas' ),
		  __( '#YouTube', 'texas' ),
		  '',
		  '',
		  '',
		);
		$i					 = 0;
		$ct_links_output	 = '';
		foreach ( $ct_social_links as $key => $value ) {
			$link = get_theme_mod( $key, $s_social_links_default["$i"]  );
			if ( !empty( $link ) ) {
				?>
          <div class="fa-link-wrap">
            <a target="_blank" href="<?php echo esc_url( $link );?>">
              <span class="fa-wrap"><i class="fa fa-<?php echo esc_html( strtolower( $value ) );?>" aria-hidden="true"></i></span>
              <div class="link-wrap"><?php echo esc_html( $value );?></div>
            </a> 
          </div>
		  <?php
			}
			$i++;
		}

	}
endif;


/* this function gets thumbnail from Post Thumbnail or Custom field or First post image */
if ( ! function_exists( 'texas_get_thumbnail' ) ) {
	function texas_get_thumbnail($post_id)
	{
		if(has_post_thumbnail())
		{
			
			$ct_post_thumbnail_fullpath=wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), "Full");
			$thumb_array['fullpath'] = $ct_post_thumbnail_fullpath[0];
		
		}else{
			$post_content = get_post($post_id)->post_content;
			$thumb_array['fullpath'] = texas_catch_that_image($post_content);
		}	
		
		if($thumb_array['fullpath']=="" )
		{
			
			$thumb_array['fullpath'] = get_template_directory_uri()."/images/default-thumbnail.jpg";
		
		}		

		return $thumb_array;
		
	}
}

function texas_catch_that_image($post_content)
{
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);
  if($output!='')  $first_img = $matches[1][0];

  return $first_img;
}


/*
add widgets to wp-admin
*/
function texas_widgets_init() {

	register_sidebar( array(
		'name' => __('Sidebar','texas'),
		'id' => 'sidebar',
		'before_widget' => '<aside id="top-posts-3" class="widget widget_top_posts">',
		'after_widget' => '</aside>',
		'before_title' => '<h3 class="widget-title"><i class="uicon-tag"></i> ',
		'after_title' => '</h3>',
	) );
	
}
add_action( 'widgets_init', 'texas_widgets_init' );




function texas_better_comments($comment, $args, $depth)
{
?>	
 
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
        <article id="comment-<?php comment_ID(); ?>" class="comment-body">
            <div class="comment_avatar">
                <?php echo get_avatar( $comment, 45 ); ?>
            </div>
			<div class="ct-comment-content">
                <div class="comment_postinfo">
                    <?php printf(__('<cite class="fn">%s</cite> <span class="says"> on </span>','texas'), get_comment_author_link()) ?>
                    <span class="comment_date"><?php printf(__('%1$s at %2$s','texas'), get_comment_date(),  get_comment_time()) ?></span><?php edit_comment_link(__('(Edit)','texas'),'  ','') ?>
                </div> <!-- .comment_postinfo -->
    
                <div class="comment_area">
                    <div class="comment-content clearfix">
                        <?php if ($comment->comment_approved == '0') : ?>
                            <em><?php esc_html_e('Your comment is awaiting moderation.','texas') ?></em>
                            <br />
                        <?php endif; ?>
                        
                        <?php comment_text() ?>
                        
                        
                        <div class="reply-container">
                            <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                            
                        </div>
                          
                    </div> <!-- end comment-content-->
                    
                </div> <!-- end comment_area-->
            </div>
            <p class="clear"></p>
        </article> <!-- .comment-body -->

<?php
}


if ( ! function_exists( 'texas_get_section_menu' ) ) {
	function texas_get_section_menu(){
		$section_menu = '';
		$section_default = array( 
				'slider',
				'blog',				
				'tool',);
		$sortable_value = maybe_unserialize( get_theme_mod( 'home_layout',$section_default ) );
		
	
		if ( ! empty( $sortable_value ) ) : 
		  foreach ( $sortable_value as $checked_value ) :
			?>
            	<li><a href="<?php echo esc_url(home_url('/'));?>#<?php echo esc_html($checked_value);?>"><?php echo esc_html(ucfirst($checked_value));?></a></li>
            <?php
		  endforeach;
		endif; 
	
	}

}

/**
 * Convert Hex Code to RGB   #FFFFFF -> 255 255 255
 * @param  string $hex Color Hex Code
 * @return array       RGB values
 
 texas_hex2rgb( '#FFFFFF')
 */
function texas_hex2rgb( $hex )
{
	if ( strpos( $hex,'rgb' ) !== FALSE )
	{
		$rgb_part = strstr( $hex, '(' );
		$rgb_part = trim($rgb_part, '(' );
		$rgb_part = rtrim($rgb_part, ')' );
		$rgb_part = explode( ',', $rgb_part );
		
		$rgb = array($rgb_part[0], $rgb_part[1], $rgb_part[2], $rgb_part[3]);
		
	}
	elseif( $hex == 'transparent' )
	{
		$rgb = array( '255', '255', '255', '0' );
	}
	else
	{
		$hex = str_replace( '#', '', $hex );	
		if( strlen( $hex ) == 3 )
		{
			$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
			$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
			$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
		}
		else
		{
			$r = hexdec( substr( $hex, 0, 2 ) );
			$g = hexdec( substr( $hex, 2, 2 ) );
			$b = hexdec( substr( $hex, 4, 2 ) );
		}
		$rgb = array( $r, $g, $b );
	}

	return $rgb; // returns an array with the rgb values
}

/* user:
$colour = '#00A800';
$brightness = 0.9; // 90% brighter
$newColour = cts_change_color($colour,$brightness);
*/
function texas_change_color($hex, $percent) {
	// Work out if hash given
	$hash = '';
	if (stristr($hex,'#')) {
		$hex = str_replace('#','',$hex);
		$hash = '#';
	}
	/// HEX TO RGB
	$rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2)));
	//// CALCULATE 
	for ($i=0; $i<3; $i++) {
		// See if brighter or darker
		if ($percent > 0) {
			// Lighter
			$rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent));
		} else {
			// Darker
			$positivePercent = $percent - ($percent*2);
			$rgb[$i] = round($rgb[$i] * $positivePercent) + round(0 * (1-$positivePercent));
		}
		// In case rounding up causes us to go to 256
		if ($rgb[$i] > 255) {
			$rgb[$i] = 255;
		}
	}
	//// RBG to Hex
	$hex = '';
	for($i=0; $i < 3; $i++) {
		// Convert the decimal digit to hex
		$hexDigit = dechex($rgb[$i]);
		// Add a leading zero if necessary
		if(strlen($hexDigit) == 1) {
		$hexDigit = "0" . $hexDigit;
		}
		// Append to the hex string
		$hex .= $hexDigit;
	}
	return $hash.$hex;
}


function texas_get_typography( $option_name,$option_default= array() )
{
  $return = '';
	
  $value = get_theme_mod( $option_name, $option_default );

  if ( isset( $value['font-family'] ) ) {
	$return .= 'font-family: \''.$value['font-family'].'\', sans-serif;';
  }
  if ( isset( $value['variant'] ) ) {
	$return .= 'font-style:'.$value['variant'].';';  
  }

  if ( isset( $value['font-size'] ) ) {
	$return .= 'font-size:'.$value['font-size'].';';  
  }
  if ( isset( $value['line-height'] ) ) {
	$return .= 'line-height:'.$value['line-height'].';';  
  }
  if ( isset( $value['letter-spacing'] ) ) {
	$return .= 'letter-spacing:'.$value['letter-spacing'].';'; 
  }
  if ( isset( $value['color'] ) ) {
	$return .= 'color:'.$value['color'].';'; 
  }	
  if ( isset( $value['text-transform'] ) ) {
	$return .= 'text-transform:'.$value['text-transform'].';';  
  }  
  
  if ( isset( $value['text-transform'] ) ) {
	$return .= 'text-align:'.$value['text-align'].';';  
  }    
  
  return $return ; 
}

/*	
*	get background 
*	---------------------------------------------------------------------
*/
function texas_get_background($args,$opacity=1)
{
	$background = "";


	$rgb = texas_hex2rgb($args);
	$background .= "background-color:rgba(".$rgb[0].",".$rgb[1].",".$rgb[2].",".esc_attr($opacity).");";

	return $background;
}

