(function( openx, $, undefined ) {
+ var body = document.getElementsByTagName('body')[0];
+
+ var id;
+ var node;
+
var count = 0;
var slots = {};
- var ads = new Array();
+ var ads = [];
+
+
+ openx.show_ads = function(server, zones) {
+ document.write = render;
+ document.writeln = render;
- openx.fetch_ads = function(server, zones) {
+ var domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
- var spc;
+ var spc = document.createElement('script');
- spc = "<script type='text/javascript' src='";
- spc += location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
- spc += "/www/delivery/spc.php?zones=";
+ spc.type = 'text/javascript';
+ spc.async = false;
+ spc.defer = false;
+
+ spc.src = domain;
+ spc.src += "/www/delivery/spc.php?zones=";
/** Only fetch banners, that are really included in this page */
$('.oa').each(function() {
if (node.hasClass(name)) {
var id = 'oa_' + ++count;
slots[id] = node;
- spc += escape(id + '=' + zones[name] + "|");
+ spc.src += escape(id + '=' + zones[name] + "|");
}
}
});
- spc += "&nz=1&source=" + escape(OA_source);
- spc += "&r=" + Math.floor(Math.random()*99999999);
- spc += "&block=1&charset=UTF-8";
+ spc.src += "&nz=1&source=" + escape(OA_source);
+ spc.src += "&r=" + Math.floor(Math.random()*99999999);
+ spc.src += "&block=1&charset=UTF-8";
- if (window.location) spc += "&loc=" + escape(window.location);
- if (document.referrer) spc += "&referer=" + escape(document.referrer);
+ if (window.location) spc.src += "&loc=" + escape(window.location);
+ if (document.referrer) spc.src += "&referer=" + escape(document.referrer);
- spc+="'></script>";
+ spc.onload = init_ads;
+
+ body.appendChild(spc);
- document.write(spc);
- document.write("<script type='text/javascript' src='http://" + server + "/www/delivery/fl.js'></script>");
- }
+ var fl = document.createElement('script');
- openx.render_ads = function() {
+ fl.type = 'text/javascript';
+ fl.async = false;
+ fl.defer = false;
+
+ fl.src = domain + '/www/delivery/fl.js';
+
+ body.appendChild(fl);
+
+ }
+
+ function init_ads() {
- /** Render the fetched ad-banners... */
for (var id in slots) {
- // alert(id + ": " + OA_output[id]);
- if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '') {
- document.write("<div id='" + id + "'>");
- document.write(OA_output[id]);
- document.write("</div>");
+ if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
ads.push(id);
- // alert('Banner ' + id + ': ' + OA_output[id]);
- }
}
+ render_ad();
+
}
- openx.show_ads = function() {
+ function render_ad() {
- /** Show the rendered banners */
- for (var i=0; i<ads.length; i++) {
- var ad = $('#'+ads[i]).detach();
- slots[ads[i]].append(ad);
+ if (ads.length == 0) {
+ id = undefined;
+ node = undefined;
+ return;
}
+
+ id = ads.pop();
+ node = slots[id];
+
+ // node.append(id + ": " + node.attr('class'));
+
+ var result;
+ var script;
+ var src;
+ var inline;
+
+ while ((result = /<script/i.exec(OA_output[id])) != null) {
+ node.append(OA_output[id].slice(0,result.index));
+ /** Strip all text before "<script" from OA_output[id] */
+ OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
+ result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
+ if (result == null) {
+ /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
+ // alert(OA_output[id]);
+ OA_output[id] = "";
+ render_ad();
+ return;
+ }
+ /** Remember iinline-code, if present */
+ src = result[1]
+ inline = result[2];
+ /** Strip all text up to and including "</script>" from OA_output[id] */
+ OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
+ result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src);
+ if (result == null) {
+ /** script-tag with inline-code: execute inline-code! */
+ eval(inline);
+ }
+ else {
+ /** script-tag with src-URL! */
+ script = document.createElement('script');
+ script.type = 'text/javascript';
+ script.async = false;
+ script.defer = false;
+ script.src = result[1];
+ script.onload = render_ad;
+ /** The banner might not be rendered fully, or include more calls to document.write(). */
+ ads.push(id);
+ /** Load the script and halt all work until the script is loaded and executed... */
+ body.appendChild(script); // << The onload-event is only fired when appendChild is used!
+ return;
+ }
+ }
+ node.append(OA_output[id]);
+ OA_output[id] = "";
+
+ /** This statement will only reached, when no script-element was rendered! */
+ render_ad();
+
+ }
+
+ function render() {
+
+ if (id == undefined)
+ return;
+
+ var str = "";
+ for (var i=0; i < arguments.length; i++)
+ str += arguments[i];
+
+ OA_output[id] = str + OA_output[id];
+
}
} ( window.openx = window.openx || {}, jQuery ));