var
- settings, _zones, _options, domain, id, node,
+ settings, _options, domain, id, node,
count = 0,
slots = {},
+ min_width = {},
+ max_width = {},
+ width,
+ rendered = {},
+ visible = {},
+ rendering = false,
+ resize_timer,
queue = [],
- ads = [],
output = [];
- $.openx = function( zones, options ) {
-
- var name, src, errors = [], i;
+ /*
+ * Configuration-Options for jQuery.openx
+ *
+ * Since the domain-name of the ad-server is the only required parameter,
+ * jQuery.openx for convenience can be configured with only that one
+ * parameter. For example: "jQuery.openx('openx.example.org');". If more
+ * configuration-options are needed, they must be specified as an object.
+ * For example: "jQuery.openx({'server': 'openx.example.org', ... });".
+ *
+ *
+ * Server-Settings:
+ *
+ * server: string Name of the server, without protocol or port. For
+ * example "openx.example.org". This option is
+ * REQUIRED.
+ * protocol: Optional parameter.
+ * http: All connections to the ad-server are made via HTTP.
+ * https: All connections to the ad-server are made via HTTPS.
+ * If empty, document.location.protocol will be used.
+ * http_port: number Port-Number for HTTP-connections to the ad-server
+ * (only needed, when it is not the default-value 80).
+ * https_port: Port-Number for HTTPS-connections to the ad-server
+ * (only needed, when it is not the default-value 443).
+ *
+ *
+ * Seldom needed special Server-Settings (these parameters are only needed,
+ * if the default delivery-configration of the OpenX-Server was changed):
+ *
+ * path: string Path to delivery-scripts. DEFAULT: "/www/delivery".
+ * fl: string Flash-Include-Script. DEFAULT: "fl.js".
+ *
+ *
+ * Delivery-Options (for details and explanations see the see:
+ * http://www.openx.com/docs/2.8/userguide/single%20page%20call):
+ *
+ * block: 1 Don't show the banner again on the same page.
+ * 0 A Banner might be shown multiple times on the same
+ * page (DEFAULT).
+ * blockcampaign: 1 Don't show a banner from the same campaign again on
+ * the same page.
+ * 0 A Banner from the same campaign might be shown
+ * muliple times on the same page (DEFAULT).
+ * target: string The value is addes as the HTML TARGET attribute in
+ * the ad code. Examples for sensible values: "_blank",
+ * "_top".
+ * withtext: 1 Show text below banner. Enter this text in the
+ * Banner properties page.
+ * 0 Ignore the text-field from the banner-properties
+ (DEFAULT).
+ * charset: string Charset used, when delivering the banner-codes.
+ * If empty, the charset is guessed by OpenX. Examples
+ * for sensible values: "UTF-8", "ISO-8859-1".
+ *
+ *
+ * Other settings:
+ *
+ * selector: string A selector for selecting the DOM-elements, that
+ * should display ad-banners. DEFAULT: ".oa".
+ * See: http://api.jquery.com/category/selectors/
+ * min_prefix: string Prefix for the encoding of the minmal width as
+ * CSS-classname. DEFAULT: "min_".
+ * max_prefix: string Prefix for the encoding of the maximal width as
+ * CSS-classname. DEFAULT: "max_".
+ * resize_delay: number Number of milliseconds to wait, before a
+ * recalculation of the visible ads is scheduled.
+ * If the value is choosen to small, a recalculation
+ * might be scheduled, while resizing is still in
+ * progress. DEFAULT: 200.
+ * debug: boolean Turn on/off console-debugging. DEFAULT: false.
+ */
+ $.openx = function( options ) {
if (domain) {
if (console.error) {
console.error('jQuery.openx was already initialized!');
- console.log('Configured zones: ', _zones);
console.log('Configured options: ', _options);
}
return;
}
- _zones = zones;
+ /** Enable convenient-configuration */
+ if (typeof(options) == 'string')
+ options = { 'server': options };
+
_options = options;
- if (!options.server)
- errors.push('Required option "server" is missing!');
- if (errors.length > 0) {
+ if (!options.server) {
if (console.error) {
- for (i=0; i<errors.length; i++)
- console.error('Required option "server" is missing!');
+ console.error('Required option "server" is missing!');
console.log('options: ', options);
}
return;
settings = $.extend(
{
- 'protocol': document.location.protocol
+ 'protocol': document.location.protocol,
+ 'delivery': '/www/delivery',
+ 'fl': 'fl.js',
+ 'selector': '.oa',
+ 'min_prefix': 'min_',
+ 'max_prefix': 'max_',
+ 'resize_delay': 200,
+ 'debug': false
},
options
);
if (settings.protocol === 'https:' && settings.https_port)
domain += ':' + settings.https_port;
+ if (settings.debug && console.debug)
+ console.debug('Ad-Server: ' + domain);
+
/**
* Without this option, jQuery appends an timestamp to every URL, that
* is fetched via $.getScript(). This can mess up badly written
* third-party-ad-scripts, that assume that the called URL's are not
* altered.
*/
- $.ajaxSetup({ cache: true });
-
- src = domain + '/www/delivery/spc.php?zones=';
+ $.ajaxSetup({ 'cache': true });
- /** Only fetch banners, that are really included in this page */
- for(name in zones) {
- $('.oa').each(function() {
+ /**
+ * jQuery.openx only works with "named zones", because it does not know,
+ * which zones belong to which website. For mor informations about
+ * "named zones" see:
+ * http://www.openx.com/docs/2.8/userguide/single%20page%20call
+ *
+ * For convenience, jQuery.openx only fetches banners, that are really
+ * included in the actual page. This way, you can configure jQuery.openx
+ * with all zones available for your website - for example in a central
+ * template - and does not have to worry about performance penalties due
+ * to unnecessarily fetched banners.
+ */
+ for(name in OA_zones) {
+ $(settings.selector).each(function() {
var
- node = $(this),
- id;
- if (node.hasClass(name)) {
+ id,
+ classes,
+ i,
+ min = new RegExp('^' + settings.min_prefix + '([0-9]+)$'),
+ max = new RegExp('^' + settings.max_prefix + '([0-9]+)$'),
+ match;
+ if (this.id === name) {
id = 'oa_' + ++count;
- slots[id] = node;
- queue.push(id);
- src += escape(id + '=' + zones[name] + "|");
+ slots[id] = this;
+ min_width[id] = 0;
+ max_width[id] = Number.MAX_VALUE;
+ classes = this.className.split(/\s+/);
+ for (i=0; i<classes.length; i++) {
+ match = min.exec(classes[i]);
+ if (match)
+ min_width[id] = match[1];
+ match = max.exec(classes[i]);
+ if (match)
+ max_width[id] = match[1];
+ }
+ rendered[id] = false;
+ visible[id] = false;
+ if (settings.debug && console.debug)
+ console.debug(
+ 'Slot ' + count + ': ' + this.id + ' (' + min_width[id]
+ + (max_width[id] != Number.MAX_VALUE ? '-' + max_width[id] : '')
+ + ')'
+ );
}
});
}
- if (typeof OA_source !== 'undefined')
- src += "&source=" + escape(OA_source);
- src += "&nz=1&r=" + Math.floor(Math.random()*99999999);
- src += "&block=1&charset=UTF-8";
+ /** Add resize-event */
+ $(window).resize(function() {
+ clearTimeout(resize_timer);
+ resize_timer = setTimeout(recalculate_visible , settings.resize_timeout);
+ });
- if (window.location) src += "&loc=" + escape(window.location);
- if (document.referrer) src += "&referer=" + escape(document.referrer);
+ /** Fetch the JavaScript for Flash and schedule the initial fetch */
+ $.getScript(domain + settings.delivery + '/' + settings.fl, recalculate_visible);
+
+ }
- $.getScript(src, load_flash);
+ function recalculate_visible() {
+ width = $(document).width();
+ if (settings.debug && console.debug)
+ console.debug('Scheduling recalculation of visible banners for width ' + width);
+ if (!rendering)
+ fetch_ads();
+
}
- function load_flash() {
+ function fetch_ads() {
- $.getScript(domain + '/www/delivery/fl.js', init_ads);
+ /** Guide rendering-process for early restarts */
+ rendering = true;
+
+ if (settings.debug && console.debug)
+ console.debug('Starting recalculation of visible banners for width ' + width);
+
+ var name, src = domain + settings.delivery + '/spc.php';
+
+ /** Order banners for all zones that were found on the page */
+ src += '?zones=';
+ for(id in slots) {
+ visible[id] = width >= min_width[id] && width <= max_width[id];
+ if (visible[id]) {
+ if (!rendered[id]) {
+ queue.push(id);
+ src += escape(id + '=' + OA_zones[slots[id].id] + "|");
+ rendered[id] = true;
+ if (settings.debug && console.debug)
+ console.debug('Fetching banner ' + slots[id].id);
+ }
+ else {
+ /** Unhide already fetched visible banners */
+ if (settings.debug && console.debug)
+ console.debug('Unhiding already fetched banner ' + slots[id].id);
+ $(slots[id]).slideDown();
+ }
+ }
+ else {
+ /** Hide unvisible banners */
+ if (settings.debug && console.debug)
+ console.debug('Hiding banner ' + slots[id].id);
+ $(slots[id]).hide();
+ }
+ }
+ src += '&nz=1'; // << We want to fetch named zones!
+
+ /**
+ * These are some additions to the URL of spc.php, that are originally
+ * made in spcjs.php
+ */
+ src += '&r=' + Math.floor(Math.random()*99999999);
+ if (window.location) src += "&loc=" + escape(window.location);
+ if (document.referrer) src += "&referer=" + escape(document.referrer);
+
+ /** Add the configured options */
+ if (settings.block === 1)
+ src += '&block=1';
+ if (settings.blockcampaign === 1)
+ src += '&blockcampaign=1';
+ if (settings.target)
+ src += '&target=' + settings.target;
+ if (settings.withtext === 1)
+ src += '&withtext=1';
+ if (settings.charset)
+ src += '&charset=' + settings.charset;
+
+ /** Add the source-code - if present */
+ if (typeof OA_source !== 'undefined')
+ src += "&source=" + escape(OA_source);
+
+ /** Signal, that this task is done / in progress */
+ width = undefined;
+
+ /** Fetch data from OpenX and schedule the render-preparation */
+ $.getScript(src, init_ads);
}
function init_ads() {
- var i, id;
+ var i, id, ads = [];
for (i=0; i<queue.length; i++) {
id = queue[i];
if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
ads.push(id);
}
+ queue = ads;
document.write = document_write;
document.writeln = document_write;
function render_ads() {
- while (ads.length > 0) {
+ while (queue.length > 0) {
var result, src, inline;
- id = ads.shift();
- node = slots[id];
+ id = queue.shift();
+ node = $(slots[id]);
+
+ if (settings.debug && console.debug)
+ console.debug('Rendering banner ' + slots[id].id);
node.slideDown();
/** script-tag with src-URL! */
if (OA_output[id].length > 0)
/** The banner-code was not rendered completely yet! */
- ads.unshift(id);
+ queue.unshift(id);
/** Load the script and halt all work until the script is loaded and executed... */
$.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
return;
id = undefined;
node = undefined;
+ rendering = false;
+
+ if (settings.debug && console.debug)
+ console.debug('Recalculation of visible banners done!');
+
+ /** Restart rendering, if new task was queued */
+ if (width)
+ fetch_ads();
+
}
/** This function is used to overwrite document.write and document.writeln */
for (var i=0; i<arguments.length; i++)
output.push(arguments[i]);
- if (id != ads[0])
+ if (id != queue[0])
/**
* Re-Add the last banner-code to the working-queue, because included
* scripts had added markup via document.write(), which is not
* Otherwise the added markup would be falsely rendered together with
* the markup from the following banner-code.
*/
- ads.unshift(id);
+ queue.unshift(id);
}