count = 0,
slots = {},
+ min_width = {},
+ max_width = {},
+ is_pagewidth = {},
+ pagewidth,
+ rendered = {},
+ visible = {},
+ rendering = false,
+ resize_timer,
queue = [],
- ads = [],
output = [];
* (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):
*
* 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:
+ *
+ * source: array An optional array of JavaScript-Objects, that
+ * define prefixes and suffixes, that should be
+ * added to source-tag defined in OA_source. Each
+ * object can (should) specify the properties "min"
+ * and "max". OA_source will only be altered, when
+ * min <= page-width <= max is true. If this is the
+ * case, the property "prefix" will be prepended to
+ * OA_source and the property "suffix" will be
+ * appended.
+ * 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-class. DEFAULT: "min_".
+ * max_prefix: string Prefix for the encoding of the maximal width as
+ * CSS-class. DEFAULT: "max_".
+ * pw_marker: string CSS-class, that marks the encoded maximal and minmal
+ * width as page width. DEFAULT: "pw".
+ * 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 ) {
- var name, src, errors = [], i;
-
if (domain) {
if (console.error) {
console.error('jQuery.openx was already initialized!');
_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,
- 'cache': true
+ 'delivery': '/www/delivery',
+ 'fl': 'fl.js',
+ 'selector': '.oa',
+ 'min_prefix': 'min_',
+ 'max_prefix': 'max_',
+ 'pw_marker': 'pw',
+ '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
*/
$.ajaxSetup({ 'cache': true });
-
- src = domain + '/www/delivery/spc.php';
-
/**
* jQuery.openx only works with "named zones", because it does not know,
* which zones belong to which website. For mor informations about
* template - and does not have to worry about performance penalties due
* to unnecessarily fetched banners.
*/
- src += '?zones=';
for(name in OA_zones) {
- $('.oa').each(function() {
+ $(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 + '=' + OA_zones[name] + "|");
+ slots[id] = this;
+ min_width[id] = 0;
+ max_width[id] = Number.MAX_VALUE;
+ is_pagewidth[id] = false;
+ 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];
+ is_pagewidth[id] = classes[i] === settings.pw_marker;
+ }
+ rendered[id] = false;
+ visible[id] = false;
+ if (settings.debug && console.debug)
+ console.debug(
+ 'Slot ' + count + ': ' + this.id
+ + (is_pagewidth[id] ? ', pagewidth: ' : ', width: ')
+ + min_width[id]
+ + (max_width[id] != Number.MAX_VALUE ? '-' + max_width[id] : '')
+ );
}
});
}
+
+ /** Cleanup data for responsive source-tag's */
+ for (i=0; i<settings.source.length; i++) {
+ if (!settings.source[i].min) settings.source[i].min = 0;
+ if (!settings.source[i].max) settings.source[i].max = Number.MAX_VALUE;
+ if (!settings.source[i].prefix) settings.source[i].prefix = '';
+ if (!settings.source[i].suffix) settings.source[i].suffix = '';
+ }
+
+ /** Add resize-event */
+ $(window).resize(function() {
+ clearTimeout(resize_timer);
+ resize_timer = setTimeout(recalculate_visible , settings.resize_timeout);
+ });
+
+ /** Fetch the JavaScript for Flash and schedule the initial fetch */
+ $.getScript(domain + settings.delivery + '/' + settings.fl, recalculate_visible);
+
+ }
+
+ function recalculate_visible() {
+
+ pagewidth = $(document).width();
+ if (settings.debug && console.debug)
+ console.debug('Scheduling recalculation of visible banners for width ' + pagewidth);
+ if (!rendering)
+ fetch_ads();
+
+ }
+
+ function fetch_ads() {
+
+ /** Guide rendering-process for early restarts */
+ rendering = true;
+
+ if (settings.debug && console.debug)
+ console.debug('Starting recalculation of visible banners for width ' + pagewidth);
+
+ var
+ name,
+ width,
+ i,
+ source_prefix = '',
+ source_suffix = '',
+ src = domain + settings.delivery + '/spc.php';
+
+ /** Order banners for all zones that were found on the page */
+ src += '?zones=';
+ for(id in slots) {
+ width =
+ is_pagewidth[id]
+ ? pagewidth
+ : Math.round($(slots[id]).parent().width());
+ 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!
/**
if (settings.charset)
src += '&charset=' + settings.charset;
- /** Add the source-code - if present */
- if (typeof OA_source !== 'undefined')
- src += "&source=" + escape(OA_source);
-
- /** Chain-load the scripts (next script to load is fl.js */
- $.getScript(src, load_flash);
+ /** Calculate responsive source-tag's */
+ for (i=0; i<settings.source.length; i++) {
+ if (pagewidth >= settings.source[i].min
+ && pagewidth <= settings.source[i].max
+ ) {
+ source_prefix = settings.source[i].prefix + source_prefix;
+ source_suffix = source_suffix + settings.source[i].suffix;
+ }
+ }
- }
+ /** Add the source-code - if present */
+ if (typeof OA_source !== 'undefined'
+ || source_prefix != ''
+ || source_suffix != ''
+ ) {
+ if (settings.debug && console.debug)
+ console.debug('OA_source: ' + source_prefix + OA_source + source_suffix);
+ src += "&source=" + escape(source_prefix + OA_source + source_suffix);
+ }
- function load_flash() {
+ /** Signal, that this task is done / in progress */
+ pagewidth = undefined;
- $.getScript(domain + '/www/delivery/fl.js', init_ads);
+ /** 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 (pagewidth)
+ 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);
}