Fetch on resize: added resize-event that hides/shows banners accordingly
[openx] / jquery.openx.js
index d687dfa..889a539 100644 (file)
 
   count = 0,
   slots = {},
+  min_width = {},
+  max_width = {},
+  width,
+  rendered = {},
+  visible = {},
+  rendering = false,
+  resize_timer,
   queue = [],
   output = [];
 
    * selector:      string  A selector for selecting the DOM-elements, that
    *                        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_".
+   * max_prefix:    string  Prefix for the encoding of the maximal width as
+   *                        CSS-classname. DEFAULT: "max_".
+   * 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.
    */
   $.openx = function( options ) {
 
         'delivery': '/www/delivery',
         'fl': 'fl.js',
         'selector': '.oa',
-        'cache': true
+        'min_prefix': 'min_',
+        'max_prefix': 'max_',
+        'resize_delay': 200
       },
       options
       );
     for(name in OA_zones) {
       $(settings.selector).each(function() {
         var
-        id;
+        id,
+        classes,
+        i,
+        min = new RegExp('^' + settings.min_prefix + '([0-9]+)$'),
+        max = new RegExp('^' + settings.max_prefix + '([0-9]+)$'),
+        match;
         if (this.id === name) {
           id = 'oa_' + ++count;
           slots[id] = this;
+          min_width[id] = 0;
+          max_width[id] = Number.MAX_VALUE;
+          classes = this.className.split(/\s+/);
+          for (i=0; i<classes.length; i++) {
+            match = min.exec(classes[i]);
+            if (match)
+              min_width[id] = match[1];
+            match = max.exec(classes[i]);
+            if (match)
+              max_width[id] = match[1];
+          }
+          rendered[id] = false;
+          visible[id] = false;
         }
       });
     }
 
+    /** 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() {
+
+    width = $(document).width();
+    if (!rendering)
+      fetch_ads();
+    
+  }
+
   function fetch_ads() {
 
+    /** Guide rendering-process for early restarts */
+    rendering = true;
+
     var name, src = domain + settings.delivery + '/spc.php';
 
     /** Order banners for all zones that were found on the page */
     src += '?zones=';
     for(id in slots) {
-      queue.push(id);
-      src += escape(id + '=' + OA_zones[slots[id].id] + "|");
+      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;
+        }
+        else {
+          /** Unhide already fetched visible banners */
+          $(slots[id]).slideDown();
+        }
+      }
+      else {
+        /** Hide unvisible banners */
+        $(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 */
+    width = undefined;
+
     /** Fetch data from OpenX and schedule the render-preparation */
     $.getScript(src, init_ads);
 
 
     id = undefined;
     node = undefined;
+    rendering = false;
+
+    /** Restart rendering, if new task was queued */
+    if (width)
+      fetch_ads();
+
   }
 
   /** This function is used to overwrite document.write and document.writeln */