Index: /trunk/themes/default/template/thumbnails.tpl
===================================================================
--- /trunk/themes/default/template/thumbnails.tpl	(revision 12958)
+++ /trunk/themes/default/template/thumbnails.tpl	(revision 13444)
@@ -1,3 +1,5 @@
 {if !empty($thumbnails)}{strip}
+{combine_script id='jquery.ajaxmanager' path='themes/default/js/plugins/jquery.ajaxmanager.js' load='async'}
+{combine_script id='thumbnails.loader' path='themes/default/js/thumbnails.loader.js' require='jquery.ajaxmanager' load='async'}
 {*define_derivative name='derivative_params' width=160 height=90 crop=true*}
 {html_style}
@@ -23,9 +25,10 @@
 {/html_style}
 {foreach from=$thumbnails item=thumbnail}
+{assign var=derivative value=$pwg->derivative($derivative_params, $thumbnail.src_image)}
 	<li>
 	<span class="wrap1">
 		<span class="wrap2">
 		<a href="{$thumbnail.URL}">
-			<img class="thumbnail" src="{$pwg->derivative_url($derivative_params, $thumbnail.src_image)}" alt="{$thumbnail.TN_ALT}" title="{$thumbnail.TN_TITLE}">
+			<img class="thumbnail" {if !$derivative->is_cached()}data-{/if}src="{$derivative->get_url()}" alt="{$thumbnail.TN_ALT}" title="{$thumbnail.TN_TITLE}">
 		</a>
 		</span>
Index: /trunk/themes/default/js/thumbnails.loader.js
===================================================================
--- /trunk/themes/default/js/thumbnails.loader.js	(revision 13444)
+++ /trunk/themes/default/js/thumbnails.loader.js	(revision 13444)
@@ -0,0 +1,29 @@
+var thumbnails_queue = jQuery.manageAjax.create('queued', {
+  queue: true,  
+  cacheResponse: false,
+  maxRequests: 3,
+  preventDoubleRequests: false
+});
+
+function add_thumbnail_to_queue(img, loop) {
+  thumbnails_queue.add({
+    type: 'GET', 
+    url: img.data('src'), 
+    data: { ajaxload: 'true' },
+    dataType: 'json',
+    success: function(result) {
+      img.attr('src', result.url);
+    },
+    error: function() {
+      if (loop < 3)
+        add_thumbnail_to_queue(img, ++loop); // Retry 3 times
+    }
+  }); 
+}
+
+jQuery('img').each(function() {
+  var img = jQuery(this);
+  if (typeof img.data('src') != 'undefined') {
+    add_thumbnail_to_queue(img, 0);
+  }
+});
Index: /trunk/i.php
===================================================================
--- /trunk/i.php	(revision 13426)
+++ /trunk/i.php	(revision 13444)
@@ -320,4 +320,15 @@
 {
   global $page;
+
+  if (isset($_GET['ajaxload']) and $_GET['ajaxload'] == 'true')
+  {
+    include_once(PHPWG_ROOT_PATH.'include/functions_cookie.inc.php');
+    include_once(PHPWG_ROOT_PATH.'include/functions_url.inc.php');
+
+    $response = new json_response();
+    $response->url = embellish_url(get_absolute_root_url().$page['derivative_path']);
+    echo json_encode($response);
+    return;
+  }
   $fp = fopen($page['derivative_path'], 'rb');
 
@@ -344,4 +355,8 @@
 }
 
+class json_response
+{
+  var $url;
+}
 
 $page=array();
@@ -401,4 +416,5 @@
   }
   send_derivative($expires);
+  exit;
 }
 
Index: /trunk/include/derivative.inc.php
===================================================================
--- /trunk/include/derivative.inc.php	(revision 13252)
+++ /trunk/include/derivative.inc.php	(revision 13444)
@@ -100,5 +100,5 @@
 
   private $params;
-  private $rel_path, $rel_url;
+  private $rel_path, $rel_url, $is_cached;
 
   function __construct($type, $src_image)
@@ -114,5 +114,5 @@
     }
 
-    self::build($src_image, $this->params, $this->rel_path, $this->rel_url);
+    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->is_cached);
   }
 
@@ -154,5 +154,5 @@
   }
 
-  private static function build($src, &$params, &$rel_path, &$rel_url)
+  private static function build($src, &$params, &$rel_path, &$rel_url, &$is_cached=null)
   {
     if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
@@ -161,4 +161,5 @@
       $params = null;
       $rel_path = $rel_url = $src->rel_path;
+      $is_cached = true;
       return;
     }
@@ -192,8 +193,10 @@
       if ($mtime===false or $mtime < $params->last_mod_time)
       {
+        $is_cached = false;
         $url_style = 2;
       }
       else
       {
+        $is_cached = true;
         $url_style = 1;
       }
@@ -304,4 +307,9 @@
     }
   }
+
+  function is_cached()
+  {
+    return $this->is_cached;
+  }
 }
 
