Added copyright- and license-information
[openx] / openx.js
1 /*
2  * (C) Copyright 2012 juplo (http://juplo.de/).
3  *
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
8  *
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.
13  *
14  * Contributors:
15  * - Kai Moritz
16  */
17
18 (function( openx, $, undefined ) {
19
20   var
21
22   domain, id, node,
23
24   count = 0,
25   slots = {},
26   queue = [],
27   ads = [],
28   output = [];
29
30
31   openx.show_ads = function(server, zones) {
32
33     domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
34
35     var
36     name,
37     src = domain;
38
39     /**
40      * Without this option, jQuery appends an timestamp to every URL, that
41      * is fetched via $.getScript(). This can mess up badly written
42      * third-party-ad-scripts, that assume that the called URL's are not
43      * altered.
44      */
45     $.ajaxSetup({ cache: true });
46
47     src += "/www/delivery/spc.php?zones=";
48
49     /** Only fetch banners, that are really included in this page */
50     for(name in zones) {
51       $('.oa').each(function() {
52         var
53         node = $(this),
54         id;
55         if (node.hasClass(name)) {
56           id = 'oa_' + ++count;
57           slots[id] = node;
58           queue.push(id);
59           src += escape(id + '=' + zones[name] + "|");
60         }
61       });
62     }
63
64     if (typeof OA_source !== 'undefined')
65       src += "&source=" + escape(OA_source);
66     src += "&nz=1&r=" + Math.floor(Math.random()*99999999);
67     src += "&block=1&charset=UTF-8";
68
69     if (window.location)   src += "&loc=" + escape(window.location);
70     if (document.referrer) src += "&referer=" + escape(document.referrer);
71
72     $.getScript(src, load_flash);
73
74   }
75
76   function load_flash() {
77
78     $.getScript(domain + '/www/delivery/fl.js', init_ads);
79
80   }
81
82   function init_ads() {
83
84     var i, id;
85     for (i=0; i<queue.length; i++) {
86       id = queue[i];
87       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
88         ads.push(id);
89     }
90
91     document.write = document_write;
92     document.writeln = document_write;
93
94     render_ads();
95
96   }
97
98   function render_ads() {
99
100     while (ads.length > 0) {
101
102       var result, src, inline, i;
103
104       id = ads.shift();
105       node = slots[id];
106
107       node.slideDown();
108
109       // node.append(id + ": " + node.attr('class'));
110
111       /**
112        * If output was added via document.write(), this output must be
113        * rendered before other banner-code from the OpenX-server is rendered!
114        */
115       insert_output();
116
117       while ((result = /<script/i.exec(OA_output[id])) != null) {
118         node.append(OA_output[id].slice(0,result.index));
119         /** Strip all text before "<script" from OA_output[id] */
120         OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
121         result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
122         if (result == null) {
123           /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
124           // alert(OA_output[id]);
125           OA_output[id] = "";
126         }
127         else {
128           /** Remember iinline-code, if present */
129           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!
130           inline = result[2];
131           /** Strip all text up to and including "</script>" from OA_output[id] */
132           OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
133           result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
134           if (result == null) {
135             /** script-tag with inline-code: execute inline-code! */
136             result = /^\s*<.*$/m.exec(inline);
137             if (result != null) {
138               /** Remove leading HTML-comments, because IE will stumble otherwise */
139               inline = inline.slice(result[0].length,inline.length);
140             }
141             $.globalEval(inline);
142             insert_output(); // << The executed inline-code might have called document.write()!
143           }
144           else {
145             /** script-tag with src-URL! */
146             if (OA_output[id].length > 0)
147               /** The banner-code was not rendered completely yet! */
148               ads.unshift(id);
149             /** Load the script and halt all work until the script is loaded and executed... */
150             $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
151             return;
152           }
153         }
154       }
155
156       node.append(OA_output[id]);
157       OA_output[id] = "";
158     }
159
160     /** All entries from OA_output were rendered */
161
162     id = undefined;
163     node = undefined;
164   }
165
166   /** This function is used to overwrite document.write and document.writeln */
167   function document_write() {
168
169     if (id == undefined)
170       return;
171
172     for (var i=0; i<arguments.length; i++)
173       output.push(arguments[i]);
174
175     if (id != ads[0])
176       /**
177        * Re-Add the last banner-code to the working-queue, because included
178        * scripts had added markup via document.write(), which is not
179        * proccessed yet.
180        * Otherwise the added markup would be falsely rendered together with
181        * the markup from the following banner-code.
182        */
183       ads.unshift(id);
184
185   }
186
187   /**
188    * This function prepends the collected output from calls to
189    * document_write() to the current banner-code.
190    */
191   function insert_output() {
192
193     if (output.length > 0) {
194       output.push(OA_output[id]);
195       OA_output[id] = "";
196       for (i=0; i<output.length; i++)
197         OA_output[id] += output[i];
198       output = [];
199     }
200
201   }
202
203 } ( window.openx = window.openx || {}, jQuery ));
204
205 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!