<?php
/**
 * Digital enqueue scripts
 *
 * @package Digital
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'digital_scripts' ) ) {
	/**
	 * Load theme's JavaScript and CSS sources.
	 */
	function digital_scripts() {
		// Get the theme data.
		$the_theme         = wp_get_theme();
		$theme_version     = $the_theme->get( 'Version' );	
		$suffix            = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

		// Grab asset urls.
		$theme_styles  = "/css/theme.css";
		$theme_styles_bootstraputiliti  = "/css/bootstrap-utilities.min.css";
		//$theme_styles  = "/css/theme{$suffix}.css";
		$theme_scripts = "/js/theme{$suffix}.js";

		$css_version = $theme_version . '.' . filemtime( get_template_directory() . $theme_styles ); // @phpstan-ignore-line -- file exists
		wp_enqueue_style( 'digital-styles', get_template_directory_uri() . $theme_styles, array(), $css_version );
		wp_enqueue_style( 'digital-styles-bootstrap-utilities', get_template_directory_uri() . $theme_styles_bootstraputiliti, array(), $css_version );
		wp_enqueue_style( 'digital-design-improvements', get_template_directory_uri() . '/css/design-improvements.css', array( 'digital-styles' ), '1.0.0' );
		digital_offcanvas_admin_bar_inline_styles();
		digital_settings_css();
		if(get_theme_mod( 'digital_container_type' ) == 'container-box'){digital_boxcss();}
		if(get_theme_mod( 'digital_content_background' ) == 'contentblock'){digital_contentblock();}
		if (get_theme_mod('digital_heroarea') =='enable')	{digital_header_styletwo();}
		if (get_theme_mod('digital_thumbhover') =='enable')	{digital_thumbhover();}
		if (get_theme_mod('digital_woostyle') !=='disable')	{digital_woostyle();}
		
		wp_enqueue_script( 'jquery' );

		wp_enqueue_script('ajax-search', get_template_directory_uri() . '/js/ajax-search.js', array('jquery'), '1.0', true);
		wp_localize_script('ajax-search', 'ajaxsearch', array('ajaxurl' => admin_url('admin-ajax.php')));
	

		$js_version = $theme_version . '.' . filemtime( get_template_directory() . $theme_scripts ); // @phpstan-ignore-line -- file exists
		wp_enqueue_script( 'digital-scripts', get_template_directory_uri() . $theme_scripts, array(), $js_version, true );
		wp_enqueue_script('digital-ticker-js', get_template_directory_uri().'/js/tickerme.js', array(), '1.0', false );
		
		digital_scrolltop();
		if (get_theme_mod('digital_loading_animation') !== 'disable' ){ repulicpro_js_codes(); }
		if (get_theme_mod('digital_stickymenu') == 'sticky-top' ){ repulicpro_ifstickytop(); }
		repulicpro_jsticker_codes();
		
		//wp_register_script( 'digital-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js' );
		wp_enqueue_script('digital-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js', array(), '1.0', false );	
		// wp_localize_script('digital-tickertwo-js', 'custom_ticker_settings', array(
	//		'titleText' => get_theme_mod('digital_tickerlabel_text', 'News Ticker'),
	//	));
	//	wp_enqueue_script( 'digital-tickertwo-js' );	

	
		if (get_theme_mod('digital_sticky_sidebarblog') == 'enable' ){ repulicpro_sticky_sidebar(); }
		
		if (get_theme_mod('digital_enablefonts') == 'enable' ){
		wp_enqueue_style('digital-body-font', '//fonts.googleapis.com/css?family='.str_replace(" ", "+", get_theme_mod('digital_body_font', 'Open Sans') ).':100,300,400,500,600,700' );
		}		

		
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}
} // End of if function_exists( 'digital_scripts' ).

add_action( 'wp_enqueue_scripts', 'digital_scripts' );

/**
* Enqueue script for custom customize control.
*/
function digital_custom_customize_enqueue() {
	wp_enqueue_style( 'customizer-css', get_stylesheet_directory_uri() . '/css/customizer-css.css' );
}
add_action( 'customize_controls_enqueue_scripts', 'digital_custom_customize_enqueue' );


