slots = {},
min_width = {},
max_width = {},
- width,
+ is_pagewidth = {},
+ pagewidth,
rendered = {},
+ visible = {},
+ rendering = false,
+ resize_timer,
queue = [],
output = [];
* 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_".
+ * CSS-class. DEFAULT: "min_".
* max_prefix: string Prefix for the encoding of the maximal width as
- * CSS-classname. DEFAULT: "max_".
+ * 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 ) {
'selector': '.oa',
'min_prefix': 'min_',
'max_prefix': 'max_',
- 'cache': true
+ '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
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];
+ min_width[id] = +match[1];
match = max.exec(classes[i]);
if (match)
- max_width[id] = match[1];
+ 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] : '')
+ );
}
});
}
- /** Set initial window-width */
- width = $(document).width();
+ /** 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, fetch_ads);
+ $.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() {
- var name, src = domain + settings.delivery + '/spc.php';
+ /** 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, src = domain + settings.delivery + '/spc.php';
/** Order banners for all zones that were found on the page */
src += '?zones=';
for(id in slots) {
- if (width >= min_width[id] && width <= max_width[id]) {
- queue.push(id);
- src += escape(id + '=' + OA_zones[slots[id].id] + "|");
- rendered[id] = true;
+ 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 (typeof OA_source !== 'undefined')
src += "&source=" + escape(OA_source);
+ /** Signal, that this task is done / in progress */
+ pagewidth = undefined;
+
/** Fetch data from OpenX and schedule the render-preparation */
$.getScript(src, init_ads);
id = queue.shift();
node = $(slots[id]);
+ if (settings.debug && console.debug)
+ console.debug('Rendering banner ' + slots[id].id);
+
node.slideDown();
// node.append(id + ": " + node.attr('class'));
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 */