//cookie for banner
function setCookie(name, value) {
	document.cookie = name + "=" + (value || "");
}

function setCookie(name, value, exdays) {
	const d = new Date();
	d.setTime(d.getTime() + exdays);
	let expires = "expires=" + d.toUTCString();
	document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}


jQuery(document).ready(($) => {
	let listBTNmenu = $(".menu-item-has-children").before();
	$(listBTNmenu).on('click', function() {
 			if ($('.custom-sub-menu', $(this)).hasClass('click')) {
 				$('.custom-sub-menu', $(this)).removeClass('click');
 				$('.custom-sub-menu', $(this)).slideUp();
 			} else {
 				$('.custom-sub-menu', $(this)).addClass('click');
 				$('.custom-sub-menu', $(this)).slideDown();
 			}
		});
	// for (let i = 0; i < listBTNmenu.length; i++) {
	// 	$(listBTNmenu[i]).click(() => {
	// 		if ($('.menu-item-has-children .custom-sub-menu').hasClass('click')) {
	// 			$('.menu-item-has-children .custom-sub-menu').removeClass('click');
	// 			$('.menu-item-has-children .custom-sub-menu').slideUp();

	// 		} else {
	// 			$('.menu-item-has-children .custom-sub-menu').addClass('click');
	// 			$('.menu-item-has-children .custom-sub-menu').slideDown();

	// 		}
	// 	})
	// }
// 	$(listBTNmenu).each(function () {
// 		$(this).off('click touchstart').on('click touchstart', function() {
// 			if ($('.custom-sub-menu', $(this)).hasClass('click')) {
// 				$('.custom-sub-menu', $(this)).removeClass('click');
// 				$('.custom-sub-menu', $(this)).slideUp();
// 			} else {
// 				$('.custom-sub-menu', $(this)).addClass('click');
// 				$('.custom-sub-menu', $(this)).slideDown();

// 			}
// 		});
// 	});
})

var cookie_expires = document.querySelector("#gr-popup.cookie-expires");
if (typeof (cookie_expires) != 'undefined' && cookie_expires != null) {
	var cookie_popup_expires = getCookie('popup_expires');
	console.log(cookie_popup_expires);
	if (cookie_popup_expires) {
		document.getElementById('gr-popup').style.display = "none";
	} else {
		document.getElementById('gr-popup').style.display = "block";
	}

	var timeCookie = document.getElementById('gr-popup').getAttribute('data-timeCookie');
	var el_timeCookie = timeCookie.split(':');
	var hours = el_timeCookie[0];
	var min = el_timeCookie[1];
	console.log(hours, min);
	var exdays_miliseconds = (hours * 60 * 60 * 1000) + (min * 60 * 1000);
	console.log(exdays_miliseconds);

	document.querySelector("#gr-popup.cookie-expires .popup-close").addEventListener("click", function () {
		console.log("expires");
		setCookie('popup_expires', 'checkCookie', exdays_miliseconds);
		document.getElementById('gr-popup').style.display = "none";
	});
}

var cookie_default = document.querySelector("#gr-popup.cookie-default");
if (typeof (cookie_default) != 'undefined' && cookie_default != null) {
	var cookie_popup_default = getCookie('popup_default');
	if (cookie_popup_default) {
		document.getElementById('gr-popup').style.display = "none";
	} else {
		document.getElementById('gr-popup').style.display = "block";
	}

	document.querySelector("#gr-popup.cookie-default .popup-close").addEventListener("click", function () {
		setCookie('popup_default', 'checkCookie');
		document.getElementById('gr-popup').style.display = "none";
	});
}

