(function($) {

    $.fn.ps_defer = function(options) {
        var settings = {
            threshold    : 0,
            failurelimit : 0,
            event        : "scroll",
            effect       : "show",
            container    : window
        };
                
        if(options) {
            $.extend(settings, options);
        }
		
		
        this.each(function() 
        {
            var self = this;
            
            /* Save original only if it is not defined in HTML. */
            
            if (undefined == $(self).attr("original")) 
            {
                $(self).attr("original", $(self).attr("src"));
                $(self).removeAttr("src");
                $(self).attr('loaded','false');
                $(self).attr('isReady','false');                
            }
            $(self).bind('appear', function()
            {
            	if ($(self).attr('loaded') == 'false')
            	{
            		$(self).attr('src', $(self).attr('original'));
            		$(self).attr('loaded','true');
            	}
			});
			$(self).load(function(evt)
			{
				$(evt.target).attr('isReady','true');
			});
		});
	}
})(jQuery);
