/**
 * fitRows layout mode
 */

( function( window, factory ) {
	// universal module definition
	/* jshint strict: false */ /*globals define, module, require */
	if ( typeof define == 'function' && define.amd ) {
		// AMD
		define(
			[
			'../layout-mode'
			],
			factory
		);
	} else if ( typeof exports == 'object' ) {
		// CommonJS
		module.exports = factory(
			require( '../layout-mode' )
		);
	} else {
		// browser global
		factory(
			window.Isotope.LayoutMode
		);
	}

}( window, function factory( LayoutMode ) {
	'use strict';

	var FitRows = LayoutMode.create( 'fitRows' );

	var proto = FitRows.prototype;

	proto._resetLayout = function() {
		this.x    = 0;
		this.y    = 0;
		this.maxY = 0;
		this._getMeasurement( 'gutter', 'outerWidth' );
	};

	proto._getItemLayoutPosition = function( item ) {
		item.getSize();

		var itemWidth = item.size.outerWidth + this.gutter;
		// if this element cannot fit in the current row
		var containerWidth = this.isotope.size.innerWidth + this.gutter;
		if ( this.x !== 0 && itemWidth + this.x > containerWidth ) {
			this.x = 0;
			this.y = this.maxY;
		}

		var position = {
			x: this.x,
			y: this.y
		};

		this.maxY = Math.max( this.maxY, this.y + item.size.outerHeight );
		this.x   += itemWidth;

		return position;
	};

	proto._getContainerSize = function() {
		return { height: this.maxY };
	};

	return FitRows;

}));
