Tagging = function(context) {
this.site = 'eoldal';
this.zone = 'szemelyes-csaladi';
this.protocol = window.location.protocol;
this.autodetect_url = this.protocol + '//cts.p24.hu/service/detect/';
this.context = context;
this.positions = {};
this.positions_weighted = {};
this.positions['head'] = '[{\"service_name\": \"Sanoma\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Sanoma\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Gemius Traffic\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Gemius\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['head'] = false;
this.positions['body-start'] = '[{\"service_name\": \"Sanoma\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['body-start'] = false;
this.positions['body-end'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"\"}, {\"service_name\": \"Gemius\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['body-end'] = false;
this.positions['body-end-adv'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"
\"}]';
this.positions_weighted['body-end-adv'] = false;
this.positions['top'] = '[]';
this.positions_weighted['top'] = false;
this.positions['top-hl'] = '[{\"service_name\": \"Google Adverts\", \"weight\": 0, \"source\": \" HIRDETÉS
\"}, {\"service_name\": \"Adverticum AdServer\", \"weight\": 100, \"source\": \"\"}]';
this.positions_weighted['top-hl'] = true;
this.positions['top-blog'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['top-blog'] = false;
this.positions['top-other'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['top-other'] = false;
this.positions['right010'] = '[{\"service_name\": \"Google Adverts\", \"weight\": 0, \"source\": \" HIRDETÉS
\"}, {\"service_name\": \"Adverticum AdServer\", \"weight\": 100, \"source\": \"\"}]';
this.positions_weighted['right010'] = true;
this.positions['right020'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['right020'] = false;
this.positions['sky-hl'] = '[]';
this.positions_weighted['sky-hl'] = false;
this.positions['sky-user'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['sky-user'] = false;
this.positions['sky-other'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['sky-other'] = false;
this.positions['header010'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['header010'] = false;
this.positions['header020'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['header020'] = false;
this.positions['bottom'] = '[{\"service_name\": \"Sanoma/Hirstart\", \"weight\": null, \"source\": \"\"}]';
this.positions_weighted['bottom'] = false;
this.positions['content-bottom'] = '[]';
this.positions_weighted['content-bottom'] = false;
}
Tagging.prototype.get_json = function(text) {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, '')))
&& eval('(' + text + ')');
}
Tagging.prototype.random_tag = function(position) {
var tags = [];
var weighted_tags = [];
for (index in position) {
tag = position[index];
tags[index] = tag.source;
for (var i = 0; i < tag.weight; i++) {
weighted_tags.push(index);
}
}
// Pick a random tag from array based on weight
var random_index = weighted_tags[Math.floor((Math.random() * weighted_tags.length))];
return tags[random_index];
}
Tagging.prototype.all_tags = function(position) {
var tags = [], index, tag;
for (index in position) {
tag = position[index];
tags[index] = tag.source;
}
return tags.join("\n");
}
Tagging.prototype.render = function(slug, context) {
document.write(this.get_source(slug, context));
}
Tagging.prototype.renderAsync = function (slug, elSelector, clearNodeContent, context) {
var receiverNode = (elSelector != undefined && elSelector != null) ? document.querySelector(elSelector) : document.querySelector("[data-cts-position='" + slug + "']");
if (receiverNode != undefined && clearNodeContent != undefined && clearNodeContent != null && clearNodeContent) {
receiverNode.innerHTML = '';
}
if (receiverNode != undefined) {
receiverNode.setAttribute("data-cts-tag", slug);
try {
window.dispatchEvent(new CustomEvent("_ce_cts_render_async", { "detail": { "pos": slug, "tagging": new Tagging() } }));
} catch (err) {
console.log(err);
}
}
var htmlDoc = document.implementation.createHTMLDocument();
htmlDoc.body.innerHTML = (this.get_source(slug, context)).replace(/(src=)("|')(\/\/)/g, '$1$2' + document.location.protocol + '//');
[].map.call(htmlDoc.body.childNodes, function (node) {
var newNode = node.cloneNode(true);
var prepareNode = function (element) {
var scriptNode = '';
if (element.nodeName.toLowerCase() == 'script') {
scriptNode = document.createElement('script');
scriptNode.type = 'text/javascript';
if (element.hasAttribute('src') === false) {
scriptNode.textContent = element.textContent;
} else {
scriptNode.src = element.src;
}
}
return scriptNode;
};
var preparedNodeData = prepareNode(node);
if (preparedNodeData != '') newNode = preparedNodeData;
for (var i = 0; i < node.childNodes.length; i++) {
try {
var childNode = newNode.childNodes[i];
if (childNode.nodeName.toLowerCase() == 'script') {
childNode.parentNode.removeChild(childNode);
}
var preparedScriptData = prepareNode(childNode);
if (preparedScriptData != '') newNode.appendChild(preparedScriptData);
} catch (e) { }
}
if (receiverNode != undefined) receiverNode.appendChild(newNode);
});
};
Tagging.prototype.get_source = function(slug, context) {
if (typeof this.positions[slug] == 'undefined') {
// This position doesn't exist, call the autodetect
this._detect(slug);
} else {
var position = this.get_json(this.positions[slug]);
var tag_source = "";
if (this.positions_weighted[slug]) {
tag_source = this.random_tag(position)
} else {
tag_source = this.all_tags(position);
}
// Replace variables
var variables = {};
for (variable in this.context) {
variables[variable] = this.context[variable];
}
for (variable in context) {
variables[variable] = context[variable];
}
tag_source = this._replace_variables(tag_source, variables);
// Remove unset variables and return result
return tag_source.replace(/\$\w+/g, "").replace(/\${\w+}/g, "");
}
return '';
}
Tagging.prototype.log = function(err) {
//this._load(error_endpoint);
}
Tagging.prototype._load = function(url) {
var head = document.getElementsByTagName('head')[0] || document.documentElement,
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);
}
Tagging.prototype._detect = function(position) {
this._load(this.autodetect_url + this.site +'/'+ this.zone +'/' + position + '/');
}
Tagging.prototype._replace_variables = function(content, variables) {
var RE_VALID_IDENT = /^[a-zA-Z_]\w*$/;
for (variable in variables) {
if (RE_VALID_IDENT.test(variable)) {
var regexes = [new RegExp('\\$' + variable, 'g'), new RegExp('\\${' + variable + '}', 'g')];
for (re in regexes) {
content = content.replace(regexes[re], variables[variable]);
}
} else {
throw "'" + variable + "' is not a valid identifier.";
}
}
return content;
}