Defer( function () {
$(document).ready(function() {
if ( ! document.cookie.match(/^(.*;)?\s*bf_adfpv_v1\s*=\s*[^;]+(.*)?$/) ) {
var date = new Date();
var now = new Date();
date.setTime(date.getTime()+(10*365*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = "bf_adfpv_v1="+now.toGMTString()+expires+"; path=/";
}
else {
// Initialize AdSense
(adsbygoogle = window.adsbygoogle || []);
// Affiliate ad override configuration from PHP
var affiliateOverrides = {"top-leaderboard-ad":{"total_percentage":25,"options":[{"name":"House Ad","weight":1,"href":"\/homebrew\/signup","img":"\/homebrew\/images\/houseads_20211207\/take10_signup\/webp\/810X100.webp","width":810,"height":100}]},"inline-unit-ad":{"total_percentage":25,"options":[{"name":"House Ad","weight":1,"href":"\/homebrew\/signup","img":"\/homebrew\/images\/houseads_20211207\/take10_signup\/webp\/810X100.webp","width":810,"height":100}]},"sidebar-block-1-ad":{"total_percentage":25,"options":[{"name":"House Ad","weight":1,"href":"\/homebrew\/signup","img":"\/homebrew\/images\/houseads_20211207\/take10_signup\/webp\/300x250.webp","width":300,"height":250}]}};
// Process each potential ad override
if (affiliateOverrides && Object.keys(affiliateOverrides).length > 0) {
Object.keys(affiliateOverrides).forEach(function(adSlotId) {
var override = affiliateOverrides[adSlotId];
var adElement = document.getElementById(adSlotId);
// Check if ad exists and should be replaced based on total percentage
if (adElement && override.options && override.options.length > 0 && (Math.random() * 100 < override.total_percentage)) {
// Choose an option based on weights
var option = chooseWeightedOption(override.options);
// Build affiliate ad HTML
var style = option.style || '';
if (option.width && option.height) {
style += (style ? '; ' : '') + 'max-width:100%;';
}
var attributes = '';
if (option.target) attributes += ' target="' + option.target + '"';
if (option.rel) attributes += ' rel="' + option.rel + '"';
var affiliateAdHtml = '
';
// Replace the AdSense container with affiliate ad
$(adElement).closest('ins.adsbygoogle').replaceWith(affiliateAdHtml);
}
});
}
// Helper function to select a weighted random option
function chooseWeightedOption(options) {
if (!options || options.length === 0) {
// This case implies an issue with how options are passed or an empty configuration.
// Returning undefined is safer than the original's options[0] which would error.
return undefined;
}
var eligibleOptions = [];
var totalPositiveWeight = 0;
// First pass: Collect options with strictly positive weights and sum these weights.
for (var i = 0; i < options.length; i++) {
var option = options[i];
// Ensure weight is treated as a number; default undefined/non-numeric to 0.
var weight = (typeof option.weight === 'number') ? option.weight : 0;
if (weight > 0) {
eligibleOptions.push(option);
totalPositiveWeight += weight;
}
}
if (totalPositiveWeight > 0) {
// If there are options with positive weights, choose from them.
var randomValue = Math.random() * totalPositiveWeight;
var cumulativeWeight = 0;
for (var j = 0; j < eligibleOptions.length; j++) {
// eligibleOptions[j].weight is guaranteed to be > 0 here.
cumulativeWeight += eligibleOptions[j].weight;
if (randomValue <= cumulativeWeight) {
return eligibleOptions[j];
}
}
// Fallback for rare floating-point precision issues or if randomValue is exactly totalPositiveWeight.
// Returning the last eligible option is a robust choice here.
return eligibleOptions[eligibleOptions.length - 1];
} else {
// No options with positive weight were found.
// This means all original options had weights <= 0 (e.g., all zeros or empty list of positive weights).
// In this scenario, pick one randomly from the original 'options' list,
// giving all of them an equal chance.
return options[Math.floor(Math.random() * options.length)];
}
}
// Initialize all remaining AdSense ads
$("ins.adsbygoogle").each(function() {
$("(adsbygoogle = window.adsbygoogle || []).push({})").insertAfter($(this));
});
var js = document.createElement("script");
js.type = "text/javascript";
js.async = true;
js.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
document.body.appendChild(js);
}
});
});