<?php $instance['per_row'] = ! empty( $instance['per_row'] ) ? $instance['per_row'] : 3; ?>

<?php echo wp_kses( $args['before_widget'], wp_kses_allowed_html( 'post' ) ); ?>

<?php if ( empty ( $instance['display'] ) ) : ?>
	<div class="alert alert-warning">
		<?php echo __( 'Display type not defined', 'realia' ); ?>
	</div><!-- /.alert -->
<?php else: ?>
	<?php if ( ! empty( $instance['title'] ) ) : ?>
	    <?php echo wp_kses( $args['before_title'], wp_kses_allowed_html( 'post' ) ); ?>
	    <?php echo esc_attr( $instance['title'] ); ?>
	    <?php echo wp_kses( $args['after_title'], wp_kses_allowed_html( 'post' ) ); ?>
	<?php endif; ?>

	<?php if ( have_posts() ) : ?>
		<div class="type-<?php echo esc_attr( $instance['display'] ); ?>">
			<?php if ( $instance['display'] == 'box' ) : ?>
				<div class="row">
					<?php while( have_posts() ) : the_post(); ?>				
						<div class="col-sm-6 col-md-<?php echo 12 / $instance['per_row']; ?>">
							<?php include Realia_Template_Loader::locate( 'properties/box-simple' ); ?>
						</div><!-- /.col-sm-4 -->
					<?php endwhile; ?>
				</div>
			<?php elseif ( $instance['display'] == 'only-image' ) : ?>
				<div class="row">
					<?php $index = 1; ?>
					<?php global $wp_query; ?>

					<?php while( have_posts() ) : the_post(); ?>				
						<div class="col-sm-6 col-md-<?php echo 12 / $instance['per_row']; ?>">
							<?php include Realia_Template_Loader::locate( 'properties/only-image' ); ?>
						</div><!-- /.col-sm-4 -->

						<?php if ( $index % $instance['per_row'] == 0 && $index < count( $wp_query->posts ) ) : ?>
							</div><div class="row">
						<?php endif; ?>	

						<?php $index++; ?>
					<?php endwhile; ?>
				</div><!-- /.row -->
			<?php elseif( $instance['display'] == 'small' ): ?>
				<?php while( have_posts() ) : the_post(); ?>				
					<?php include Realia_Template_Loader::locate( 'properties/small' ); ?>
				<?php endwhile; ?>
			<?php endif; ?>
		</div>
	<?php else : ?>
		<div class="alert alert-warning">
			<?php echo __( 'No properties found.', 'realia' ); ?>
		</div><!-- /.alert -->		
	<?php endif; ?>
<?php endif; ?>

<?php echo wp_kses( $args['after_widget'], wp_kses_allowed_html( 'post' ) ); ?>