﻿/**
 * @name: Preload JQuery
 * @description: preload image 
 * @Author: Marco Patierno
 * @website: www.hotminds.it
 * @email: m.patierno@hotminds.it
 * @version: 1.0 
 * @date: September 03, 2010
 * @released: September 03, 2010 {1.0}
 * @category: Javascript Custom Component
 * @copyright (c) 2010 Marco Patierno (powered by: http://www.hotminds.it/)
 */
//---------------------
//    Example    
//---------------------
//$(function() {
//    $.preload([
//        "http://farm3.static.flickr.com/2661/3792282714_90584b41d5_b.jpg",
//        "http://farm2.static.flickr.com/1266/1402810863_d41f360b2e_o.jpg"
//    ], {
//        init: function(loaded, total) {
//            $("#indicator").html("Loaded: "+loaded+"/"+total);
//        },
//        loaded: function(img, loaded, total) {
//            $("#indicator").html("Loaded: "+loaded+"/"+total);
//            $("#place").append(img);
//        },
//        loaded_all: function(loaded, total) {
//            $("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
//        }
//    });
//});
//---------------------
(function($) {
     var imgList = [];
     $.extend({
      preload: function(imgArr, option) {
        var setting = $.extend({
        init: function(loaded, total) {},
        loaded: function(img, loaded, total) {},
        loaded_all: function(loaded, total) {}
      }, option);
      var total = imgArr.length;
      var loaded = 0;
      setting.init(0, total);
      for(var i in imgArr) {
        imgList.push($("<img />")
          .attr("src", imgArr[i])
          .load(function() {
            loaded++;
            setting.loaded(this, loaded, total);
            if(loaded == total) {
              setting.loaded_all(loaded, total);
            }
          })
        );
      }
     }
     });
    })(jQuery);
