/*!
 * Masonry layout mode
 * sub-classes Masonry
 * https://masonry.desandro.com
 */

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

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

	// -------------------------- masonryDefinition -------------------------- //
	// create an Outlayer layout class
	var MasonryMode = LayoutMode.create( 'masonry' );

	var proto = MasonryMode.prototype;

	var keepModeMethods = {
		_getElementOffset: true,
		layout: true,
		_getMeasurement: true
	};

	// inherit Masonry prototype
	for ( var method in Masonry.prototype ) {
		// do not inherit mode methods
		if ( ! keepModeMethods[ method ] ) {
			proto[ method ] = Masonry.prototype[ method ];
		}
	}

	var measureColumns   = proto.measureColumns;
	proto.measureColumns = function() {
		// set items, used if measuring first item
		this.items = this.isotope.filteredItems;
		measureColumns.call( this );
	};

	// point to mode options for fitWidth
	var _getOption   = proto._getOption;
	proto._getOption = function( option ) {
		if ( option == 'fitWidth' ) {
			return this.options.isFitWidth !== undefined ?
			this.options.isFitWidth : this.options.fitWidth;
		}
		return _getOption.apply( this.isotope, arguments );
	};

	return MasonryMode;

}));
