Fixed an error in the ordering, output added via document.write is rendered
[openx] / openx.js
index cba60d3..3499345 100644 (file)
--- a/openx.js
+++ b/openx.js
 
   count = 0,
   slots = {},
-  ads = [];
+  queue = [],
+  ads = [],
+  output = [];
 
 
   openx.show_ads = function(server, zones) {
 
     var
     domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server,
+    name,
     src = domain;
 
     document.write = document_write;
     src += "/www/delivery/spc.php?zones=";
 
     /** Only fetch banners, that are really included in this page */
-    $('.oa').each(function() {
-      var
-      node = $(this),
-      name, id;
-      for(name in zones) {
+    for(name in zones) {
+      $('.oa').each(function() {
+        var
+        node = $(this),
+        id;
         if (node.hasClass(name)) {
           id = 'oa_' + ++count;
           slots[id] = node;
+          queue.push(id);
           src += escape(id + '=' + zones[name] + "|");
         }
-      }
-    });
+      });
+    }
 
     src += "&nz=1&source=" + escape(OA_source);
     src += "&r=" + Math.floor(Math.random()*99999999);
@@ -55,7 +59,9 @@
 
   function init_ads() {
 
-    for (var id in slots) {
+    var i, id;
+    for (i=0; i<queue.length; i++) {
+      id = queue[i];
       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
         ads.push(id);
     }
 
     while (ads.length > 0) {
 
-      id = ads.pop();
+      var result, src, inline, i;
+
+      id = ads.shift();
       node = slots[id];
 
+      node.slideDown();
+
       // node.append(id + ": " + node.attr('class'));
 
-      var result, src, inline;
+      /**
+       * If output was added via document.write(), this output must be
+       * rendered before other banner-code from the OpenX-server is rendered!
+       */
+      if (output.length > 0) {
+        output.push(OA_output[id]);
+        OA_output[id] = "";
+        for (i=0; i<output.length; i++)
+          OA_output[id] += output[i];
+        output = [];
+      }
 
       while ((result = /<script/i.exec(OA_output[id])) != null) {
         node.append(OA_output[id].slice(0,result.index));
           }
           else {
             /** script-tag with src-URL! */
-            ads.push(id); // << The banner might not be rendered fully, or include more calls to document.write().
+            ads.unshift(id); // << The banner might not be rendered fully, or include more calls to document.write().
             /** 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;
 
   function document_write() {
 
-    if (id == undefined)
-      return;
-
-    var
-    str = "",
-    i;
-
-    for (i=0; i < arguments.length; i++)
-      str += arguments[i];
-
-    OA_output[id] = str + OA_output[id];
+    for (var i=0; i<arguments.length; i++)
+      output.push(arguments[i]);
+
+    if (id != ads[0])
+      /**
+       * Re-Add the last banner-code to the working-queue, because included
+       * scripts had added markup via document.write(), which is not
+       * proccessed yet.
+       * Otherwise the added markup would be falsely rendered together with
+       * the markup from the following banner-code.
+       */
+      ads.unshift(id);
 
   }