(function ($) {
	"use strict";
	$(document).ready(function () {
		if ($(window).width() <= 400) {
			let i = `width=320, user-scalable=yes`;
			$("#monile_view_port")?.attr('content', i);
		}
		if ($(window).width() > 700) {

			$('.menu-item-has-children')?.mouseenter(function () {
				$('.menu-item-has-children ul.sub-menu').css({
					display: 'flex',
				});
			})
				.mouseleave(function () {
					$('.menu-item-has-children ul.sub-menu').css({
						display: 'none'
					});
				});

		}

		if (($(".page_nav_")?.length) > 0) {
			$("#primary").css({
				'padding-top': '0px'
			})
			$("#page").css({
				'padding-top': '0px'
			})
		}

		if (($(".page_nav")?.length) > 0) {
			$("#primary").css({
				'padding-top': '0px'
			})
			$("#page").css({
				'padding-top': '0px'
			})
		}


		if ($(window).innerWidth() <= 500) {
			let marginRight = $("#masthead .container").offset();
			if (marginRight) {
				$(".red-hero").css({
					"background-position": `calc(100% - 40px) 95%`
				})
			}
		} else {
			let marginRight = $("#masthead .container").offset();
			if (marginRight) {
				$(".red-hero").css({
					"background-position": `calc(100% - ${marginRight.left}px) -100%`
				})
			}
		}
		$(window).on('resize', function () {
			if ($(window).innerWidth() >= 1280) {
				// take margin-left from other containers
				var margin = $("section.home-firstsec .container").css("margin-left");
				// and put on leftright
				$(".section.leftright-sec .inner").css("padding-left", margin);
				$(".section.leftright-altsec .inner").css("padding-right", margin);
			} else {
				$(".section.leftright-sec .inner").css("padding-left", '30px');
				$(".section.leftright-altsec .inner").css("padding-right", '30px');
			}
		});
		/*Videos Ajax Filter Start*/
    if ($(".videos-section").length) {
        $('.nav-link').on('click', function (e) {
			var academy_url;
			academy_url = url_academy.split('?')[0];
            e.preventDefault();
            $('.nav-link').removeClass('active');
            $(this).addClass('active');
            //var url_current = $("#url_current").data("url-current");

			
			// var url_current = $(this).data("slug");
			var url_current = $(this).attr('href');
			// if (url_current == 'all') {
			// 	url_current == '';
			// } else {
			// 	url_current == url_current;
			// }

			if (history.pushState) {
				// history.pushState({}, document.title, url_current);
				history.pushState("", "", url_current);
			}

            $.ajax({
                type: 'POST',
                url: propertyguru.ajax_url,
                data: {
                    action: 'filter_videos',
                    category: $(this).data('slug'),
					// videoID: $(this).data('video')
					// video_id : video_id
					video_id : $(this).data('video'),
                //    url_current: url_current
                },
                success: function (data) {
                    $('.video-main-wrapper').html(data);
					tooltipShare();
					totalCountShare();
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
                },
                error: function () {
                    $('.video-main-wrapper').html('An error has occurred.');
                }
            })
        });
    }
/*Videos Ajax Filter End */
// New
    if ($(".iproperty-videos-section").length) {
        $('.nav-link-bite-sized').on('click', function (e) {
			var academy_url;
			academy_url = url_academy.split('?')[0];
            e.preventDefault();
            $('.nav-link-bite-sized').removeClass('active');
            $(this).addClass('active');
            //var url_current = $("#url_current").data("url-current");

			var url_current = $(this).attr('href');
			// if (url_current == 'all') {
			// 	url_current == '';
			// } else {
			// 	url_current == url_current;
			// }

			if (history.pushState) {
				// history.pushState({}, document.title, url_current);
				history.pushState("", "", url_current);
			}

            $.ajax({
                type: 'POST',
                url: propertyguru.ajax_url,
                data: {
                    action: 'filter_videos',
                    category: $(this).data('slug'),
					// videoID: $(this).data('video')
					// video_id : video_id
					video_id : $(this).data('video'),
                //    url_current: url_current
                },
                success: function (data) {
                    $('.video-main-wrapper-bite-sized').html(data);
					tooltipShare();
					totalCountShare();
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
                },
                error: function () {
                    $('.video-main-wrapper-bite-sized').html('An error has occurred.');
                }
            })
        });
    }
    /*Videos Ajax Filter End*/
		if ($(".page-template-academy-resources").length) {
		  $('#videos-filters-search-button').on('click', function (e) {
				console.log('yiqi the great');
				  e.preventDefault();
					$.ajax({
							type: 'POST',
							url: propertyguru.ajax_url,
							data: {
									action: 'search_videos',
									keyword: $('#videos-filters-search-text').val()
							},
							success: function (data) {
									$('.videos-listing').html(data);
				console.log('success search');
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
							},
							error: function () {
									$('.videos-listing').html('An error has occurred.');
							}
					})
			});
		}

		if ($(".page-template-academy-resources-new").length) {
		  $('#videos-filters-search-button').on('click', function (e) {
				console.log('yiqi the great');
				  e.preventDefault();
					$.ajax({
							type: 'POST',
							url: propertyguru.ajax_url,
							data: {
									action: 'search_videos',
									keyword: $('#videos-filters-search-text').val()
							},
							success: function (data) {
									$('.videos-listing').html(data);
				console.log('success search');
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
							},
							error: function () {
									$('.videos-listing').html('An error has occurred.');
							}
					})
			});
		}

		if ($(".page-template-academy-resources-new").length) {
		  $('#videos-filters-search-button-new').on('click', function (e) {
				  e.preventDefault();
					$.ajax({
							type: 'POST',
							url: propertyguru.ajax_url,
							data: {
									action: 'search_videos',
									keyword: $('#videos-filters-search-text-new').val()
							},
							success: function (data) {
									$('.videos-listing-new').html(data);
				console.log('success search');
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
							},
							error: function () {
									$('.videos-listing-new').html('An error has occurred.');
							}
					})
			});
		}
/*Videos Ajax ChangeFeatured Start*/
    if ($(".page-template-academy-resources").length) {

		$(document.body).on('click', '.other_videos' ,function(e){
			
			// $('html,body').animate({
      		//   scrollTop: $(".videos-filters").offset().top
			//  },
        	// 'slow');

            e.preventDefault();
           // $('.nav-link').removeClass('active');
            //$(this).addClass('active');

            $.ajax({
                type: 'POST',
                url: propertyguru.ajax_url,
                data: {
                    action: 'filter_change_featured',
                    this_id: $(this).data('id')
                //    url_current: url_current
                },
                success: function (data) {
                    $('.featured_video_wrapper').html(data);
					tooltipShare();
					totalCountShare();
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
                },
                error: function () {
                    $('.featured_video_wrapper').html('An error has occurred.');
                }
            })
        });
    }
    /*Videos Ajax ChangeFeatured End*/

/*Videos Ajax ChangeFeatured Start*/
    if ($(".page-template-academy-resources-new").length) {

		$(document.body).on('click', '.other_videos_new' ,function(e){
			$('html,body').animate({
      		  scrollTop: $(".videos-filters-new").offset().top
			 },
        	'slow');
            e.preventDefault();
           // $('.nav-link').removeClass('active');
            //$(this).addClass('active');

            $.ajax({
                type: 'POST',
                url: propertyguru.ajax_url,
                data: {
                    action: 'filter_change_featured',
                    this_id: $(this).data('id')
                //    url_current: url_current
                },
                success: function (data) {
                    $('.featured_video_wrapper_new').html(data);
					clear_click();
					tooltipShare();
					totalCountShare();
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
                },
                error: function () {
                    $('.featured_video_wrapper_new').html('An error has occurred.');
                }
            })
        });
    }

    if ($(".page-template-academy-resources-new").length) {
		var current_url = window.location.href;
		console.log(current_url);
		// Check if there is an ID in the fragment part of the URL
		var fragment = current_url.split('#')[1];

		if (fragment) {
		// Get the element with the ID matching the fragment
		var element = document.getElementById(fragment);

		// If the element exists, scroll to it
		if (element) {
			element.scrollIntoView({ behavior: 'smooth' });
		}
		}

		$(document.body).on('click', '.other_videos' ,function(e){	
			
			if($(this).parents('.videos-section').hasClass('videos-section')){
			     	$('html,body').animate({
				     	scrollTop: $(".videos-filters").offset().top
					},
					'slow');
			}
			if($(this).parents('.iproperty-videos-section').hasClass('iproperty-videos-section')){
						$('html,body').animate({
							scrollTop: $(".videos-filters-new").offset().top
					},
					'slow');
			}
			
            e.preventDefault();
           // $('.nav-link').removeClass('active');
            //$(this).addClass('active');

            $.ajax({
                type: 'POST',
                url: propertyguru.ajax_url,
                data: {
                    action: 'filter_change_featured',
                    this_id: $(this).data('id')
                //    url_current: url_current
                },
                success: function (data) {
                    $('.featured_video_wrapper').html(data);
					clear_click();
					tooltipShare();
					totalCountShare();
//                     if (history.pushState) {
//                         history.pushState({}, document.title, url_current);
//                     }
                },
                error: function () {
                    $('.featured_video_wrapper').html('An error has occurred.');
                }
            })
        });
    }
    /*Videos Ajax ChangeFeatured End*/
		//menu mobile

		$(".menu-toggle")?.click(() => {
			$("#site-navigation").addClass('active');
		});
		$(".menu-close")?.click(() => {
			$("#site-navigation").removeClass('active');
		})

		//get submenu by js

		let listSubMenu = $(".sub-menu li a");
		let listRender = $(".page_nav ul");
		let listResult = [];
		let domRender = (name, href) => {
			return `<li><a href="${href}">${name}</a></li>`;
		};

		for (let i = 0; i < listSubMenu?.length; i++) {
			listResult.push({
				text: listSubMenu[i]?.textContent,
				url: listSubMenu[i]?.getAttribute('href')
			})
			if ((i + 1) == listSubMenu?.length) {
				listRender.empty();
			}
		}
		listResult.map(e => {
			let string = domRender(e.text, e.url);
			listRender?.append(string);
		})

		if ($(".contact-sec").length) {
			var offset = $(".contact-sec").offset().top;
			$(".short-cta a")?.click((e) => {
				e.preventDefault();
				$('html, body').scrollTop(offset - 100);
				return false;

			})
		}

		function social_count(social) {
			$.ajax({
				type: "POST",
				url: propertyguru.ajax_url,
				dataType: 'json',
				data: {
					action: 'propertyguru_single_post_social',
					id: $('.single-sharing-meta').attr('data-id'),
					social: social,
					_wpnonce: propertyguru.nonce
				},
				success: function (rs) {

				}
			});
		}
		function social_count_video(social) {
			var _this = $('.option_action .share-link');
			$.ajax({
				type: "POST",
				url: propertyguru.ajax_url,
				dataType: 'json',
				data: {
					action: 'propertyguru_single_post_social_video',
					id: $('#permalink-primary-video').attr('data-id'),
					social: social,
					_wpnonce: propertyguru.nonce
				},
				success: function (data) {
					
				}
			});
		}
		$(document).on('click', '.single-sharing-meta a', function (e) {
			var social = $(this).attr('data-social');
			social_count(social);
		});

		$(window).load(function () {
			var sliderItem = $('#main-slider .main-slide-item').map(function () {
				var imgHeight = $(this).find('.thumb-pc').height();

				return parseInt(imgHeight);
			});

			var minSliderItem = Math.min.apply(Math, sliderItem);

			if (minSliderItem > 0) {
				$('#main-slider .main-slide-item .thumb-pc').css('height', minSliderItem);
			}
		});


		if ($('body').hasClass('single-post')) {
			var views = [];
			const regex = /postid-([0-9]+)/gm;
			var m = regex.exec($('body').attr('class'));



			if (m !== null) {
				var post_id = parseInt(m[1]);
				views.push(post_id);

				Cookies.set('single_views', views.join(','));
			}
		}

		$(document).on('click', '.cta', function () {
			var _this = $(this),
				type = $(this).attr('data-type'),
				id = $(this).attr('data-postID');
			$.ajax({
				type: "POST",
				url: propertyguru.ajax_url,
				dataType: 'json',
				data: {
					action: 'propertyguru_cta_banner',
					type: type,
					id: id
				},
				success: function (rs) {
				}
			});
		});

		$(document).on('click', '.option_action  .share-link', function (e) {
			e.preventDefault();
		});

		$(document).on('click', '.featured_video_wrapper .option_action .like_up', function (e) {
			e.preventDefault();
			var _this = $(this),
				type = $(this).attr('data-type'),
				id = $(this).attr('data-id'),
				isOK = false;
			if (typeof Cookies.get(type) != 'undefined') {
				var _cookies = JSON.parse(Cookies.get(type));
				$.each(_cookies, function (i, v) {
					if (v == id) {
						isOK = true;
					}
				});
			}
			
			if(_this.hasClass('active') && type == 'like-up' ){
				alert(propertyguru.labels.like_up);
				return;
			}
			if( isOK && type == 'like-up') {
				alert(propertyguru.labels.like_up);
				return;
			}
			if($('.featured_video_wrapper .option_action .like_up').hasClass('active')){
				alert(propertyguru.labels.like_up);
				return;
			} 
			
			else if (!isOK) {
				$.ajax({
					type: "POST",
					url: propertyguru.ajax_url,
					dataType: 'json',
					data: {
						action: 'propertyguru_single_post_video',
						id: id,
						type: type,
						nonce: propertyguru.nonce
					},
					success: function (data) {
						if (typeof data.success != 'undefined') {
							if (_this.next().length <= 0) {
								_this.after('<div class="social-count">' + data.count + '</div>');
							} else {
								_this.next().html(data.count);
							}
							_this.addClass('active');
							Cookies.set(type, data.cookie);
						} else {
							if (typeof data.message != 'undefined') {
								alert(data.message);
							}
						}
					}
				});
			}
		});
		$(document).on('click', '.featured_video_wrapper_new .option_action .like_up', function (e) {
			e.preventDefault();
			var _this = $(this),
				type = $(this).attr('data-type'),
				id = $(this).attr('data-id'),
				isOK = false;
			if (typeof Cookies.get(type) != 'undefined') {
				var _cookies = JSON.parse(Cookies.get(type));
				$.each(_cookies, function (i, v) {
					if (v == id) {
						isOK = true;
					}
				});
			}

			if(_this.hasClass('active') && type == 'like-up' ){
				alert(propertyguru.labels.like_up);
				return;
			}
			if( isOK && type == 'like-up') {
				alert(propertyguru.labels.like_up);
				return;
			}
			if($('.featured_video_wrapper_new .option_action .like_up').hasClass('active')){
				alert(propertyguru.labels.like_up);
				return;
			} 
			else if (!isOK) {
				$.ajax({
					type: "POST",
					url: propertyguru.ajax_url,
					dataType: 'json',
					data: {
						action: 'propertyguru_single_post_video',
						id: id,
						type: type,
						nonce: propertyguru.nonce
					},
					success: function (data) {
						if (typeof data.success != 'undefined') {
							if (_this.next().length <= 0) {
								_this.after('<div class="social-count">' + data.count + '</div>');
							} else {
								_this.next().html(data.count);
							}
							_this.addClass('active');
							Cookies.set('like-up-sec2', data.cookie);
						} else {
							if (typeof data.message != 'undefined') {
								alert(data.message);
							}
						}
					}
				});
			}
		});

		$(document).on('click', '.popup-footer__icon', function (e) {
			e.preventDefault();

			var _this = $(this),
				type = $(this).attr('data-type'),
				id = $(this).attr('data-id');
			$.ajax({
				type: "POST",
				url: propertyguru.ajax_url,
				dataType: 'json',
				data: {
					action: 'propertyguru_popup_post',
					id: id,
					type: type
				},
				success: function (rs) {
					if (_this.next().length <= 0) {
						_this.after('<div class="social-count">' + rs.count + '</div>');
					} else {
						_this.next().html(rs.count);
					}
				}
			});
		});


		$(document).on('click', '.single-extra-meta-right a', function (e) {
			e.preventDefault();

			var _this = $(this),
				type = $(this).attr('data-type'),
				id = $(this).attr('data-id'),
				isOK = false;

			if (typeof Cookies.get(type) != 'undefined') {
				var _cookies = JSON.parse(Cookies.get(type));
				// console.log(_cookies);
				$.each(_cookies, function (i, v) {
					if (v == id) {
						isOK = true;
					}
				});
			}

			if (!isOK) {
				$.ajax({
					type: "POST",
					url: propertyguru.ajax_url,
					dataType: 'json',
					data: {
						action: 'propertyguru_single_post',
						id: id,
						type: type,
						_wpnonce: propertyguru.nonce
					},
					success: function (rs) {
						if (typeof rs.complete != 'undefined') {
							if (_this.next().length <= 0) {
								_this.after('<div class="social-count">' + rs.count + '</div>');
							} else {
								_this.next().html(rs.count);
							}

							_this.addClass('active');
							Cookies.set(type, rs.cookie);
						} else {
							if (typeof rs.message != 'undefined') {
								alert(rs.message);
							}
						}
					}
				});
			} else {
				alert(propertyguru.labels[type]);
			}

		});

		// Tooltip share in academy page 
		function clear_click (){
			$(document).ready(function(){
				$('.share-icon').unbind('click');
				$('.featured_video_wrapper_new .count-social-video li a ').unbind('click');
				$('.featured_video_wrapper .count-social-video li a ').unbind('click');
			});
		}
		function tooltipShare () {
			$(document).ready(function(){
				$('.share-icon').bind('click', function(event){
					event.stopPropagation();
				$(this).parent().children('.social-box').toggle();
				});
				$('.social-box').click(function(event){
				   event.stopPropagation();
				});
				$(document).click(function(){
					  $('.social-box').hide();
				});
			});
		}
		tooltipShare();
		
		
		// Total Count social video in academy page 
		function totalCountShare () {
			$(document).ready(function(){
				$('.featured_video_wrapper .count-social-video li a ').bind('click', function(e){
					
					var dataId =  $(this).attr('data-id');
					var dataType = $(this).attr('data-type');
					var dataValue = $(this).attr('data-value');
			
					if(dataType == '_post_share') {
					
						$("<textarea/>").appendTo("body").val(dataValue).select().each(function () {
							document.execCommand('copy');
							alert("Copy Success");
							$('.social-box').css('display', 'none');
						}).remove();
					
					}
			
					$.ajax({
					type: "POST",
					url: propertyguru.ajax_url,
					dataType: 'json',
					data: {
						action: 'total_count_social_video',
						id: dataId,
						type : dataType,
						_wpnonce: propertyguru.nonce
					},
					success: function (data) {
							$('.featured_video_wrapper .total-share').html(data);
					}
				});
				});
				// Section  social video featured_video_wrapper_new
				$('.featured_video_wrapper_new .count-social-video li a ').bind('click', function(e){
				
					var dataId =  $(this).attr('data-id');
					var dataType = $(this).attr('data-type');
					var dataValue = $(this).attr('data-value');
		
					if(dataType == '_post_share') {
					
					$("<textarea/>").appendTo("body").val(dataValue).select().each(function () {
							document.execCommand('copy');
							alert("Copy Success");
							$('.social-box').css('display', 'none');
						}).remove();
					
					}
		
				$.ajax({
					type: "POST",
					url: propertyguru.ajax_url,
					dataType: 'json',
					data: {
						action: 'total_count_social_video',
						id: dataId,
						type : dataType,
						_wpnonce: propertyguru.nonce
					},
					success: function (data) {
						$('.featured_video_wrapper_new .total-share').html(data);
					}
				});
				});
			})
		}
		totalCountShare();

		if ($('body').hasClass('single-post')) {
			var clipboard = new ClipboardJS('.clipboard-share');
			clipboard.on('success', function (e) {
				alert('Copy success');
				social_count('share-link');
				e.clearSelection();
			});
		}

		if ($('body').hasClass('page-template-academy-resources')) {
			var clipboard = new ClipboardJS('.clipboard-share-video');
			clipboard.on('success', function (e) {
				// alert('Copy success');
				social_count_video('share-link-video');
				e.clearSelection();
			});
		}



		$('textarea#comment').addClass('textarea-init');
		$(document).on('focus', '#comment', function (e) {
			if ($(this).hasClass('textarea-init')) {
				autosize($('textarea#comment'));
				$(this).removeClass('textarea-init');
				$(this).closest('#commentform').addClass('textarea-focus');
			}
		});

		if (jQuery().owlCarousel) {
			$('.owl-carousel').owlCarousel({
				loop: true,
				margin: 10,
				responsiveClass: true,
				items: 1,
				dots: false,
				nav: true,
				navText: ['<i class="icon-left-arrow"></i>', '<i class="icon-right-arrow"></i>']
			});
		}

		if (jQuery().select2) {
			$('.form-select2').select2({ width: '100%' });
		}
		if ($('body').hasClass('page-template-news')) {
			$(document).on('change', '.news-search-filters :input', function (e) {
				e.preventDefault();

				filter_search($(this));
			});

			function filter_search($this) {
				var params = [],
					url = $this.closest('form').attr('action'),
					keyword = $('.news-search-input input[name="k"]').val(),
					hasParam = false;

				$('.news-search-filters select').each(function () {
					var val = $(this).val(),
						name = $(this).attr('name');

					if (val != 'all') {
						params.push(name + '=' + val);
					} else {
						if (getUrlParameter(name)) {
							hasParam = true;
						}
					}
				});

				if (keyword.length > 0) {
					params.push('search=' + keyword);
				}

				if (params.length > 0) {
					url = url + '?' + params.join('&');
					window.location.href = url;
				} else {
					if (hasParam) {
						window.location.href = url;
					}
				}


			}

			var getUrlParameter = function getUrlParameter(sParam) {
				var sPageURL = window.location.search.substring(1),
					sURLVariables = sPageURL.split('&'),
					sParameterName,
					i;

				for (i = 0; i < sURLVariables.length; i++) {
					sParameterName = sURLVariables[i].split('=');

					if (sParameterName[0] === sParam) {
						return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
					}
				}
				return false;
			}
		}

		$('.menu-toggle').click(function () {
			$(this)
				.parent()
				.siblings('.main-navigation').addClass('toggled');
			$('.menu-close').click(function () {
				$(this).parents('.main-navigation').removeClass('toggled');
			})
			$('.main-navigation li a').on("click", function () {
				$(this).parents('.main-navigation').removeClass('toggled');
			});
			//remove popup  when click screen//
			$(document).mouseup(function (event) {
				event.preventDefault();
				var container = $(".main-navigation.toggled");
				if (!container.is(event.target) && container.has(event.target).length === 0) {
					$(".main-navigation.toggled").removeClass('toggled');
				}
			});
		})

		// guidelines popup
		$('.comment-respond .comment-reply-desc a').on("click", function (e) {
			e.preventDefault();
			console.log("zzz");
			$(this).parents('.site-main').find('.guidelines').addClass("js-active");
		})
		$('.guidelines .cta-agree button').click(function () {
			$(this).parents('.guidelines').removeClass("js-active");
		})
		$('.guidelines .cta-close').click(function () {
			$(this).parents('.guidelines').removeClass("js-active");
		})

		$(document).mouseup(function (event) {
			event.preventDefault();
			var container = $(".guidelines .container");
			if (!container.is(event.target) && container.has(event.target).length === 0) {
				$(".guidelines").removeClass('js-active');
			}
		});

		$('.not_login_submit,.reply_notlogin').click((e) => {
			e.preventDefault();
			$('.login_requit').css('display', 'flex');
			$("body, html").animate({ scrollTop: $('.login_requit').offset().top - 300 }, 500), !1;
			setTimeout(() => {
				window.location = "https://agentnet.staging.propertyguru.com.sg/";
			}, 1500);
		})

		$('.pop-up-test button').click(() => {
			console.log("test");
			$('.pop-up-test').hide();
		});

	});

	$(".fastkey .oacarousel").slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		centerMode: true,
		variableWidth: true,
		arrows: true,
	});
	$(".partner360 .oacarousel").slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		arrows: true,
		variableWidth: true,
		centerMode: true,
	});
	$(".homeloan .oacarousel").slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		arrows: true,
		centerMode: true,
	});
	$(".weeklyfeature .oacarousel").slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		centerMode: true,
		arrows: true
	});
	$(".market-insights .avail-toyou .oacarousel").slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		centerMode: true,
		arrows: true
	});
	$(".market-insights .harness-data .oacarousel").slick({
		dots: false,
		slidesToShow: 1,
		slidesToScroll: 1,
		centerMode: true,
		arrows: true
	});
	if ($(window).innerWidth() >= 1280) {
		// take margin-left from other containers
		var margin;
		setTimeout(function () {
			margin = $("section.home-firstsec .container").css("margin-left");
			// and put on leftright
			$(".section.leftright-sec .inner").css("padding-left", margin);
			$(".section.leftright-altsec .inner").css("padding-right", margin);
		}, 300);
	} else {
		$(".section.leftright-sec .inner").css("padding-left", '30px');
		$(".section.leftright-altsec .inner").css("padding-right", '30px');
	}


	if ($(window).width() < 767) {
		$('body.page-template-template-renewal').addClass('device-mobile-optimized');
	}
	document.querySelectorAll('a.scrollToAnchor').forEach(a => {
		a.addEventListener('click', function (e) {
			e.preventDefault();
			var href = this.getAttribute("href");
			var elem = document.querySelector(href) || document.querySelector("a[name=" + href.substring(1, href.length) + "]");
			//gets Element with an id of the link's href
			//or an anchor tag with a name attribute of the href of the link without the #
			window.scroll({
				top: elem.offsetTop,
				left: 0,
				behavior: 'smooth'
			});
		});
	});

})(jQuery);

