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, _zones, _options, domain, id, node,
37 $.openx = function( zones, options ) {
39 var name, src, errors = [], i;
43 console.error('jQuery.openx was already initialized!');
44 console.log('Configured zones: ', _zones);
45 console.log('Configured options: ', _options);
54 errors.push('Required option "server" is missing!');
55 if (errors.length > 0) {
57 for (i=0; i<errors.length; i++)
58 console.error('Required option "server" is missing!');
59 console.log('options: ', options);
66 'protocol': document.location.protocol
71 domain = settings.protocol + '//';
72 domain += settings.server;
73 if (settings.protocol === 'http:' && settings.http_port)
74 domain += ':' + settings.http_port;
75 if (settings.protocol === 'https:' && settings.https_port)
76 domain += ':' + settings.https_port;
79 * Without this option, jQuery appends an timestamp to every URL, that
80 * is fetched via $.getScript(). This can mess up badly written
81 * third-party-ad-scripts, that assume that the called URL's are not
84 $.ajaxSetup({ cache: true });
86 src = domain + '/www/delivery/spc.php?zones=';
88 /** Only fetch banners, that are really included in this page */
90 $('.oa').each(function() {
94 if (node.hasClass(name)) {
98 src += escape(id + '=' + zones[name] + "|");
103 if (typeof OA_source !== 'undefined')
104 src += "&source=" + escape(OA_source);
105 src += "&nz=1&r=" + Math.floor(Math.random()*99999999);
106 src += "&block=1&charset=UTF-8";
108 if (window.location) src += "&loc=" + escape(window.location);
109 if (document.referrer) src += "&referer=" + escape(document.referrer);
111 $.getScript(src, load_flash);
115 function load_flash() {
117 $.getScript(domain + '/www/delivery/fl.js', init_ads);
121 function init_ads() {
124 for (i=0; i<queue.length; i++) {
126 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
130 document.write = document_write;
131 document.writeln = document_write;
137 function render_ads() {
139 while (ads.length > 0) {
141 var result, src, inline;
148 // node.append(id + ": " + node.attr('class'));
151 * If output was added via document.write(), this output must be
152 * rendered before other banner-code from the OpenX-server is rendered!
156 while ((result = /<script/i.exec(OA_output[id])) != null) {
157 node.append(OA_output[id].slice(0,result.index));
158 /** Strip all text before "<script" from OA_output[id] */
159 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
160 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
161 if (result == null) {
162 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
163 // alert(OA_output[id]);
167 /** Remember iinline-code, if present */
168 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!
170 /** Strip all text up to and including "</script>" from OA_output[id] */
171 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
172 result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
173 if (result == null) {
174 /** script-tag with inline-code: execute inline-code! */
175 result = /^\s*<.*$/m.exec(inline);
176 if (result != null) {
177 /** Remove leading HTML-comments, because IE will stumble otherwise */
178 inline = inline.slice(result[0].length,inline.length);
180 $.globalEval(inline);
181 insert_output(); // << The executed inline-code might have called document.write()!
184 /** script-tag with src-URL! */
185 if (OA_output[id].length > 0)
186 /** The banner-code was not rendered completely yet! */
188 /** Load the script and halt all work until the script is loaded and executed... */
189 $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
195 node.append(OA_output[id]);
199 /** All entries from OA_output were rendered */
205 /** This function is used to overwrite document.write and document.writeln */
206 function document_write() {
211 for (var i=0; i<arguments.length; i++)
212 output.push(arguments[i]);
216 * Re-Add the last banner-code to the working-queue, because included
217 * scripts had added markup via document.write(), which is not
219 * Otherwise the added markup would be falsely rendered together with
220 * the markup from the following banner-code.
227 * This function prepends the collected output from calls to
228 * document_write() to the current banner-code.
230 function insert_output() {
232 if (output.length > 0) {
233 output.push(OA_output[id]);
235 for (i=0; i<output.length; i++)
236 OA_output[id] += output[i];
242 })( jQuery, window, document );
244 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!