<?php
/**
 * Magazine Style enqueue scripts
 *
 * @package Magazine Style
 */

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

if ( ! function_exists( 'magazinestyle_scripts' ) ) {
	/**
	 * Load theme's JavaScript and CSS sources.
	 */
	function magazinestyle_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( 'magazinestyle-styles', get_template_directory_uri() . $theme_styles, array(), $css_version );
		wp_enqueue_style( 'magazinestyle-styles-bootstrap-utilities', get_template_directory_uri() . $theme_styles_bootstraputiliti, array(), $css_version );
		magazinestyle_offcanvas_admin_bar_inline_styles();
		magazinestyle_settings_css();
		if(get_theme_mod( 'magazinestyle_container_type' ) == 'container-box'){magazinestyle_boxcss();}
		if(get_theme_mod( 'magazinestyle_content_background' ) == 'contentblock'){magazinestyle_contentblock();}
		if (get_theme_mod('magazinestyle_heroarea') =='enable')	{magazinestyle_header_styletwo();}
		if (get_theme_mod('magazinestyle_thumbhover') =='enable')	{magazinestyle_thumbhover();}
		if (get_theme_mod('magazinestyle_woostyle') !=='disable')	{magazinestyle_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( 'magazinestyle-scripts', get_template_directory_uri() . $theme_scripts, array(), $js_version, true );
		wp_enqueue_script('magazinestyle-ticker-js', get_template_directory_uri().'/js/tickerme.js', array(), '1.0', false );
		
		magazinestyle_scrolltop();
		if (get_theme_mod('magazinestyle_loading_animation') !== 'disable' ){ repulicpro_js_codes(); }
		if (get_theme_mod('magazinestyle_stickymenu') == 'sticky-top' ){ repulicpro_ifstickytop(); }
		repulicpro_jsticker_codes();
		
		//wp_register_script( 'magazinestyle-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js' );
		wp_enqueue_script('magazinestyle-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js', array(), '1.0', false );	
		// wp_localize_script('magazinestyle-tickertwo-js', 'custom_ticker_settings', array(
	//		'titleText' => get_theme_mod('magazinestyle_tickerlabel_text', 'News Ticker'),
	//	));
	//	wp_enqueue_script( 'magazinestyle-tickertwo-js' );	

	
		if (get_theme_mod('magazinestyle_sticky_sidebarblog') == 'enable' ){ repulicpro_sticky_sidebar(); }
		
		if (get_theme_mod('magazinestyle_enablefonts') == 'enable' ){
		wp_enqueue_style('magazinestyle-body-font', '//fonts.googleapis.com/css?family='.str_replace(" ", "+", get_theme_mod('magazinestyle_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( 'magazinestyle_scripts' ).

add_action( 'wp_enqueue_scripts', 'magazinestyle_scripts' );

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


if ( ! function_exists( 'magazinestyle_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 magazinestyle_offcanvas_admin_bar_inline_styles() {
		$navbar_type = get_theme_mod( 'magazinestyle_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( 'magazinestyle-styles', $css );
	}
}
$loadcbar = ''; // Initialize $loadcbar with an empty string
if ( ! function_exists( 'magazinestyle_settings_css' ) ) {
	
	if (get_theme_mod('magazinestyle_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 magazinestyle_settings_css(){	
		
		 global $loadcbar, $heroarea;
		 $custom_csscode = get_theme_mod('custom_css', '');
		 $show_carttotal = get_theme_mod('magazinestyle_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('magazinestyle_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('magazinestyle_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('magazinestyle_tagscloud_style')=='stylethree'){
			$tagscloud ='';
		}elseif(get_theme_mod('magazinestyle_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('magazinestyle_body_font')).','.$morefonts.';}
		#wrapper-footer-full, #wrapper-static-hero{background-color: '.esc_html(get_theme_mod('magazinestyle_footerbg','#e9ecef')).';}	
		@media (min-width: 768px) {.styletwo-header a.navbar-brand.custom-logo-link{max-width: '.esc_html(get_theme_mod('magazinestyle_styletwo_logosize','10')).'% !important;}}
		div#wrapper-footer{background: '.esc_html(get_theme_mod('magazinestyle_footer_copy','#494949')).';}
		.tickertitle>.bn-title{background: '.esc_html(get_theme_mod('magazinestyle_tickerbg','#2096cd')).' !important;}
		.tickertitle>.bn-title>span{border-color: transparent transparent transparent '.esc_html(get_theme_mod('magazinestyle_tickerbg','#2096cd')).' !important;}
		.tickertitle>.bn-title>p{color: '.esc_html(get_theme_mod('magazinestyle_tickerlabelfont','#ffffff')).' !important;}
		div#ticker_container{background: '.esc_html(get_theme_mod('magazinestyle_tickerpostbg','#ffffff')).' !important;}
		div#ticker_container a{color: '.esc_html(get_theme_mod('magazinestyle_tickerpostlink','#5533ff')).' !important;}
		.col-2.bn-title{background: '.esc_html(get_theme_mod('magazinestyle_tickerbg_two','#F44336')).' !important;color: '.esc_html(get_theme_mod('magazinestyle_tickerlabelfont_two','#ffffff')).' !important;}
		.row.ticker-three .col-2.bn-title-three{background: '.esc_html(get_theme_mod('magazinestyle_tickerbg_three','#F44336')).' !important;color: '.esc_html(get_theme_mod('magazinestyle_tickerlabelfont_three','#ffffff')).' !important;}
		.row.ticker-three .col-10 svg{color: '.esc_html(get_theme_mod('magazinestyle_tickerbg_three','#F44336')).' !important;}
		.ticker,.ticker-content,.ticker-swipe,.ticker-swipe span,.row.ticker-two{background: '.esc_html(get_theme_mod('magazinestyle_tickerpostbg_two','#f8f0db')).' !important;}
		.row.ticker-three{background: '.esc_html(get_theme_mod('magazinestyle_tickerpostbg_three','#0d1726')).' !important;}
		.ticker-content a{color: '.esc_html(get_theme_mod('magazinestyle_tickerpostlink_two','#1F527B')).' !important;}
		.row.ticker-three marquee a{color: '.esc_html(get_theme_mod('magazinestyle_tickerpostlink_three','#ffffff')).' !important;}		
		h2.entry-title a{font-size: '.esc_html(get_theme_mod('magazinestyle_posttitlefont','20')).'px !important; font-style: '.esc_html(get_theme_mod('magazinestyle_posttitlefont_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('magazinestyle_posttitlefont_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('magazinestyle_posttitlefont_weight','600')).' !important;}
		.post-excerpt p{font-size: '.esc_html(get_theme_mod('magazinestyle_postexcert_size','17')).'px !important; font-style: '.esc_html(get_theme_mod('magazinestyle_postexcert_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('magazinestyle_postexcert_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('magazinestyle_postexcert_weight','100')).' !important;}h1.entry-title{color: '.esc_html(get_theme_mod('magazinestyle_postsingle_color','#333333')).' !important;font-size: '.esc_html(get_theme_mod('magazinestyle_postsingle_size','32')).'px !important; font-style: '.esc_html(get_theme_mod('magazinestyle_postsingle_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('magazinestyle_postsingle_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('magazinestyle_postsingle_weight','100')).' !important;}
		.page-loading::before {background-color: '.esc_html(get_theme_mod('magazinestyle_loading_bar_color','#ff3030')).' !important;} '.$custom_csscode. $showtotal. $loadcbar . $tagscloud.'	
			
		';		
		wp_add_inline_style( 'magazinestyle-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( 'magazinestyle-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( 'magazinestyle-scripts', $jscodeticker );		
	}
}
function magazinestyle_admin_enqueue_styles() {
    wp_enqueue_style('magazinestyle-admin-css', get_template_directory_uri() . '/css/admin-style.css');
}
add_action('admin_enqueue_scripts', 'magazinestyle_admin_enqueue_styles');



 function magazinestyle_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( 'magazinestyle-styles', $mycss );
} 
 function magazinestyle_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( 'magazinestyle-styles', $thumbhover );
} 
 function magazinestyle_contentblock(){
    $magazinestyle_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;}
	.magazinestyle-indexwid .col-md-7 ul li,
	.magazinestyle-indexwid .col-md-5,
	body.single-post main#main,
	body.page main#main,
	body.single-product main#main{background: #fff;padding: 2%;}
	.magazinestyle-popular, .magazinestyle-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( 'magazinestyle-styles', $magazinestyle_contentblock );
} 

function magazinestyle_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( 'magazinestyle-styles', $heroarea );
}

function magazinestyle_woostyle(){
	$woostyle = '';
	wp_add_inline_style( 'magazinestyle-styles', $woostyle );
}

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

	wp_add_inline_style( 'magazinestyle-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( 'magazinestyle-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( 'magazinestyle-scripts', $jscode );		
		
		$csscode = 'div#right-sidebar, div#left-sidebar{height: fit-content;}';
		wp_add_inline_style( 'magazinestyle-styles', $csscode );		
	}
}
