if (typeof BdSet != 'object') { BdSet = {}; }
jQuery(document).ready(function() {
const config_ajax_height = 500;
const win_height = jQuery(window).height();
const win_offset = win_height * .1;
const header_hight = jQuery("header").height();
const topbar_height = jQuery(".topbar_basic, .topbar").height()
const load_more_spinner = jQuery("#load-more-spinner")
const is_mobile_theme = jQuery("link[href*='themes/mobile_2015/style.css']").length || false;
const page_type = jQuery("body").data("page");
const jbody = jQuery("body");
let sidefix = undefined;
let ga_list = []
let ga_list_height = []
let offset_height = undefined;
let scroll = undefined;
let scroll_btm = undefined;
const get_ga_list = function() {
ga_list = jQuery('.the-post').has('h2 a').not(':has(.blog-post-preview-read-more, .post-meta-read-more)')
return ga_list;
}
const get_ga_list_hight = function(){
return ga_list.toArray().map( function(el){ return el.offsetHeight } );
}
ga_list = get_ga_list()
ga_list_height = get_ga_list_hight();
const fix_header_position = function() {
jQuery("header").css("margin-top", topbar_height)
}
const fix_ad_scroll = function() {
jQuery('.ad-top, .ad-fix, ad-single-bottom').not('.fixed').first().each(function(index) {
const ad = jQuery(this);
if (!offset_height) {
const adminbar_h = jQuery("#wpadminbar").height();
const topbar_h = is_mobile_theme ? 0 : topbar_height;
offset_height = adminbar_h + topbar_h;
}
const ad_margin_top = ad.css("margin-top");
// If half of the banner stay in the screen for 1.2 sec,
// the sticky effect will be removed.
if (scroll_btm >= ad.offset().top + ad.height()/2 &&
scroll + offset_height <= ad.offset().top &&
!ad.hasClass("sticky-count")
){
ad.addClass("sticky-count")
setTimeout( function() {
ad.addClass("fixed")
}, 1200);
}
if (ad.hasClass("fixed") || (scroll + offset_height <= ad.offset().top)) { return };
const parent = jQuery(this).parent(".ad-loop, ad-single-bottom");
parent.css("height", parent.height() + "px");
ad.addClass("ad-pos-fixed fixed");
ad.css("margin-top", offset_height + "px")
ad.delay(1000).fadeOut(400, function() {
ad.css("margin-top", ad_margin_top)
ad.removeClass("ad-pos-fixed").fadeIn(400)
});
});
}
const image_tagging = function() {
if (!jQuery.fn.run_image_tagging) { return; }
jQuery(document).run_image_tagging();
}
const fix_ad_style = function() {
const ad = jQuery(".ad-loop, .ad-single-bottom");
if (jQuery("#sidebar").length) {
// get post width , not include sidebar
const ad_loop_width = jQuery("#blog-posts, .the-post, .status-publish, .blog-post-container").outerWidth();
ad.css({ 'width': ad_loop_width + "px" });
} else {
var ad_loop_margin = -(jQuery('body .container').width() - jQuery("#blog-posts, .the-post").width()) / 2;
//if(!jQuery("body").data('post-id')) {
ad.css("margin-left", ad_loop_margin);
ad.css("margin-right", ad_loop_margin);
//}
jQuery(".w-100.center").css({"width":"75%","margin":"auto"});
ad.css("text-align", "center");
}
ad.children().filter("div").css("width", ad.width() + "px");
}
const load_ajax_page = function() {
const spinner = load_more_spinner;
if (scroll_btm < jQuery(document).height() - config_ajax_height ||
!spinner.length ||
spinner.is(":visible") ||
jQuery("#loop-loaded-content").hasClass('final-page') ||
jQuery("body.popup-opened").length
) { return; }
spinner.show();
const page = jQuery(".js-more-content").length + 2
const more = jQuery('
')
jQuery("#loop-loaded-content").append(more);
//uri needs a leading slash, add one if no one exists allready
const pathname = location.pathname
checked_path = pathname.slice(-1) == "/" ? pathname : pathname + '/'
// check for search
var found = document.location.href.match('[\?|\&]s=(.*)$');
if (found != null) {
var search = "&s=" + found[1];
} else {
var search = '';
}
more.load(`${checked_path}page/${page}?ajax=1&page=${page}${search} #blog-posts`, function(res, stat) {
if (stat === "error" || res.indexOf(" 102) { // This number needs to match the height defined in class: ".post-content-boxes".
text_arr.pop();
jQuery(this).text(text_arr.join(" ")+"...");
}
});
if (is_mobile_theme) { return }
FB.XFBML.parse();
});
}
const sent_scroll_pageview = function() {
if (ga_list.length < 0) { return; };
ga_list.each(function(i, ele) {
const el = $(ele);
if (is_in_viewport(el, ga_list_height[i], 0) == false) {
el.removeClass('pageview_sent');
}
if(el.hasClass('pageview_sent')){ return; }
if (is_in_viewport(el, ga_list_height[i]) == true) {
el.addClass('pageview_sent');
ga('set', 'anonymizeIp', true);
const link_el = el.find('h2 a');
if (!link_el.length) { return false; }
const url = link_el[0].pathname + '-homepage';
ga('send', 'pageview', url);
return false;
}
});
}
// dont refresh add on mouse over
jQuery('[id^=cncpt-]').mouseenter(function(){
var el = jQuery(this);
var timeoutId = setTimeout(function() {
el.addClass("do-not-refresh");
}, 1500);
el.mouseleave(function() {
clearTimeout(timeoutId);
});
});
const is_in_viewport = function(el, hight = el.height(), offset = win_offset) {
const et = el.offset().top;
const eb = et + hight
const wt = scroll;
const wb = scroll + win_height;
// Three conditions, top and btm both out side window , or one of them appear in the window.
return (et < wt && eb > wb) || (et > wt + offset && et < wb-offset) || (eb > wt + offset && eb < wb - offset);
};
// let ad_time_list = {}
// const reload_ads = function() {
// jQuery(jQuery('[id^=cncpt-]').get().reverse()).each(function(){ //We reverse the load init order (Not actually indicative of what will load first, only what is initalized first)
// const el = jQuery(this);
// const ad_id = el.attr('id');
// ad_time_list[ad_id] = ad_time_list[ad_id] || Date.now();
// if(is_in_viewport(el) && !el.hasClass('do-not-refresh')) {
// if (Date.now() - ad_time_list[ad_id] > 30000 ) {
// el.empty();
// BdSet.show_banner()(null, el);
// ad_time_list[ad_id] = Date.now();
// }
// } else {
// ad_time_list[ad_id] = Date.now();
// }
// })
// }
const activate_unveil = function() {
jQuery(".the-post img[data-lazy-src], .content-area img[data-lazy-src], .blog-post-content img[data-lazy-src]").not(".lazy-loaded").first().each(function() {
const el = jQuery(this);
if (scroll_btm > el.offset().top - config_ajax_height*2) { // config_ajax_height is distance to show in window.
el.fadeOut().attr('src', el.attr('data-lazy-src')).fadeIn(1000);
el.addClass('lazy-loaded');
console.log('lazy-loaded');
if (!jQuery.fn.repos_imgtag) { return true; } // means continue
el.repos_imgtag();
}
})
};
const sidebar_init = function(){
if (jQuery("#sidebar").length && jQuery("#sidebar-content").length) {
sidefix = {}
sidefix.sidebar = jQuery("#sidebar");
sidefix.cont = jQuery("#sidebar-content");
sidefix.status = 0 ;
sidefix.footer = jQuery('footer');
sidefix.bottombar = jQuery('.bottombar');
sidefix.post = jQuery(".blog-post-container, #blog-posts").first()
} else {
return;
}
}
const sidebar_pos_fix = function() {
if(!sidefix) { return };
const sidebar = sidefix.sidebar;
const content = sidefix.cont;
const footer = sidefix.footer;
const bottombar = sidefix.bottombar;
const footer_top = footer.length ? footer.offset().top : Infinity;
const post = sidefix.post;
const content_height = content.height() || 0
const post_height = post.height() || 0
const top_line = sidebar.offset().top
const bottom_line = top_line + content_height
if(isNaN(sidefix.btmbar_h)){
sidefix.btmbar_h = bottombar.length ? bottombar.height() : 0;
}
// fix width
if (!content.hasClass('width-fixed')) {
content.css({'min-width': sidebar.width() + "px", 'max-width': sidebar.width() + "px"});
content.addClass('width-fixed')
}
sidefix.inner_win_height = sidefix.inner_win_height || win_height - sidefix.btmbar_h - topbar_height
let reach_header,reach_footer,middle_css,top_css,bottom_css;
if (content_height > post_height ) {
return;
} else if (content_height < sidefix.inner_win_height ) {
content_to_btm = sidefix.btmbar_h + sidefix.inner_win_height - content_height;
reach_header = scroll >= top_line - topbar_height
reach_footer = scroll_btm >= footer_top + content_to_btm;
middle_css = {'bottom': '', 'top': topbar_height};
top_css = {'bottom': '', 'top': ''};
bottom_css = {'top': '', 'bottom': scroll_btm - footer_top};
} else {
reach_header = scroll_btm >= bottom_line + sidefix.btmbar_h
reach_footer = scroll_btm >= footer_top + sidefix.btmbar_h
middle_css = {'bottom': sidefix.btmbar_h};
top_css = {'bottom': ''};
bottom_css = {'bottom': scroll_btm - footer_top};
}
if ( reach_header && !reach_footer ) { // when scroll in middle
if (sidefix.status==1){return} // no need to run code , when the status is the same
sidefix.status=1
content.addClass("sidebar-sticky-top");
content.css(middle_css)
} else if (!reach_header && !reach_footer) { // when scroll to top
if (sidefix.status==2){return}
sidefix.status=2
content.removeClass("sidebar-sticky-top");
content.removeClass("sidebar-sticky-bottom");
content.css(top_css)
} else if ( page_type != 'else' && footer.length && reach_footer) { // when scroll to bottom
content.css(bottom_css)
if (sidefix.status==3){return}
sidefix.status=3
content.removeClass("sidebar-sticky-top");
content.addClass("sidebar-sticky-bottom");
}
}
const fix_comment_bar = function() {
const bar = jQuery("#comment-bar");
if (page_type != 'else' && bar.length > 0) {
const form = jQuery("#comment-bar-form");
const length = bar.height() - form.height() + bar.offset().top;
const width = bar.width();
if (scroll < bar.offset().top - 65) {
form.removeClass("stick");
form.css({'position': 'absolute', 'top': '0', 'width': width});
} else if (scroll > length - 65) {
form.removeClass("stick");
form.css({'position': 'absolute', 'bottom': '0', 'top': 'auto', 'width': width});
} else {
form.addClass("stick");
form.css({'height': form.height() + 'px', 'width': width});
}
}
}
let header_hight_status = 0;
const fix_background_position = function() {
if (header_hight_status == 1) { return }
jQuery("body").css("background-position-y", 0)
header_hight_status == 1
const diff = header_hight - scroll;
if (diff < 0) { return };
jQuery("body").css("background-position-y", diff)
header_hight_status == 0
}
const add_scroll_event = function() {
//load new content in loop
scroll = jQuery(window).scrollTop();
scroll_btm = scroll + win_height;
load_ajax_page();
activate_unveil();
//this is f*cking ugly
if (jQuery("body").data("blog-id") != '23') {
fix_ad_scroll();
}
fix_background_position();
if(page_type == 'else'){
sent_scroll_pageview();
}
if (is_mobile_theme) { return }
fix_comment_bar();
sidebar_pos_fix();
}
BdSet.convet_gif_to_video();
//BdSet.ads_load_ajax();
fix_header_position();
image_tagging();
fix_ad_style();
sidebar_init()
add_scroll_event();
// setInterval(reload_ads, 5000);
jQuery(window).scroll(add_scroll_event);
})