/*
 * jQuery preloadImg plugin
 * A really really simple image preload thing.
 * Simplified from preloadImages plugin by Blair McBride,
 * and using a preload method from Macromedia's MM_preloadImages().
 * Version 1.0.1  (10/1/2008)
 * @requires jQuery v1.2.6+
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @name preloadImg
 * @type jQuery
 * @cat Plugins/Browser Tweaks
 * @author Robert Wilt
*/

(function($) {
/**
*
* Queue list of images and start preloading
*
* @example $.preloadImages("a.gif");
* @example $.preloadImages(["a.gif", "b.jpg", "c.png"]);
*
* @param imgList A single image URL, or a list of image URLs
*/
$.preloadImg = function(imgList) {
  $.preloadImages.add(imgList);
  startPreload();
};

/**
* Add a single image or list of images to the queue.
* Does not start preloading.
*
* @example $.preloadImg.add("a.gif");
* @example $.preloadImg.add(["a.gif", "b.jpg", "c.png"]);
*
* @param imgList A single image URL, or a list of image URLs
*/
$.preloadImg.add = function(imgList) {
  if(typeof(imgList) == "string") {
    $.preloadImg.queue.push({src:imgList,img:null});
    return;
  }

  if(imgList.length < 1) return;

  for(var i = 0; i < imgList.length; i++) {
    if(typeof(imgList[i]) == "string")
      $.preloadImg.queue.push({src:imgList[i],img:null});
  }
}

/**
* Start processing the preload queue.
*
* @example $.preloadImg.start();
*
*/
$.preloadImg.start = function() {
  startPreload();
}

/*
* The preload queue.
* Each item in the queue is an object with the image src, and the image.
*/
$.preloadImg.queue = new Array();

/*
* The preload function.
* Note that the function does not re-preload images that were preloaded before.
* So it is possible to add some images, start, add more, start again.
* Also note how the images are created. This seems to avoid the IE 15-max problem
* that other preloaders mention.
*/
function startPreload() {
  var q = $.preloadImg.queue;
  for (var i = 0; i < q.length; i++) {
    if (!q[i].img) {
      q[i].img = new Image;
      q[i].img.src = q[i].src;
    }
  }
}


})(jQuery);
