/*global URI, console */
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];

(function () {
  var gads = document.createElement('script');
  gads.async = true;
  gads.type = 'text/javascript';

  var useSSL = 'https:' === document.location.protocol;
  gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';

  var node = document.getElementsByTagName('script')[0];
  node.parentNode.insertBefore(gads, node);
})();

(function ($) {
  googletag.cmd.push(function () {
    if (typeof PP !== "undefined") {
      if (PP.channel) googletag.pubads().setTargeting("channel", PP.channel);
      if (PP.channelMetaGame) googletag.pubads().setTargeting("game", PP.channelMetaGame);
      if (URI().query(true).campaign) googletag.pubads().setTargeting("campaign", URI().query(true).campaign);
      googletag.pubads().setTargeting("server", Twitch.deployFlavor);
    }
    googletag.pubads().collapseEmptyDivs(true);  // Hide slots when no fill
    if (window.SitePageType === 'front_page' || window.SitePageType === 'channel') {
      googletag.pubads().enableSingleRequest();
      googletag.pubads().disableInitialLoad();
    }
  });

})(jQuery);