//Topics modal iframe
jQuery(document).ready(function($) {
    // Modal HTML structure
    var modalHtml = `
    <div id="topicModal" class="modal">
        <div class="modal-content1">
            <iframe id="modal-iframe" src="" frameborder="0" allowfullscreen width="100%" height="100%"></iframe>
            <div id="modal-close" class="modal-icon close" style="position: absolute;top: 20px;right: 30px;cursor: pointer;">
            	<svg fill="#000000" height="22px" width="22px" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 460.775 460.775" xml:space="preserve"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M285.08,230.397L456.218,59.27c6.076-6.077,6.076-15.911,0-21.986L423.511,4.565c-2.913-2.911-6.866-4.55-10.992-4.55 c-4.127,0-8.08,1.639-10.993,4.55l-171.138,171.14L59.25,4.565c-2.913-2.911-6.866-4.55-10.993-4.55 c-4.126,0-8.08,1.639-10.992,4.55L4.558,37.284c-6.077,6.075-6.077,15.909,0,21.986l171.138,171.128L4.575,401.505 c-6.074,6.077-6.074,15.911,0,21.986l32.709,32.719c2.911,2.911,6.865,4.55,10.992,4.55c4.127,0,8.08-1.639,10.994-4.55 l171.117-171.12l171.118,171.12c2.913,2.911,6.866,4.55,10.993,4.55c4.128,0,8.081-1.639,10.992-4.55l32.709-32.719 c6.074-6.075,6.074-15.909,0-21.986L285.08,230.397z"></path> </g></svg>
            </div>
			<div id="loading-animation">
				<div class="loader"></div>
			</div>
        </div>
    </div>`;
    // Add the modal HTML structure to the body when the page loads
    // window.addEventListener("DOMContentLoaded", function () {
    //     console.log("add modal")
    	document.body.insertAdjacentHTML("beforeend", modalHtml);
    // });
    // Function to open the modal
		function openModal(url) {
			  // Display the loading animation
			  	var modalContent = document.getElementsByClassName("modal-content1")[0];
				var loadingAnimation = document.createElement("div");
				loadingAnimation.id = "loading-animation";
				loadingAnimation.innerHTML = '<div class="loader"></div>';
				modalContent.appendChild(loadingAnimation);
			// document.getElementById("modal-iframe").src = url;
			if (!document.getElementById("modal-iframe").getAttribute("src")) {
				document.getElementById("modal-iframe").src = url; // Set the src attribute only if it's empty
			}
			document.getElementById("modal-iframe").addEventListener("load", function () {
				// Remove the loading animation
				document.getElementById("loading-animation").remove();
			  });
			  // Set a timeout to remove the loading animation after 3 seconds
			setTimeout(function () {
				document.getElementById("loading-animation").remove();
			}, 3000);
			document.getElementById("topicModal").style.display = "block";
			document.body.classList.add("modal-open");
		}
    // Attach an event listener to the button that opens the modal
    var topicButtons = document.querySelectorAll(".modal-button");
    topicButtons.forEach(function(topicButton) {
        topicButton.addEventListener("click", function() {
            var url = topicButton.getAttribute("data-url");
            openModal(url);
        });
    });

    // Function to close the modal
    function closeModal() {
        document.getElementById("modal-iframe").src = "";
        document.getElementById("topicModal").style.display = "none";
        document.body.classList.remove("modal-open");
    }

    // Event listener for the close button
    // document.querySelector(".close").addEventListener("click", function () {
    //     closeModal();
    // });

    // Event listener for clicking outside the modal
    window.addEventListener("click", function (e) {
        if (e.target === document.getElementById("topicModal")) {
            closeModal();
        }
    });
	$("#modal-close").click(function () { 
		closeModal();
		console.log("close modal");
	});
});