/*global NYTD, window, escape */
/*
$Id: googleads.js 49969 2010-11-15 20:02:14Z helckt $
(c) 2010 The New York Times Company
*/
NYTD.GoogleAds = NYTD.GoogleAds || {
defaultGoogleParamsList: {
AFS:{
'google_ad_output': 'js',
'google_afs_ad': 'w2n4',
'google_afs_client': 'newyorktimes',
'google_afs_query': null,
'google_afs_adsafe': 'high',
'google_afs_channel': null,
'google_afs_adtest': 'off'
},
AFC:{
'google_adtest': 'off',
'google_ad_type': 'text',
'google_ad_client': 'nytimes_article_var',
'google_ad_output': 'js',
'google_ad_channel': null,
'google_skip': 0,
'google_safe': 'high',
'google_hints': null,
'google_contents': null,
'google_page_url': null,
'google_targeting': null,
'google_image_size': null,
'google_max_num_ads': 3
},
RAD:{
'google_adtest': 'off',
'google_ad_client': 'nytimes_article_var',
'google_ad_output': 'js',
'google_ad_channel': null,
'google_safe': 'high',
'google_page_url': null,
'google_max_num_ads': 0,
'google_num_radlinks': 5,
'google_max_radlink_len': 23,
'google_kw_type': 'broad',
'google_kw': null
},
LB:{
'google_ad_client': 'ca-nytimes_display_html',
'google_alternate_ad_url': 'http://www.nytimes.com/ads/remnant/networkredirect-leaderboard.html',
'google_ad_width': 728,
'google_ad_height': 90,
'google_ad_format': '728x90_pas_abgc',
'google_ad_type': 'image',
'google_safe': 'high',
'google_targeting': 'site',
'google_ad_channel': null
}
},
cpcCount: 0,
metaTagCache: {},
debugElement: {},
targetList: {
'sponLink': ['adxSponLink', 'SponLink', 'adxSponLinkA', 'SponLinkA'],
'sponLinkA': ['adxSponLinkA', 'SponLinkA', 'adxSponLink', 'SponLink'],
'middleRight': ['MiddleRight', 'SFMiddle']
},
adTarget: '',
templates: {
"afsWideAd" :
'
',
"afsNarrowAd" :
'' +
'
<%=line1%> ' +
'
<%=line2%>
<%=line3%>
' +
'
' +
'
',
"radLink":
'',
"afcAd":{
'text':
'' +
'' +
'<%=line1%> ' +
'<%=line2%> ' +
'<%=line3%> ' +
'<%=visibleUrl%> ' +
' ',
'image':
'' +
' ' +
' ',
'flash':
'' +
' ' +
' ' +
' ' +
' ' +
' '
}
},
loadCss: function() {
// check if googleads.css is already loaded:
var cssLoaded = 0;
var linkNodes = $$('link');
if(linkNodes.any(function(x) {
return x.getAttribute('href').indexOf("2.0/styles.css") != -1 ;
})) { cssLoaded = 1; }
if(!cssLoaded) {
var cssLink =document.createElement("link");
cssLink.setAttribute("rel", "stylesheet")
cssLink.setAttribute("type", "text/css")
cssLink.setAttribute("href", NYTD.Hosts.cssHost + "/css/0.1/screen/common/googleads.css")
$$("head")[0].appendChild(cssLink)
}
},
getQueryParam: function(defaultQuery, key) {
key = key || "query";
var query = window.location.toString().toQueryParams()[key] || defaultQuery;
if (!query) {
query = $$('meta[name="CG"]')[0];
query = query?query.getAttribute("content"):null;
}
else {
query = decodeURI(query);
query = query.replace(/'/g, "\\'");
}
return query;
},
getFirstGood: function() {
for(var i = 0; i < arguments.length; i++) {
if("undefined" !== typeof(arguments[i]) && "undefined" !== arguments[i] && null !== arguments[i] && '' !== arguments[i] && arguments[i] === arguments[i]) {
return arguments[i];
}
}
return null;
},
getParsedMetaTag: function( metaTagName ) {
if ( !this.metaTagCache[metaTagName] ) {
var metaTagValue = '';
if ( !this.metaTagCache.metaTags ) {
this.metaTagCache.metaTags = $('meta');
}
for ( var m in this.metaTagCache.metaTags ) {
if ( this.metaTagCache.metaTags[m].name == metaTagName ) {
metaTagValue = this.metaTagCache.metaTags[m].content;
metaTagValue = metaTagValue.replace( /[\s\,\.]+/g, '_' );
break;
}
}
this.metaTagCache[metaTagName] = metaTagValue;
}
return this.metaTagCache[metaTagName];
},
setDebugElement: function(adProgram) {
var w = window;
var s1 = "", s2 = "";
if(window.location.search.match(/ga_debug=1/)) {
s1 += "
";
NYTD.GoogleAds.debugElement.html = s1;
NYTD.GoogleAds.debugElement.text = s2;
}
},
getGoogleAds: function(adProgram, paramList) {
if(paramList === undefined) { return; }
NYTD.GoogleAds.preserveAppParams();
NYTD.GoogleAds.resolveGoogleVars(adProgram, paramList);
NYTD.GoogleAds.setDebugElement(adProgram);
if(adProgram === "AFS") {
window.google_afs_request_done = NYTD.GoogleAds.googleAfsRequestDone;
NYTD.require('http://www.google.com/afsonline/show_afs_ads.js', true);
}
else if(adProgram === "AFC" || adProgram === "RAD" || adProgram === "LB") {
window.google_ad_request_done = NYTD.GoogleAds.googleAdRequestDone;
window.google_radlink_request_done = NYTD.GoogleAds.googleRadlinkRequestDone;
NYTD.require('http://pagead2.googlesyndication.com/pagead/show_ads.js', true);
}
},
preserveAppParams: function() {
/* look for the GoogleAds global variables that might be set by the app and save them. They
* may or may not be suffixed with 'nyt_'. This is called for each GoogleAds position but
* only does anything the first time it is called.
*/
var w = window;
if(!this.appParams) {
this.appParams = {};
$H(this.defaultGoogleParamsList).each(function(pair) {
$H(pair.value).each(function(pair2) {
if(w['nyt_' + pair2.key] || w[pair2.key]) {
this.appParams[pair2.key] = this.getFirstGood(w['nyt_' + pair2.key], w[pair2.key]);
}
}, this);
}, this);
}
},
clearGoogleParams: function(adProgram) {
var w = window;
$H(this.defaultGoogleParamsList[adProgram]).each(function(pair) {
w[pair.key[0]] = null;
}, this);
},
resolveGoogleVars: function(adProgram, adxParams) {
var w = window;
$H(this.defaultGoogleParamsList[adProgram]).each(function(pair){
var param = pair.key;
var defaultValue = pair.value;
if(adxParams[param] || this.appParams[param] || defaultValue) {
w[param] = this.getFirstGood(this.appParams[param], adxParams[param], defaultValue);
}
}, this);
if(adProgram === "AFS") {
w.google_afs_query = this.getQueryParam();
w.google_afs_channel = [this.appParams.google_afs_channel, adxParams.google_afs_channel].join(' ');
w.google_afs_adtest = NYTD.env === "production" ? "off" : "on";
this.narrowPosition = this.getFirstGood(adxParams.narrowPosition, 'SponLink');
this.widePosition = this.getFirstGood(adxParams.widePosition, 'SponLinkA');
this.relatedAdsPosition = this.getFirstGood(adxParams.relatedAdsPosition, 'SponLink');
}
else if(adProgram === "AFC") {
w.google_ad_channel = [this.appParams.google_ad_channel, adxParams.google_ad_channel].join(' ');
w.google_skip = this.getFirstGood(NYTD.GoogleAds.cpcCount, 0);
w.google_adtest = NYTD.env === "production" ? "off" : "on";
if(adxParams.ad_target) {
this.adTarget = adxParams.ad_target;
}
else if(adxParams.ad_target_list && this.targetList[adxParams.ad_target_list]) {
this.adTarget = this.targetList[adxParams.ad_target_list].find(function(t){ return($(t) && !$(t + '_googleAds')); });
}
}
else if(adProgram === "RAD") {
w.google_ad_channel = [this.appParams.google_ad_channel, adxParams.google_ad_channel].join(' ');
w.google_adtest = NYTD.env === "production" ? "off" : "on";
this.ad_target = adxParams.ad_target;
}
else if(adProgram === "LB") {
w.google_ad_channel = [this.appParams.google_ad_channel, adxParams.google_ad_channel].join(' ');
w.google_adtest = NYTD.env === "production" ? "off" : "on";
this.ad_target = adxParams.ad_target;
}
},
/*
* These are the Google Callback Functions. They need to be in the Global Namespace
* so that google code can call them. In our old GoogleAds code, these functions are
* defined in the ad position. Since there is still the chance that old code could
* appear on the page, and overwrite these functions, we define them here as methods
* of NYTD.GoogleAds, and put them into the global namespace in GetGoogleAds() each
* time we need them.
*/
/* Google code will call this function when AFS ads are requested */
googleAfsRequestDone: function(ads) {
// Verify that there are actually ads to display. If not, try to get Radlinks
var google_num_ads = ads.length;
if (google_num_ads <= 0) {
NYTD.GoogleAds.getGoogleAds('RAD', {'ad_target':NYTD.GoogleAds.narrowPosition});
}
var wideAds = ""; //wide ad unit html text
var narrowAds = ""; //narrow ad unit html text
for(i = 0; i < google_num_ads; i++){
var values = {
url: ads[i].url,
line1: ads[i].line1,
line2: ads[i].line2,
visibleUrl: ads[i].visible_url
};
if (ads[i].type=="text/wide"){
wideAds += NYTD.Template(NYTD.GoogleAds.templates.afsWideAd, values);
} else {
values.line3 = ads[i].line3;
narrowAds += NYTD.Template(NYTD.GoogleAds.templates.afsNarrowAd, values);
}
}
if (narrowAds !== "") {
narrowAds = '';
}
else {
// if there are no narrow ads, request related ads
NYTD.GoogleAds.getGoogleAds('RAD', {'ad_target':NYTD.GoogleAds.narrowPosition});
}
if (wideAds !== "") {
wideAds = '';
}
if($(NYTD.GoogleAds.widePosition)) { $(NYTD.GoogleAds.widePosition).innerHTML = wideAds; }
if($(NYTD.GoogleAds.narrowPosition)) { $(NYTD.GoogleAds.narrowPosition).innerHTML = narrowAds; }
NYTD.GoogleAds.clearGoogleParams('AFS');
},
/* Google code will call this function when RADLINK ads are requested */
googleRadlinkRequestDone: function(radlinks) {
if (radlinks.length === 0) { return; }
var s = "";
var radlinksStr = '';
NYTD.GoogleAds.radCount = NYTD.GoogleAds.radCount + radlinks.length || radlinks.length;
for (i = 0; i < radlinks.length; ++i) {
var values = {
queryHost: NYTD.GoogleAds.queryHost,
term: radlinks[i].term,
thisPage: escape(window.location)
};
radlinksStr += NYTD.Template(NYTD.GoogleAds.templates.radLink, values);
}
s += '\n';
if($(NYTD.GoogleAds.ad_target)) { $(NYTD.GoogleAds.ad_target).innerHTML = s; }
NYTD.GoogleAds.clearGoogleParams('RAD');
},
/* Google code will call this function when AFC ads are requested */
googleAdRequestDone: function(ads) {
var ga = NYTD.GoogleAds;
if(ads && typeof(ads) === 'object' && typeof(ads[0]) !== 'undefined' && $(ga.adTarget)) {
var className = 'nytd_google_ads';
if (ads.length === 1) {
className = 'nytd_google_ads nytd_google_single_ad';
}
var s = ga.debugElement.html || '';
var afc = "";
var adType = ads[0].type;
if(adType === 'text') {
s += ''; }
else { s += afc; }
$(ga.adTarget).innerHTML = s;
}
ga.clearGoogleParams('AFC');
return;
}
};
Event.observe(window, 'load', NYTD.GoogleAds.loadCss);