jQuery(document).ready(
  function (jQuery) {


  });

  //============================================================
   var derivatives = {
	    elements: null,
	    done: 0,
	    total: 0,	
	    finished: function() {
            derivatives.elements.splice(0, 1);
		        return derivatives.done == derivatives.total && derivatives.elements && derivatives.elements.length==0;
	          }
      };
   
  function progress(success,data) {
    if (derivatives.finished()) {
      jQuery("#theMainImage").trigger("Finish", [derivatives]);
    }

    }

  derivatives.elements = [];
  var ids = derivatives.elements.splice(0, 500);
  var params = { max_urls: 10, ids: ids, types: [], src: [],width:[], height:[]  };

  function getDerivativeUrls(nofile) {
    params.max_urls = nofile.length;
    jQuery(nofile).each(function (i, t) {
      if (typeof t.file != "undefined") {
        nbsrc = params.src.length;
        var reg = new RegExp("^\/", "g",1);
        t.file=   t.file.replace( reg ,"",1);
        if (nbsrc == 0) {
          params.ids.push(t.id);
          params.types.push(t.type);
          params.src.push(t.file);
         

        } else if (~jQuery.inArray(t.file, params.src)) {
          jQuery.noop();
        } else {
          params.ids.push(t.id);
          params.types.push(t.type);
          params.src.push(t.file);
 
        }
      }
    });

    if(derivatives.elements.length==0)
            derivatives.elements = params.ids;
    derivatives.total += params.src.length;




    jQuery("body").append("<div style='position:abosolute;left:-200;top:-200'>");
  if (derivatives.elements.length) {
    if (derivatives.total - derivatives.done > 0)
      setTimeout("Set_images(params.src)", 500 * (derivatives.total - derivatives.done));
     }
	} //ready 
	//===============================================================
	Set_images = function (dataUrls) {
	  if (typeof jQuery.manageAjax != "undefined") {
	    for (var i = 0; i < dataUrls.length; i++) {
	      fichier = dataUrls[i];
	      if (fichier != "")
	        jQuery.manageAjax.add("queued", {
	          type: 'GET',
	          url: 'i.php?' + fichier + "&ajaxload=true",
	          dataType: 'json',
	          success: (function (data) {
	            derivatives.done++;
	            try {
	              for (var j = 0; j < params.src.length; j++) {
	                if (data.url.match(params.src[j])) {
	                  params.src[j] = ""; break;
	                }
	              }
	            } catch (e) {

	            }
	            jQuery("#theMainImage").trigger("Success", data);
	          }),
	          error: (function (data) {
	            jQuery("#theMainImage").trigger("Error", data);
	          }),
	          fail: (function (data) {
	            jQuery("#theMainImage").trigger("fail", data);
	          }),
	          progress: (function (data) {
	            jQuery("#theMainImage").trigger("Progress", data);
	          }),

	          complete: (function (data) {
	            data.src = jQuery("#theMainImage").attr("src"); 
	            jQuery("#theMainImage").trigger("Complete", data);
	          }),
	          isRejected: (function (data) {
	            jQuery("#theMainImage").trigger("IsRejected", data);
	          })

	        });
	    }

	  }
	}