Tagging = function(context) {
  this.site = 'eoldal';
  this.zone = 'utazas';
  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\": \"Google Adverts\", \"weight\": null, \"source\": \"<script type=\\"text/javascript\\" src=\\"//ad-blacklist.p24.hu/adx_blacklist.js\\"></script><script type=\\"text/javascript\\">    var noADX = false;    if (        typeof blckAdx != \'undefined\' &&        typeof blckAdx[window.location.hostname] != \'undefined\' &&        blckAdx[window.location.hostname].indexOf(location.pathname) >= 0    ) {        noADX = true;    }</script>\"}]';
  this.positions_weighted['head'] = false;
  
  this.positions['body-start'] = '[]';
  this.positions_weighted['body-start'] = false;
  
  this.positions['body-end'] = '[{\"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\\">    var googleCPLink = (\\"${google_cookie_policy_link}\\" != \\"\\") ? \\"${google_cookie_policy_link}\\" : \\"http://centralmediacsoport.hu/Company/Central-Digitalis-Media-Adatkezelesi-tajekoztato\\";        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    };</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'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 76216 --><div id=\\"zone76216\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['body-end-adv'] = false;
  
  this.positions['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\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"7844374464\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['top'] = false;
  
  this.positions['top-hl'] = '[{\"service_name\": \"Google Adverts\", \"weight\": 0, \"source\": \"<div style=\\"width: 300px; 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:300px;height:250px\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"4956456861\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}, {\"service_name\": \"Adverticum AdServer\", \"weight\": 100, \"source\": \"<!-- Goa3: 2228347 --><div id=\\"zone2228347\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['top-hl'] = true;
  
  this.positions['top-blog'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 1742457 --><div id=\\"zone1742457\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['top-blog'] = false;
  
  this.positions['top-other'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 2228345 --><div id=\\"zone2228345\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['top-other'] = false;
  
  this.positions['right010'] = '[]';
  this.positions_weighted['right010'] = false;
  
  this.positions['right020'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 2228346 --><div id=\\"zone2228346\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['right020'] = false;
  
  this.positions['sky-hl'] = '[{\"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:600px\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"1151056325\\"></ins><script>    if (typeof noADX == \'undefined\' || noADX === false) {        (adsbygoogle = window.adsbygoogle || []).push({});    }</script>\"}]';
  this.positions_weighted['sky-hl'] = false;
  
  this.positions['sky-user'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 78134 --><div id=\\"zone78134\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['sky-user'] = false;
  
  this.positions['sky-other'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 2228344 --><div id=\\"zone2228344\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['sky-other'] = false;
  
  this.positions['header010'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 2081568 --><div id=\\"zone2081568\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['header010'] = false;
  
  this.positions['header020'] = '[{\"service_name\": \"Adverticum AdServer\", \"weight\": null, \"source\": \"<!-- Goa3: 2081567 --><div id=\\"zone2081567\\" class=\\"goAdverticum\\"></div>\"}]';
  this.positions_weighted['header020'] = false;
  
  this.positions['bottom'] = '[]';
  this.positions_weighted['bottom'] = false;
  
  this.positions['content-bottom'] = '[{\"service_name\": \"Google Adverts\", \"weight\": null, \"source\": \"<script async src=\\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\\"></script><ins class=\\"adsbygoogle\\"     style=\\"display:block;margin-top:30px;margin-bottom:30px;\\"     data-ad-format=\\"autorelaxed\\"     data-ad-client=\\"ca-pub-8962558862962174\\"     data-ad-slot=\\"5168150069\\"></ins><script>     (adsbygoogle = window.adsbygoogle || []).push({});</script>\"}]';
  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.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;
}



