/**
 * Por Design Image Loading
 *
 * Copyright (c) 2010 Por Design (pordesign.eu)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 *
 */

(function($)
{
	$.fn.imgload = function(src, options)
	{
		options = $.extend
		({
			path: ''
		}, options);
		
		$(this).each( function()
		{
			$img = $(this);
			var left = $img.position().left;
			var top = $img.position().top;
			var width = $img.width();
			var height = $img.height();
			
			$(this).next('loading').remove();
			$(this).next('loading-wrapper').remove();
			
			$loading_wrapper = $('<div />');
			$loading_wrapper.addClass('loading-wrapper');
			$loading_wrapper.css
			({
				'background': '#000',
				'position': 'absolute',
				'left': left,
				'top': top,
				'width': width,
				'height': height,
				'z-index': 6666,
				'opacity': 0.8
			});
			
			$loading = $('<div />');
			$loading.addClass('loading');
			$loading.css
			({
				'background': 'transparent url(' + options.path + 'loading.gif) 50% 50% no-repeat',
				'position': 'absolute',
				'left': left,
				'top': top,
				'width': width,
				'height': height,
				'z-index': 6667
			});
			
			$(this).after($loading_wrapper);
			$(this).after($loading);
			
			if (typeof src == 'undefined')
			{
				src = $(this).attr('src');
			}
			
			$(this).attr('src', src + '?' + new Date()).load( function()
			{
				$(this).next('.loading').remove();
				$(this).next('.loading-wrapper').remove();
			});
		});
		
		return this;
	};
})(jQuery);

