Tagging = function(context) {
  this.site = 'oldalinfo';
  this.zone = 'nyitooldal';
  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\": \"<script src=\\"//stat.p24.hu/mobile_detect/mobile_detect.js\\"></script>\"}, {\"service_name\": \"Gemius Traffic\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\">    var properGemId = (\'Akg6D_9IJ9QlB2IltOv9Y5dSLZqsTOcc.Izrd4Txk7n..7\' != \'\' ? \'Akg6D_9IJ9QlB2IltOv9Y5dSLZqsTOcc.Izrd4Txk7n..7\' : \'bJGan9fENxV1TU0lGz0SNsWnTIPZsx.mK21bodMuP6r.U7\');        if(typeof _ceDeviceDetect.getDeviceType() !== \'undefined\' && _ceDeviceDetect.getDeviceType() != \'desktop\' && \'${gemius_trackid_for_mobile_device}\' != \'\') {        properGemId = \'${gemius_trackid_for_mobile_device}\';    }       var pp_gemius_identifier = properGemId;    function gemius_pending(i) { window[i] = window[i] || function() {var x = window[i+\'_pdata\'] = window[i+\'_pdata\'] || []; x[x.length]=arguments;};};    gemius_pending(\'gemius_hit\'); gemius_pending(\'gemius_event\'); gemius_pending(\'pp_gemius_hit\'); gemius_pending(\'pp_gemius_event\');    (function(d,t) {try {var gt=d.createElement(t),s=d.getElementsByTagName(t)[0],l=\'http\'+((location.protocol==\'https:\')?\'s\':\'\'); gt.setAttribute(\'async\',\'async\');    gt.setAttribute(\'defer\',\'defer\'); gt.src=l+\'://gahu.hit.gemius.pl/xgemius.js\'; s.parentNode.insertBefore(gt,s);} catch (e) {}})(document,\'script\');</script>\"}, {\"service_name\": \"Sanoma\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\" src=\\"//ad-blacklist.p24.hu/adx_blacklist.js\\"></script><script type=\\"text/javascript\\" src=\\"//ad-blacklist.p24.hu/rubicon_blacklist.js\\"></script><script type=\\"text/javascript\\">    (function(){        var blacklistsToCheck = [\'ADX\', \'RUBICON\'];        for (var i=0; i<blacklistsToCheck.length; i++) {            var typeName = blacklistsToCheck[i].charAt(0) + blacklistsToCheck[i].substr(1).toLowerCase();            window[\'no\' + blacklistsToCheck[i]] = false;            if (                typeof window[\'blck\' + typeName] != \'undefined\' &&                typeof window[\'blck\' + typeName][window.location.hostname] != \'undefined\' &&                window[\'blck\' + typeName][window.location.hostname].indexOf(location.pathname) >= 0            ) {                window[\'no\' + blacklistsToCheck[i]] = true;            }        }    })();</script>\"}, {\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script>if (typeof _ceDeviceDetect != \'undefined\' && typeof _ceDeviceDetect.getDeviceType == \'function\') {    var _ceDisplayData = _ceDisplayData || {};    _ceDisplayData.gpt = _ceDisplayData.gpt || { items: [], defContName: \'div-gpt-ad\' };        (function(){        var scriptInserter = document.createElement(\'script\');        scriptInserter.src = \'https://www.googletagservices.com/tag/js/gpt.js\';        scriptInserter.async = true;        /*document.head.appendChild(scriptInserter);*/        document.head.insertBefore(scriptInserter, document.head.firstChild);    })();        var googletag = googletag || {};    googletag.cmd = googletag.cmd || [];    googletag.cmd.push(function() {        googletag.pubads().enableSingleRequest();        googletag.pubads().collapseEmptyDivs();        googletag.enableServices();    });}</script>\"}]';
  this.positions_weighted['head'] = false;
  
  this.positions['body-start'] = '[{\"service_name\": \"Google Tag Manager\", \"weight\": null, \"source\": \"<noscript><iframe src=\\"//www.googletagmanager.com/ns.html?id=GTM-N2KCDSN\\"height=\\"0\\" width=\\"0\\" style=\\"display:none;visibility:hidden\\"></iframe></noscript><script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({\'gtm.start\':new Date().getTime(),event:\'gtm.js\'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!=\'dataLayer\'?\'&l=\'+l:\'\';j.async=true;j.src=\'//www.googletagmanager.com/gtm.js?id=\'+i+dl;f.parentNode.insertBefore(j,f);})(window,document,\'script\',\'dataLayer\',\'GTM-N2KCDSN\');</script>\"}]';
  this.positions_weighted['body-start'] = false;
  
  this.positions['body-end'] = '[{\"service_name\": \"Sanoma/Oldal.info\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\" src=\\"//stat.p24.hu/CTS/measuring-codes/oldalInfo/desktop/measuringCode.min.js\\"></script>\"}, {\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\" src=\\"//stat.p24.hu/CTS/helper-scripts/waterfall_empty/closer.min.js\\"></script>\"}, {\"service_name\": \"Sanoma/Oldal.info\", \"weight\": null, \"source\": \"<script>(function () {    var prototypeFix = function () {            var prototypes = [\'Object\', \'Array\', \'Hash\', \'String\'];            for (var i = 0; i < prototypes.length; i++) {                if (typeof window[prototypes[i]] !== \'undefined\') {                    delete window[prototypes[i]].prototype.toJSON;                }            }        },        styleChanges = function () {            var adverticumElements = document.getElementsByClassName(\'goAdverticum\');            for (var i = 0; i < adverticumElements.length; i++) {                adverticumElements[i].style.display = \\"table\\";                adverticumElements[i].style.margin = \\"0 auto\\";            }        };    prototypeFix();    styleChanges();})();</script>\"}, {\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\" src=\\"//ad.adverticum.net/g3.js\\"></script>\"}, {\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\">    (function(){        var googleCPLink = (\\"${google_cookie_policy_link}\\" != \\"\\") ? \\"${google_cookie_policy_link}\\" : \\"http://centralmediacsoport.hu/Company/Central-Digitalis-Media-Adatkezelesi-tajekoztato\\";        var googleCPRename = (\\"0\\" != \\"\\") ? \\"0\\" : 1;            window.cookieconsent_options = {            \\"message\\"   : \\"A weboldalon cookie-kat haszn&#xE1;lunk, hogy biztons&#xE1;gos b&#xF6;ng&#xE9;sz&#xE9;s mellett a legjobb felhaszn&#xE1;l&#xF3;i &#xE9;lm&#xE9;nyt ny&#xFA;jthassunk.\\",            \\"dismiss\\"   : \\"Rendben!\\",            \\"learnMore\\" : \\"R&#xE9;szletes le&#xED;r&#xE1;s\\",            \\"link\\"      : googleCPLink,            \\"theme\\"     : \\"//stat.p24.hu/CTS/helper-scripts/cookieconsent/styles/custom.css\\",            \\"expiryDays\\": 1095,            \\"renamed\\"   : googleCPRename        };    })();</script><script type=\\"text/javascript\\" src=\\"//stat.p24.hu/CTS/helper-scripts/cookieconsent/cookieconsent.min.js\\"></script>\"}]';
  this.positions_weighted['body-end'] = false;
  
  this.positions['body-end-adv'] = '[]';
  this.positions_weighted['body-end-adv'] = false;
  
  this.positions['top'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><div style=\\"${google_adx_style}\\"><ins class=\\"adsbygoogle\\"         style=\\"display:inline-block;width:468px;height:60px;\\"         data-ad-client=\\"ca-pub-8962558862962174\\"         data-ad-slot=\\"8253638069\\"         data-max-num-ads =${google_max_num_ads}></ins></div><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['top'] = false;
  
  this.positions['content-top'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 728px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:728px;height:90px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"8253638069\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['content-top'] = false;
  
  this.positions['content-bottom'] = '[{\"service_name\": \"Sanoma/Oldal.info\", \"weight\": null, \"source\": \"<div style=\\"${google_adx_style}\\"><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"margin-right: 4px; display:inline-block;width:315px;height:280px\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"8737989266\\"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:315px;height:280px\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"9795920061\\"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>\"}]';
  this.positions_weighted['content-bottom'] = false;
  
  this.positions['left010'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 170px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:170px;height:240px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"1541346869\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['left010'] = false;
  
  this.positions['left020'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 170px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:170px;height:240px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"5529064461\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['left020'] = false;
  
  this.positions['left030'] = '[]';
  this.positions_weighted['left030'] = false;
  
  this.positions['right010'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 120px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:120px;height:240px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"1901273664\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['right010'] = false;
  
  this.positions['right020'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 120px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:120px;height:240px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"8002376062\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['right020'] = false;
  
  this.positions['right030'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 120px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:120px;height:240px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"9625383263\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['right030'] = false;
  
  this.positions['content010'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<div style=\\"width: 420px; text-align: right; font-size: 8px; color: #C6C6C6; margin: 0 auto;\\">HIRDET&#xC9;S</div><script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:inline-block;width:420px;height:260px;${custom_css}\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"5064669260\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['content010'] = false;
  
  this.positions['content020'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><div style=\\"margin-top: 10px; display: block;\\"><ins class=\\"adsbygoogle\\"         style=\\"display:inline-block;width:368px;height:70px;\\"         data-ad-client=\\"ca-pub-8962558862962174\\"         data-ad-slot=\\"5849079262\\"         data-max-num-ads =1></ins></div><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['content020'] = false;
  
  this.positions['content030'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><div style=\\"margin-top: 10px;display: block;\\"><ins class=\\"adsbygoogle\\"         style=\\"display:inline-block;width:368px;height:70px;\\"         data-ad-client=\\"ca-pub-8962558862962174\\"         data-ad-slot=\\"2197608867\\"         data-max-num-ads =1></ins></div><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['content030'] = false;
  
  this.positions['content040'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><div style=\\"margin-top: 10px;display: block;\\"><ins class=\\"adsbygoogle\\"         style=\\"display:inline-block;width:368px;height:70px;\\"         data-ad-client=\\"ca-pub-8962558862962174\\"         data-ad-slot=\\"6348606862\\"         data-max-num-ads =1></ins></div><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['content040'] = false;
  
  this.positions['content025'] = '[]';
  this.positions_weighted['content025'] = false;
  
  this.positions['content035'] = '[]';
  this.positions_weighted['content035'] = false;
  
  this.positions['content050'] = '[]';
  this.positions_weighted['content050'] = false;
  
  this.positions['content005'] = '[]';
  this.positions_weighted['content005'] = false;
  
  this.positions['content025'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 4460366 --><div id=\\"zone4460366\\" class=\\"goAdverticum a8a263be\\" data-adsv-name=\\"box1\\"></div>\"}]';
  this.positions_weighted['content025'] = false;
  
  this.positions['content035'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 4461562 --><div id=\\"zone4461562\\" class=\\"goAdverticum a8a263be\\" data-adsv-name=\\"box2\\"></div>\"}]';
  this.positions_weighted['content035'] = 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.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;
}



