/**
 * jQuery.lineWrapper
 * ------------------
 * S'encarrega d'agrupar imatges en files.
 * Les files se separen amb un div d'un pixel d'alçada
 */
jQuery.fn.lineWrapper = function(options) {

    // obtenim els paràmetres
    settings = jQuery.extend({
        elements: "div.brcm-image-and-caption",
        debug: false
    }, options);
    
	var previous = null;
    var currentWidth = 0;
    var $container = jQuery(this);
    var containerWidth = $container.width();
    var children = $container.find(settings.elements);

    function log(message) {
        if (settings.debug && console && console.log) {
           console.log(message);
        }
    }

    log("Container width: " + containerWidth);
    
    children.each(function() {
        // Acumular amples
        log(this);
        var imageWidth = jQuery(this).outerWidth(true) ;
        log("Image width: " + imageWidth);
        currentWidth += imageWidth;
        log("Current width: " + currentWidth);

        // Si l'ample acumulat supera el del contenidor
        if (currentWidth > containerWidth) {
            
			// creem un div per separar les files
            $separadorFiles = jQuery("<div></div>").attr("style", "clear: left; height: 1px;");
            jQuery(this).before($separadorFiles);
            log("Separator");
            
			// i reiniciem l'ample acumulat
            currentWidth = imageWidth;
        }
        previous = this;
    });
};

