﻿
$.fn.extend({
    buffer: function(options) {
        var defaults = {
            containerTag : ".container",
            excludeStart  : 0,
            excludeLength    : 1
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
            if(options.excludeStart < 0)
                options.excludeStart = 0;
                
            $(this) .find(options.containerTag)
                    .eq(options.excludeStart)
                    .prevAll()
                    .find("img")
                    .each(function(i){
                        $(this).data("buffered", true);
                        $(this).data("buffered-src", $(this).attr("src"));
                        $(this).attr("src", "");
                    });     
            
            $(this) .find(options.containerTag)
                    .eq(options.excludeLength + options.excludeStart - 1)
                    .nextAll()
                    .find("img")
                    .each(function(i){
                        $(this).data("buffered", true);
                        $(this).data("buffered-src", $(this).attr("src"));
                        $(this).attr("src", "");
                    });        
                        
            // Altijd this returnen
            return this;
        });
    },
    unbuffer: function(options) {
        var defaults = {
            containerTag : ".container",
            unbufferStart  : 0,
            unbufferLength : 1
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
            if(options.unbufferStart < 0)
                options.unbufferStart = 0;
                
            $(this) .find(options.containerTag)
                    .each(function(i){
                        if(i >= options.unbufferStart && i < parseInt(options.unbufferStart) + parseInt(options.unbufferLength))
                        {    
                            $(this) .find("img")
                                    .each(function(){
                                        if($(this).data("buffered") == true)
                                        {
                                            $(this).data("buffered", false);
                                            $(this).attr("src", $(this).data("buffered-src"));
                                        }
                                    });
                        }
                    });
            // Altijd this returnen
            return this;
        });
    }
});