/*
 * jQuery imageLoader plugin
 * Version 1.0 (Feb 03 2010)
 * Copyright (c) 2009-2010 Brandon Clark
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */


imageLoaderDefaultConfig = {
    LoadingImage: 'img/image-loader.gif',
    FailImage: 'img/image-loader-error.jpg'
}

jQuery.fn.loadImage = function(pConfig, pCallback) {
    if (pConfig)
	var config = jQuery.extend(imageLoaderDefaultConfig, pConfig);
    else
	var config = imageLoaderDefaultConfig;

    var loader = jQuery(this);    

    loader.html('<img src="' + config.LoadingImage + '"/>');

    

    var img_src = loader.attr('src');
    loader.removeAttr('src');

    img = jQuery(new Image());
    img.hide();

    img.load(function(){

	if (loader.attr('onload')){
	    cb_js = loader.get(0).getAttribute('onload')
	    onload_cb = function() { eval(cb_js); }
	    loader.removeAttr('onload');
	} else
	    onload_cb = null;

	loader.html(this);

	jQuery(this).show();

	if (onload_cb)
	    onload_cb(jQuery(this));

	if (pCallback)
	    pCallback(jQuery(this));
    })
    .attr('src', img_src)
    .error(function(){ jQuery(this).attr('src', config.FailImage); })
    .show();

    
}

jQuery.fn.loadImages = function(pConfig, pCallback){
    this.each(function(){
	jQuery(this).loadImage(pConfig, pCallback)
    })
}
