2 * (C) Copyright 2012 juplo (http://juplo.de/).
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the GNU Lesser General Public License
6 * (LGPL) version 3.0 which accompanies this distribution, and is available at
7 * http://www.gnu.org/licenses/lgpl-3.0.html
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
19 * See http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
20 * for detailed explanations for the applied best practices.
22 * The semicolon guides our code for poorly written concatenated scripts.
24 ;(function( $, window, document, undefined ) {
28 settings, _options, domain, id, node,
44 * Configuration-Options for jQuery.openx
46 * Since the domain-name of the ad-server is the only required parameter,
47 * jQuery.openx for convenience can be configured with only that one
48 * parameter. For example: "jQuery.openx('openx.example.org');". If more
49 * configuration-options are needed, they must be specified as an object.
50 * For example: "jQuery.openx({'server': 'openx.example.org', ... });".
55 * server: string Name of the server, without protocol or port. For
56 * example "openx.example.org". This option is
58 * protocol: Optional parameter.
59 * http: All connections to the ad-server are made via HTTP.
60 * https: All connections to the ad-server are made via HTTPS.
61 * If empty, document.location.protocol will be used.
62 * http_port: number Port-Number for HTTP-connections to the ad-server
63 * (only needed, when it is not the default-value 80).
64 * https_port: Port-Number for HTTPS-connections to the ad-server
65 * (only needed, when it is not the default-value 443).
68 * Seldom needed special Server-Settings (these parameters are only needed,
69 * if the default delivery-configration of the OpenX-Server was changed):
71 * path: string Path to delivery-scripts. DEFAULT: "/www/delivery".
72 * fl: string Flash-Include-Script. DEFAULT: "fl.js".
75 * Delivery-Options (for details and explanations see the see:
76 * http://www.openx.com/docs/2.8/userguide/single%20page%20call):
78 * block: 1 Don't show the banner again on the same page.
79 * 0 A Banner might be shown multiple times on the same
81 * blockcampaign: 1 Don't show a banner from the same campaign again on
83 * 0 A Banner from the same campaign might be shown
84 * muliple times on the same page (DEFAULT).
85 * target: string The value is addes as the HTML TARGET attribute in
86 * the ad code. Examples for sensible values: "_blank",
88 * withtext: 1 Show text below banner. Enter this text in the
89 * Banner properties page.
90 * 0 Ignore the text-field from the banner-properties
92 * charset: string Charset used, when delivering the banner-codes.
93 * If empty, the charset is guessed by OpenX. Examples
94 * for sensible values: "UTF-8", "ISO-8859-1".
99 * selector: string A selector for selecting the DOM-elements, that
100 * should display ad-banners. DEFAULT: ".oa".
101 * See: http://api.jquery.com/category/selectors/
102 * min_prefix: string Prefix for the encoding of the minmal width as
103 * CSS-classname. DEFAULT: "min_".
104 * max_prefix: string Prefix for the encoding of the maximal width as
105 * CSS-classname. DEFAULT: "max_".
106 * resize_delay: number Number of milliseconds to wait, before a
107 * recalculation of the visible ads is scheduled.
108 * If the value is choosen to small, a recalculation
109 * might be scheduled, while resizing is still in
110 * progress. DEFAULT: 200.
112 $.openx = function( options ) {
116 console.error('jQuery.openx was already initialized!');
117 console.log('Configured options: ', _options);
122 /** Enable convenient-configuration */
123 if (typeof(options) == 'string')
124 options = { 'server': options };
128 if (!options.server) {
130 console.error('Required option "server" is missing!');
131 console.log('options: ', options);
138 'protocol': document.location.protocol,
139 'delivery': '/www/delivery',
142 'min_prefix': 'min_',
143 'max_prefix': 'max_',
149 domain = settings.protocol + '//';
150 domain += settings.server;
151 if (settings.protocol === 'http:' && settings.http_port)
152 domain += ':' + settings.http_port;
153 if (settings.protocol === 'https:' && settings.https_port)
154 domain += ':' + settings.https_port;
157 * Without this option, jQuery appends an timestamp to every URL, that
158 * is fetched via $.getScript(). This can mess up badly written
159 * third-party-ad-scripts, that assume that the called URL's are not
162 $.ajaxSetup({ 'cache': true });
165 * jQuery.openx only works with "named zones", because it does not know,
166 * which zones belong to which website. For mor informations about
168 * http://www.openx.com/docs/2.8/userguide/single%20page%20call
170 * For convenience, jQuery.openx only fetches banners, that are really
171 * included in the actual page. This way, you can configure jQuery.openx
172 * with all zones available for your website - for example in a central
173 * template - and does not have to worry about performance penalties due
174 * to unnecessarily fetched banners.
176 for(name in OA_zones) {
177 $(settings.selector).each(function() {
182 min = new RegExp('^' + settings.min_prefix + '([0-9]+)$'),
183 max = new RegExp('^' + settings.max_prefix + '([0-9]+)$'),
185 if (this.id === name) {
186 id = 'oa_' + ++count;
189 max_width[id] = Number.MAX_VALUE;
190 classes = this.className.split(/\s+/);
191 for (i=0; i<classes.length; i++) {
192 match = min.exec(classes[i]);
194 min_width[id] = match[1];
195 match = max.exec(classes[i]);
197 max_width[id] = match[1];
199 rendered[id] = false;
205 /** Add resize-event */
206 $(window).resize(function() {
207 clearTimeout(resize_timer);
208 resize_timer = setTimeout(recalculate_visible , settings.resize_timeout);
211 /** Fetch the JavaScript for Flash and schedule the initial fetch */
212 $.getScript(domain + settings.delivery + '/' + settings.fl, recalculate_visible);
216 function recalculate_visible() {
218 width = $(document).width();
224 function fetch_ads() {
226 /** Guide rendering-process for early restarts */
229 var name, src = domain + settings.delivery + '/spc.php';
231 /** Order banners for all zones that were found on the page */
234 visible[id] = width >= min_width[id] && width <= max_width[id];
238 src += escape(id + '=' + OA_zones[slots[id].id] + "|");
242 /** Unhide already fetched visible banners */
243 $(slots[id]).slideDown();
247 /** Hide unvisible banners */
251 src += '&nz=1'; // << We want to fetch named zones!
254 * These are some additions to the URL of spc.php, that are originally
257 src += '&r=' + Math.floor(Math.random()*99999999);
258 if (window.location) src += "&loc=" + escape(window.location);
259 if (document.referrer) src += "&referer=" + escape(document.referrer);
261 /** Add the configured options */
262 if (settings.block === 1)
264 if (settings.blockcampaign === 1)
265 src += '&blockcampaign=1';
267 src += '&target=' + settings.target;
268 if (settings.withtext === 1)
269 src += '&withtext=1';
270 if (settings.charset)
271 src += '&charset=' + settings.charset;
273 /** Add the source-code - if present */
274 if (typeof OA_source !== 'undefined')
275 src += "&source=" + escape(OA_source);
277 /** Signal, that this task is done / in progress */
280 /** Fetch data from OpenX and schedule the render-preparation */
281 $.getScript(src, init_ads);
285 function init_ads() {
288 for (i=0; i<queue.length; i++) {
290 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
295 document.write = document_write;
296 document.writeln = document_write;
302 function render_ads() {
304 while (queue.length > 0) {
306 var result, src, inline;
313 // node.append(id + ": " + node.attr('class'));
316 * If output was added via document.write(), this output must be
317 * rendered before other banner-code from the OpenX-server is rendered!
321 while ((result = /<script/i.exec(OA_output[id])) != null) {
322 node.append(OA_output[id].slice(0,result.index));
323 /** Strip all text before "<script" from OA_output[id] */
324 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
325 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
326 if (result == null) {
327 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
328 // alert(OA_output[id]);
332 /** Remember iinline-code, if present */
333 src = result[1] + ' ' // << simplifies the following regular expression: the string ends with a space in any case, so that the src-URL cannot be followed by the end of the string emediately!
335 /** Strip all text up to and including "</script>" from OA_output[id] */
336 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
337 result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
338 if (result == null) {
339 /** script-tag with inline-code: execute inline-code! */
340 result = /^\s*<.*$/m.exec(inline);
341 if (result != null) {
342 /** Remove leading HTML-comments, because IE will stumble otherwise */
343 inline = inline.slice(result[0].length,inline.length);
345 $.globalEval(inline);
346 insert_output(); // << The executed inline-code might have called document.write()!
349 /** script-tag with src-URL! */
350 if (OA_output[id].length > 0)
351 /** The banner-code was not rendered completely yet! */
353 /** Load the script and halt all work until the script is loaded and executed... */
354 $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
360 node.append(OA_output[id]);
364 /** All entries from OA_output were rendered */
370 /** Restart rendering, if new task was queued */
376 /** This function is used to overwrite document.write and document.writeln */
377 function document_write() {
382 for (var i=0; i<arguments.length; i++)
383 output.push(arguments[i]);
387 * Re-Add the last banner-code to the working-queue, because included
388 * scripts had added markup via document.write(), which is not
390 * Otherwise the added markup would be falsely rendered together with
391 * the markup from the following banner-code.
398 * This function prepends the collected output from calls to
399 * document_write() to the current banner-code.
401 function insert_output() {
403 if (output.length > 0) {
404 output.push(OA_output[id]);
406 for (i=0; i<output.length; i++)
407 OA_output[id] += output[i];
413 })( jQuery, window, document );
415 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!