(function() {
var $$ = msnbc.namespace("msnbc.ads");
$$.DapAd = msnbc.core.EventDispatcher.extend({
init: function(style, placement, slot, index) {
this._super();
this.ACTIVE_CLASS = 'dap-advanced';
this.style = style;
this.placement = placement;
this.slot = slot;
this.index = index;
this.valid = false;
this.activePlaceholder = true;
this.matchOptions();
this.translateSlot();
},
translateSlot: function() {
var self = this;
$(this.slot).attr('id', this.group + '-' + this['package'] + '-' + this.index)
.removeClass('ad-data')
.addClass(self.ACTIVE_CLASS);
},
importOptions: function(options) {
var self = this;
jQuery.each(['group', 'package', 'width', 'height', 'hide', 'placement', 'style'],
function() {
self[this] = self[this] || options[this] || false;
});
var t = this;
if (t.group && t["package"]) {
t.valid = true;
}
if (t.style) {
t.activePlaceholder = false;
}
},
matchOptions: function() {
var options = {};
var sets = $(this.slot).text().match(/([\w\d]+)\s*?[\:=]\s*?([\w\d]+)/g);
if (sets) {
for (var i = 0; i < sets.length; i++) {
var kv = sets[i].match(/([\w\d]+)\s*?[\:=]\s*?([\w\d]+)/);
options[kv[1]] = kv[2];
}
} else {
// something's messed up in the input or the regex
}
this.importOptions(options);
},
transpose: function(new_slot) {
$(new_slot).replaceWith(this.slot);
$(this.slot).addClass('transposed');
},
replaceLabel: function(){
$(this.slot).find("iframe[id^='acbLbl']").hide();
// workaround IE double call
$(this.slot).find('div.adlabel').remove();
$(this.slot).prepend('
');
},
render: function() {
var self = this;
// '&PG=NBCMSB&AP=1089','300','250'
if (!this.activePlaceholder || !window.dapMgr) return;
var gp = "&PG=" + this.group + "&AP=" + this["package"];
try {
dapMgr.renderAd(this.slot.id, gp, parseInt(this.width), parseInt(this.height));
var ifr = $(this.slot).find('iframe');
if (ifr.length>0) {
ifr = ifr[0];
if (jQuery.browser.msie) {
ifr.onreadystatechange = function(){
self.replaceLabel();
};
} else {
ifr.onload = function(){
self.replaceLabel();
};
}
} else {
self.replaceLabel();
}
} catch(e) {
//console.log(e);
}
},
refresh: function(div, ad) {
try {
//$(div).empty();
for (var i = ad.length - 1; i >= 0; i--) {
var ad = ad[i];
ad.render();
};
} catch(e) {
msnbc.log("Ad refresh() error");
msnbc.log(e);
}
}
});
$$.PulseAd = $$.DapAd.extend({
init: function(style, placement, slot, index) {
this.SVC = "http://context1.kanoodle.com/cgi-bin/kontext.cgi?";
this.DEFAULTS = {
id: 78053631,
db: 'context',
query: '*general_network',
cgroup: 'bizecon',
format: 'json',
numresults: 4
};
this.data = {};
this.opts = {};
this._super(style, placement, slot, index);
this.ACTIVE_CLASS = 'pulse-ad';
},
translateSlot: function() {
var self = this;
$(this.slot).attr('id', 'pulse-' + this.opts.cgroup + '-' + this.index)
.removeClass('pulse-data')
.addClass('pulse-ad');
},
importOptions: function(options) {
var self = this;
jQuery.each(['id', 'db', 'query', 'cgroup', 'format', 'numresults'],
function() {
self.opts[this] = options[this] || self.DEFAULTS[this] || false;
});
this.desiredResults = this.opts.numresults;
this.valid = true;
if (this.style) this.activePlaceholder = false;
},
getURL: function() {
return this.SVC + jQuery.param(this.opts);
},
render: function() {
//[ { title:"", display_url:"", description:"", click_url:""}... ]
var m = '';
jQuery.each(this.data.listings,
function() {
m += '';
m += '' + this.title + '';
m += '' + this.description + '';
m += '' + this.display_url + '';
m += '
';
});
$(this.slot).append(m).addClass('rendered');
}
});
$$.DapCollection = msnbc.core.EventDispatcher.extend({
init: function(adClazz) {
this._super();
this.DATA_CLASS = 'ad-data';
this.PLACEHOLDER_CLASS = 'ad-placeholder';
this.adClazz = adClazz;
this.ads = [];
this.adsByStyle = {};
this.adsByID = {};
this.selector;
},
activate: function(selectr) {
var self = this;
var dataExp = new RegExp(this.DATA_CLASS + "\s?");
var ad;
var clazz = msnbc.namespace(self.adClazz);
if (!clazz.prototype) throw 'Unrecognized classname: ' + self.adClazz;
$(selectr).each(function(i) {
var kind = $(this).attr("class").split(' ');
var style = kind[1] || '';
var placement = kind[2] || '';
ad = new clazz(style, placement, this, i);
if (ad.valid) {
$(this).empty();
/*Ads by Style*/
self.adsByStyle[style] = self.adsByStyle[style] || [];
self.adsByStyle[style].push(ad);
/*Ads by ID*/
self.adsByID[ad.slot.id] = self.adsByID[ad.slot.id] || [];
self.adsByID[ad.slot.id].push(ad);
/*Ads (general)*/
self.ads.push(ad);
} else {
// record invalid ads?
}
});
this.selector = '.' + ad.ACTIVE_CLASS;
},
getAd: function(styleOrIndex, placement) {
if (typeof(styleOrIndex) == "number") {
return this.ads[styleOrIndex];
}
var style = styleOrIndex;
var bys = this.adsByStyle[style];
if (bys && bys.length) {
var match = [];
if (placement) {
jQuery.each(bys,
function() {
if (this.placement === placement) match.push(this);
});
} else {
match = bys;
}
if (match.length > 1) return match;
return match[0];
}
return false;
},
getAdsIn: function(selectr) {
var self = this;
var ads = [];
$(selectr).find(this.selector).each(function() {
var ad = self.adsByID[this.id];
if (ad) ads.push(ad);
});
return ads;
},
renderAll: function() {
for (var i = this.ads.length - 1; i >= 0; i--) {
var ad = this.ads[i];
ad.render();
};
},
transposeAll: function(new_selectr) {
var self = this;
$(new_selectr).each(function() {
var kind = $(this).attr("class").split(' ');
var style = kind[1] || '';
var placement = kind[2] || '';
var ad = self.getAd(style, placement);
if (ad) {
ad.activePlaceholder = true;
ad.transpose(this);
}
});
}
});
$$.PulseCollection = $$.DapCollection.extend({
init: function(adClazz) {
this._super(adClazz);
this.DATA_CLASS = 'pulse-data';
this.PLACEHOLDER_CLASS = 'pulse-placeholder';
this.currIndex = null;
},
requestAdData: function(ad) {
var self = this;
var url = ad.getURL();
try {
jQuery.getScript(url,
function(d, s) {
self.onDataSuccess(d, s);
});
} catch(e) {
this.onError(this, 'before ajax request', e);
}
},
renderAll: function() {
if (this.ads.length && this.ads.length > 0) {
this.currIndex = 0;
this.requestAdData(this.ads[this.currIndex]);
}
},
onDataSuccess: function(data, textStatus) {
// window.pulse_ads .listings
if (window.pulse_ads) {
var currAd = this.ads[this.currIndex];
currAd.data = jQuery.extend({},
window.pulse_ads);
currAd.render();
if (this.currIndex < this.ads.length - 1) {
// and call the next
this.requestAdData(this.ads[++this.currIndex]);
} else {
//done rendering pulse360 ads
}
} else {
// something is wrong, throw an error.
this.onError('', 'bad data after successful $.getScript');
}
},
onError: function(req, status, e) {
msnbc.warn("[msnbc.modules.KanoodleWrapper::onError] " + e + "/" + status);
}
});
$$.DapProxy = {
dapStandIn: function(grp_pkg, w, h, ob) {
$$.DapProxy.ads = $$.DapProxy.ads || [];
$$.DapProxy.ads.push({
gp: grp_pkg,
w: w,
h: h
});
},
replaceCalls: function() {
if ($$.DapProxy.disabled==true) {
return;
}
var self = $$.DapProxy;
var ads = jQuery.map(self.ads,
function(n) {
var split = n.gp.match(/&(\w+)=([\d\w]+)/g);
if (split) {
for (var i = 0; i < split.length; i++) {
var gp = split[i].match(/&(\w+)=([\d\w]+)/);
if (gp[1] == "PG") n.group = gp[2];
else if (gp[1] == "AP") n.pkg = gp[2];
}
}
return n;
});
$('body script').each(function() {
// look through the script in the body to find the function that called this...
var script = this;
ads = jQuery.grep(ads,
function(ad) {
var search = "dap[(]['\"]" + ad.gp + "['\"],\s?['\"]?" + ad.w + "['\"]?,\s?['\"]?" + ad.h + "['\"]?[)]";
var sregx = new RegExp(search);
var match = sregx.exec($(script).text());
if (match) {
// replace the script block with a div shim
var m = "group:" + ad.group + "\n";
m += "package:" + ad.pkg + "\n";
m += "width:" + ad.w + "\n";
m += "height:" + ad.h + "\n";
$(script).replaceWith('' + m + '
');
return false;
// found this ad, remove from search list
}
return true;
// didn't find this ad, keep it in
});
});
},
revert: function() {
// reset to original dap
// doesn't work ...
$$.DapProxy.dapStandIn = $$.DapProxy.oldDap;
}
};
})();
(function() {
if (window.dap) {
msnbc.ads.DapProxy.oldDap = dap;
dap = msnbc.ads.DapProxy.dapStandIn;
} else {
window.dap = function() {};
msnbc.ads.DapProxy.disabled = true;
}
})();