if ( ! function_exists( 'digital_offcanvas_admin_bar_inline_styles' ) ) {
	/**
	 * Add inline styles for the offcanvas component if the admin bar is visibile.
	 *
	 * Fixes that the offcanvas close icon is hidden behind the admin bar.
	 *
	 * @since 1.2.0
	 */
	function digital_offcanvas_admin_bar_inline_styles() {
		$navbar_type = get_theme_mod( 'digital_navbar_type', 'collapse' );
		if ( 'offcanvas' !== $navbar_type ) {
			return;
		}
		$css = '
		body.admin-bar .offcanvas.show  {
			margin-top: 32px;
		}
		@media screen and ( max-width: 782px ) {
			body.admin-bar .offcanvas.show {
				margin-top: 46px;
			}
		}';
		wp_add_inline_style( 'digital-styles', $css );
	}
}
$loadcbar = ''; // Initialize $loadcbar with an empty string
if ( ! function_exists( 'digital_settings_css' ) ) {
	
	if (get_theme_mod('digital_loading_animation') !== 'disable' ){
		$loadcbar = '@keyframes page-load{from{width:0}to{width:100%}}.page-loading::before{content:" ";display:block;position:fixed;z-index:10;height:3px;width:100%;top:0;left:0;background-color:#ff3030;animation:page-load ease-out 2s}.page-loading #wpadminbar{margin-top: 3px;}';}

		
	function digital_settings_css(){	
		
		 global $loadcbar, $heroarea;
		 $custom_csscode = get_theme_mod('custom_css', '');
		 $show_carttotal = get_theme_mod('digital_carttotal', true);	
		 if($show_carttotal){
			$showtotal="span.woocommerce-Price-amount.amount{display:inline;}";
		}else {
			$showtotal="span.woocommerce-Price-amount.amount{display:none;}";
		}
		if(get_theme_mod('digital_tagscloud_style')=='styleone'){
			$tagscloud =".stylefive-header a.tag-cloud-link{border:1px solid;font-size:.8em!important;padding:0 6px;-webkit-transition:.2s;transition:.2s}.stylefive-header a.tag-cloud-link:hover{background-color:#f44336;color:#fff}";
		}elseif(get_theme_mod('digital_tagscloud_style')=='styletwo'){
			$tagscloud ='a.tag-cloud-link{margin-left:16px;padding:0 8px;background:#0089e0;color:#fff;text-decoration:none;-moz-border-radius-bottomright:4px;-webkit-border-bottom-right-radius:4px;-moz-border-radius-topright:4px;-webkit-border-top-right-radius:4px;float:left;height:22px;list-style:none;line-height:22px;position:relative;font-size:14px!important;margin-block:2px;border-radius:2px 4px 4px 3px;-webkit-transition:.3s;-moz-transition:.3s;-ms-transition:.3s;-o-transition:.3s;transition:.3s}a.tag-cloud-link:before{content:"";float:left;position:absolute;top:0;left:-11px;width:0;height:0;border-color:transparent #0089e0 transparent transparent;border-style:solid;border-width:12px 12px 10px 0;-webkit-transition:.3s;-moz-transition:.3s;-ms-transition:.3s;-o-transition:.3s;transition:.3s}a.tag-cloud-link:after{content:"";position:absolute;top:10px;left:0;float:left;width:4px;height:4px;-moz-border-radius:2px;-webkit-border-radius:2px;border-radius:2px;background:#fff;-moz-box-shadow:-1px -1px 2px #004977;-webkit-box-shadow:-1px -1px 2px #004977;box-shadow:-1px -1px 2px #004977}a.tag-cloud-link:hover{background:#555}a.tag-cloud-link:hover:before{border-color:transparent #555 transparent transparent}';
		}elseif(get_theme_mod('digital_tagscloud_style')=='stylethree'){
			$tagscloud ='';
		}elseif(get_theme_mod('digital_tagscloud_style')=='stylefour'){
			$svg = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="red" class="bi bi-tag" viewBox="0 0 16 16"><path d="M6 4.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m-1 0a.5.5 0 1 0-1 0 .5.5 0 0 0 1 0"/><path d="M2 1h4.586a1 1 0 0 1 .707.293l7 7a1 1 0 0 1 0 1.414l-4.586 4.586a1 1 0 0 1-1.414 0l-7-7A1 1 0 0 1 1 6.586V2a1 1 0 0 1 1-1m0 5.586 7 7L13.586 9l-7-7H2z"/></svg>';
			$tagscloud = 'a.tag-cloud-link{font-size:16px!important;color:#050505;background:#fff;padding:2px 6px;border-radius:3px;box-shadow:0 0 4px 2px #ddd;margin:5px}a.tag-cloud-link:hover{background:#333;color:#fff}a.tag-cloud-link::before{content:"";display:inline-block;width:16px;height:13px;vertical-align:middle;background:url(\''.$svg.'\') center center/contain no-repeat;padding-right:3px}';
		}else{
			$tagscloud ='';
		}
		
		 $morefonts  = 'system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
		$custom_css = 'body{font-family: '.esc_html(get_theme_mod('digital_body_font')).','.$morefonts.';} 
		.styleone-header nav#main-nav.digitaltopmenu{background-color: '.esc_html(get_theme_mod('digital_topnavbgcolor','#3387d0')).' !important;background: '.esc_html(get_theme_mod('digital_topnavbgcolor','#3387d0')).' !important;}
		.stylefive-header #wrapper-navbar nav#main-nav{background-color: '.esc_html(get_theme_mod('digital_primarybgfive','#383e41')).' !important;border-color: '.esc_html(get_theme_mod('digital_primarybordercolorfive','#F44336')).' !important;}
		.stylefive-header #wrapper-navbar nav#main-nav li.current-menu-item, .stylefive-header div#wrapper-navbar .nav-link:hover{background: '.esc_html(get_theme_mod('digital_primarymenuhoverfive','#F44336')).' !important;color: '.esc_html(get_theme_mod('digital_primarymenuhovertextfive','#ffffff')).' !important;}
		.stylefive-header #wrapper-navbar nav#main-nav li.current-menu-item, .stylefive-header div#wrapper-navbar .nav-link{color: '.esc_html(get_theme_mod('digital_primarytextfive','#ffffff')).' !important;}
		.styleone-header .digitaltopmenu ul#main-menu li a:hover{background-color: '.esc_html(get_theme_mod('digital_topnavhover','#359BED')).' !important;color: #ffffff !important;}
		.styleone-header .digitaltopmenu ul#main-menu li a{color: '.esc_html(get_theme_mod('digital_topnavtext','#fcfcfc')).';}
		.styleone-header nav#main-nav{background: linear-gradient(0deg, '.esc_html(get_theme_mod('digital_primarymenubg','#485561')).' 50%, '.esc_html(get_theme_mod('digital_primarymenubgs','#56636f')).' 50%) !important;}
		.styleone-header .digitalprimaryone ul#main-menu li a:hover{background-color: '.esc_html(get_theme_mod('digital_primarymenuhover','#f3f3f3')).' !important;color: '.esc_html(get_theme_mod('digital_primarymenuhovertext','#333333')).' !important;}
		div#wrapper-navbar .nav-link{color: '.esc_html(get_theme_mod('digital_primarymenutext','#ffffff')).' !important;}
		.stylethree-header div#wrapper-navbar .nav-link{color: '.esc_html(get_theme_mod('digital_primarymenutext_three','#2f2b2b')).' !important;}
		.stylethree-header div#wrapper-navbar .nav-link:hover{color: '.esc_html(get_theme_mod('digital_primarymenutexthover_three','#3d951e')).' !important;}
		#wrapper-footer-full, #wrapper-static-hero{background-color: '.esc_html(get_theme_mod('digital_footerbg','#e9ecef')).';}
		.stylethree-header #wrapper-navbar nav#main-nav{background-color: '.esc_html(get_theme_mod('digital_primarymenubg_three','#ffffff')).' !important;}
		.stylesix-header #wrapper-navbar nav#main-nav{background-color: '.esc_html(get_theme_mod('digital_primarymenubg_six','#2196F3')).' !important;}
		.stylesix-header div#wrapper-navbar a.nav-link:hover{background: '.esc_html(get_theme_mod('digital_primarymenubghover_six','#062a47')).' !important;}
		.stylesix-header div#wrapper-navbar a.nav-link{color: '.esc_html(get_theme_mod('digital_primarymenutext_six','#ffffff')).' !important;}

		@media (min-width: 768px) {.styletwo-header a.navbar-brand.custom-logo-link{max-width: '.esc_html(get_theme_mod('digital_styletwo_logosize','10')).'% !important;}}
		div#wrapper-footer{background: '.esc_html(get_theme_mod('digital_footer_copy','#494949')).';}

		.styletwo-header a.nav-link{color: '.esc_html(get_theme_mod('digital_heroarea_links','#343434')).' !important;}
		.styletwo-header ul#main-menu li a:hover{color: '.esc_html(get_theme_mod('digital_heroarea_links_hover','#FF9800')).' !important;}
		header#wrapper-navbar.topbarthree{color: '.esc_html(get_theme_mod('digital_topnavibg_three','#3e4451')).' !important; color: '.esc_html(get_theme_mod('digital_topnavitext_three','#e0e0e0')).' !important;}
		header#wrapper-navbar.topbarsix{color: '.esc_html(get_theme_mod('digital_topnavibg_six','#ffffff')).' !important; color: '.esc_html(get_theme_mod('digital_topnavitext_six','#000000')).' !important;}
		'. $loadcbar . '
		h2.entry-title a{color: '.esc_html(get_theme_mod('digital_posttitlefont_color','#333333')).' !important;font-size: '.esc_html(get_theme_mod('digital_posttitlefont','17')).'px !important; font-style: '.esc_html(get_theme_mod('digital_posttitlefont_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('digital_posttitlefont_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('digital_posttitlefont_weight','400')).' !important;}
		.post-excerpt p{font-size: '.esc_html(get_theme_mod('digital_postexcert_size','17')).'px !important; font-style: '.esc_html(get_theme_mod('digital_postexcert_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('digital_postexcert_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('digital_postexcert_weight','100')).' !important;}h1.entry-title{font-size: '.esc_html(get_theme_mod('digital_postsingle_size','32')).'px !important; font-style: '.esc_html(get_theme_mod('digital_postsingle_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('digital_postsingle_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('digital_postsingle_weight','100')).' !important;}
		.page-loading::before {background-color: '.esc_html(get_theme_mod('digital_loading_bar_color','#ff3030')).' !important;} '.$custom_csscode. $showtotal.$tagscloud.'	
		::selection{background-color: '.esc_html(get_theme_mod('digital_selection_bg','#39a1eb')).' !important;color: '.esc_html(get_theme_mod('digital_selection_text','#ffffff')).' !important;}
		::-moz-selection{background-color: '.esc_html(get_theme_mod('digital_selection_bg','#ff3030')).' !important;color: '.esc_html(get_theme_mod('digital_selection_text','#ffffff')).' !important;}
			
		';		
		wp_add_inline_style( 'digital-styles', $custom_css );
	}
}

if ( ! function_exists( 'repulicpro_js_codes' ) ) {
	
	function repulicpro_js_codes(){
		
		$jscode = '(function( $ ) {
		  $(document).ready(function() {
				// Attach event listeners to specific links or elements where you want to trigger the loading animation
				var linksToAnimate = document.querySelectorAll(".load-animation-link");
				linksToAnimate.forEach(function(link) {
					link.addEventListener("click", function(e) {
						e.preventDefault(); // Prevent the default link behavior
						document.body.classList.add("page-loading");
						// Remove the loading class when the content has loaded
						window.addEventListener("DOMContentLoaded", function() {
							document.body.classList.remove("page-loading");
						});
					});
				});
				// Use this if you still want the loading animation on beforeunload
				window.addEventListener("beforeunload", function() {
					document.body.classList.add("page-loading");
				});
			
			});
		})(jQuery);';
		wp_add_inline_script( 'digital-scripts', $jscode );		
	}
}
if ( ! function_exists( 'repulicpro_jsticker_codes' ) ) {	
	function repulicpro_jsticker_codes(){
	
		$jscodeticker = '(function( $ ) {
		  $(document).ready(function() {
				$("#ticker").tickerme();
				 $("#js-news").ticker();				
			});
		})(jQuery);';

	wp_add_inline_script( 'digital-scripts', $jscodeticker );		
	}
}
function digital_admin_enqueue_styles() {
    wp_enqueue_style('digital-admin-css', get_template_directory_uri() . '/css/admin-style.css');
}
add_action('admin_enqueue_scripts', 'digital_admin_enqueue_styles');



 function digital_boxcss(){
    $mycss='body {max-width: 1279px;margin: 0 auto;background: #ececec;	}
	div#page {background: #ffffff;box-shadow: 10px 0 10px -10px #bbb, -10px 0 10px -10px #bbb;}
	';
   wp_add_inline_style( 'digital-styles', $mycss );
} 
 function digital_thumbhover(){
    $thumbhover='article .col-auto.d-lg-block:hover img {
    -webkit-transform: scale(1.1, 1.1);-moz-transform: scale(1.1, 1.1);-o-transform: scale(1.1, 1.1);-ms-transform:scale(1.1, 1.1);transform: scale(1.1, 1.1);-webkit-transition: all 0.6s linear;-moz-transition: all 0.6s linear;-o-transition: all 0.6s linear;-ms-transition: all 0.6s linear;transition: all 0.6s linear;}';
   wp_add_inline_style( 'digital-styles', $thumbhover );
} 
 function digital_contentblock(){
    $digital_contentblock='
	div#page {background: transparent !important;box-shadow: none !important;}
	.woocommerce ul.products li.product .woocommerce-loop-product__title,.woocommerce ul.products li.product .price,.card .add-to-cart-container {padding: 10px;}
	.woocommerce ul.products li.product a img{border-top-left-radius: 0.375rem;border-top-right-radius: 0.375rem;}
	.woocommerce ul.products li.product, .woocommerce-page ul.products li.product,
	article .row.g-0.border.rounded.overflow-hidden.flex-md-row.mb-4.shadow-sm.position-relative {background: #ffffff;}
	.digital-indexwid .col-md-7 ul li,
	.digital-indexwid .col-md-5,
	body.single-post main#main,
	body.page main#main,
	body.single-product main#main{background: #fff;padding: 2%;}
	.digital-popular, .digital-latest,div#left-sidebar aside, div#right-sidebar aside{padding: 10px;background: #ffffff;margin: 8px 0px;}
	div#left-sidebar aside:first-child,	div#right-sidebar aside:first-child{margin-top:0px;}';
   wp_add_inline_style( 'digital-styles', $digital_contentblock );
} 

function digital_header_styletwo(){
	$heroarea = '.styletwo-header nav#main-nav{background: #00000014 !important;}header#wrapper-navbar.nonfixed {height: 0;z-index: 100;position: relative;}';
	wp_add_inline_style( 'digital-styles', $heroarea );
}

function digital_woostyle(){
	$woostyle = '';
	wp_add_inline_style( 'digital-styles', $woostyle );
}

function digital_scrolltop(){	
	if(get_theme_mod('digital_scroll_top_position')=='left'){
		$scrollcss = '.scrollup{left: 0;right:  auto ;}';
	}
	else if(get_theme_mod('digital_scroll_top_position')=='right'){
		$scrollcss = '.scrollup{left: auto;right:  0;}';
	}
	else{
		$scrollcss = '.scrollup{left: auto;right:  0;}';
	}

	wp_add_inline_style( 'digital-styles', $scrollcss );
}

function repulicpro_ifstickytop(){
	$ifstickytop = '#wpadminbar + .styletwo-header>.container{margin-top: 120px;}header#wrapper-navbar.sticky-top{height:0;} #wpadminbar + .styletwo-header header#wrapper-navbar{margin-top: 32px;}#customize-preview .styletwo-header>.container{margin-top: 120px;}';
	wp_add_inline_style( 'digital-styles', $ifstickytop );
}


if ( ! function_exists( 'repulicpro_sticky_sidebar' ) ) {
	
	function repulicpro_sticky_sidebar(){
		
		$jscode = '(function( $ ) {
			$(document).ready(function() {
				$(window).scroll(function() {
					// Check if left sidebar exists
					var leftSidebar = $("#left-sidebar");
					var leftSidebarOffset = leftSidebar.length > 0 ? leftSidebar.offset().top : null;
		
					// Check if right sidebar exists
					var rightSidebar = $("#right-sidebar");
					var rightSidebarOffset = rightSidebar.length > 0 ? rightSidebar.offset().top : null;
		
					// Modify left sidebar properties
					if (leftSidebarOffset !== null) {
						if ($(document).scrollTop() > (leftSidebarOffset - 20)) {                        
							leftSidebar.addClass("sticky-top");
						} else {
							leftSidebar.removeClass("sticky-top");
						}
					}
		
					// Modify right sidebar properties
					if (rightSidebarOffset !== null) {
						if ($(document).scrollTop() > (rightSidebarOffset - 20)) {
							rightSidebar.addClass("sticky-top");
						} else {
							rightSidebar.removeClass("sticky-top");
						}
					}
				});
			});
		})(jQuery);';
		wp_add_inline_script( 'digital-scripts', $jscode );		
		
		$csscode = 'div#right-sidebar, div#left-sidebar{height: fit-content;}';
		wp_add_inline_style( 'digital-styles', $csscode );		
	}
}
