<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<?php $gallery = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'gallery', true ); ?>

	<?php if ( ! empty( $gallery ) ) : ?>
		<div class="property-gallery">
			<div class="property-gallery-preview">
				<?php echo wp_get_attachment_image( key( $gallery ) , 'full' );?>

                <?php $is_sticky = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'sticky', true ); ?>
                <?php $is_featured = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'featured', true ); ?>
                <?php $is_reduced = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'reduced', true ); ?>

                <?php if ( $is_featured && $is_reduced ) : ?>
                    <span class="property-badge"><?php echo __( 'Featured', 'realia' ); ?> / <?php echo __( 'Reduced', 'realia' ); ?></span>
                <?php elseif ( $is_featured ) : ?>
                    <span class="property-badge"><?php echo __( 'Featured', 'realia' ); ?></span>
                <?php elseif ( $is_reduced ) : ?>
                    <span class="property-badge"><?php echo __( 'Reduced', 'realia' ); ?></span>
                <?php endif; ?>

                <?php if ( $is_sticky ) : ?>
                    <span class="property-badge property-badge-sticky"><?php echo __( 'TOP', 'realia' ); ?></span>
                <?php endif; ?>
			</div>

			<ul class="property-gallery-index">
				<?php $index = 0; ?>
				<?php foreach ( $gallery as $id => $src ) : ?>
					<li <?php echo ( $index == 0 ) ? 'class="active"' : ''; ?>>
						<a rel="<?php echo esc_url( $src ); ?>"></a>
						<?php $index++; ?>
					</li>
				<?php endforeach; ?>
			</ul>
		</div><!-- /.property-gallery -->
	<?php endif; ?>

	<header class="entry-header">
		<?php
		if ( is_single() ) :
			the_title( '<h1 class="entry-title">', '</h1>' );
		else :
			the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
		endif;
		?>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<div class="property-overview">
			<dl>
				<?php $price = Realia_Price::get_property_price(); ?>
				<?php if ( ! empty( $price ) ) : ?>
					<dt><?php echo __( 'Price', 'realia' )?></dt><dd><?php echo wp_kses( $price, wp_kses_allowed_html( 'post' ) ); ?></dd>
				<?php endif; ?>

				<?php $id = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'id', true ); ?>
				<?php if ( ! empty( $id ) ) : ?>
					<dt><?php echo __( 'ID', 'realia' ); ?></dt><dd><?php echo esc_attr( $id ); ?></dd>
				<?php endif; ?>

				<?php $type = Realia_Query::get_property_type_name(); ?>
				<?php if ( ! empty ( $type ) ) : ?>
					<dt><?php echo __( 'Type', 'realia' ); ?></dt><dd><?php echo esc_attr( $type ); ?></dd>
				<?php endif; ?>

				<?php $contract = Realia_Query::get_property_contract_name(); ?>
				<?php if ( ! empty ( $contract ) ) : ?>
					<dt><?php echo __( 'Contract', 'realia' ); ?></dt><dd><?php echo esc_attr( $contract ); ?></dd>
				<?php endif; ?>

				<?php $location = Realia_Query::get_property_location_name(); ?>
				<?php if ( ! empty ( $location ) ) : ?>
					<dt><?php echo __( 'Location', 'realia' ); ?></dt><dd><?php echo wp_kses( $location, wp_kses_allowed_html( 'post' ) ); ?></dd>
				<?php endif; ?>

				<?php $area = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'attributes_area', true ); ?>
				<?php if ( ! empty( $area ) ) : ?>
					<dt><?php echo __( 'Area', 'realia' ); ?></dt><dd><?php echo esc_attr( $area ); ?> <?php echo get_theme_mod( 'realia_measurement_area_unit', 'sqft' ); ?></dd>
				<?php endif; ?>

				<?php $baths = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'attributes_baths', true ); ?>
				<?php if ( ! empty( $baths ) ) : ?>
					<dt><?php echo __( 'Baths', 'realia' ); ?></dt><dd><?php echo esc_attr( $baths ); ?></dd>
				<?php endif; ?>

				<?php $beds = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'attributes_beds', true ); ?>
				<?php if ( ! empty( $beds ) ) : ?>
					<dt><?php echo __( 'Beds', 'realia' ); ?></dt><dd><?php echo esc_attr( $beds ); ?></dd>
				<?php endif; ?>

				<?php $garages = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'attributes_garages', true ); ?>
				<?php if ( ! empty( $garages ) ) : ?>
					<dt><?php echo __( 'Garages', 'realia' ); ?></dt><dd><?php echo esc_attr( $garages ); ?></dd>
				<?php endif; ?>
			</dl>
		</div><!-- /.property-overview -->

		<?php the_content( sprintf(
			__( 'Continue reading %s', 'realia' ),
			the_title( '<span class="screen-reader-text">', '</span>', false )
		) ); ?>

        <?php $amenities = get_categories( array(
            'taxonomy' 		=> 'amenities',
            'hide_empty' 	=> false,
        ) ) ; ?>

        <?php $hide = get_theme_mod( 'realia_general_hide_unassigned_amenities', false ); ?>
        <?php if ( ! empty( $amenities ) ) : ?>
            <div class="property-amenities">
                <ul>
                    <?php foreach ( $amenities as $amenity ) : ?>
                        <?php $has_term = has_term( $amenity->term_id, 'amenities' ); ?>

                        <?php if ( ! $hide || ( $hide  && $has_term ) ) : ?>
                            <li <?php if ( $has_term ): ?>class="yes"<?php else : ?>class="no"<?php endif; ?>><?php echo esc_html( $amenity->name ); ?></li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            </div><!-- /.property-amenities -->
        <?php endif; ?>

		<?php $images = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'plans', true ); ?>

		<?php if ( ! empty( $images ) ) : ?>
			<div class="property-floor-plans">
				<?php foreach ( $images as $id => $url ) : ?>
	                <a href="<?php echo esc_url( $url ); ?>" rel="property-plans">
	                    <?php echo wp_get_attachment_image( $id, 'thumbnail' ); ?>
	                </a>
                <?php endforeach; ?>
			</div><!-- /.property-floor-plans -->
		<?php endif; ?>

        <!-- VALUATION -->
        <?php $crime = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'valuation_crime', true ); ?>
        <?php $traffic = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'valuation_traffic', true ); ?>
        <?php $pollution = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'valuation_pollution', true ); ?>
        <?php $education = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'valuation_education', true ); ?>
        <?php $health = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'valuation_health', true ); ?>

        <?php if ( ! empty( $crime ) || ! empty( $traffic ) || ! empty( $pollution ) || ! empty( $education ) || ! empty( $health ) ) : ?>
	        <div class="property-valuation">
	            <?php if ( ! empty( $crime ) ) : ?>
	                <dt><?php echo __( 'Crime', 'realia' ); ?></dt>
	                <dd><span style="width: <?php echo esc_attr( $crime ); ?>%;"><strong><?php echo esc_attr( $crime ); ?> %</strong></span></dd>
	            <?php endif; ?>

	            <?php if ( ! empty( $traffic ) ) : ?>
	                <dt><?php echo __( 'Traffic', 'realia' ); ?></dt>
	                <dd><span style="width: <?php echo esc_attr( $traffic ); ?>%;"><strong><?php echo esc_attr( $traffic ); ?> %</strong></span></dd>
	            <?php endif; ?>

	            <?php if ( ! empty( $pollution ) ) : ?>
	                <dt><?php echo __( 'Pollution', 'realia' ); ?></dt>
	                <dd><span style="width: <?php echo esc_attr( $pollution ); ?>%;"><strong><?php echo esc_attr( $pollution ); ?> %</strong></span></dd>
	            <?php endif; ?>

	            <?php if ( ! empty( $education ) ) : ?>
	                <dt><?php echo __( 'Education', 'realia' ); ?></dt>
	                <dd><span style="width: <?php echo esc_attr( $education ); ?>%;"><strong><?php echo esc_attr( $education ); ?> %</strong></span></dd>
	            <?php endif; ?>

	            <?php if ( ! empty( $health ) ) : ?>
	                <dt><?php echo __( 'Health', 'realia' ); ?></dt>
	                <dd><span style="width: <?php echo esc_attr( $health ); ?>%;"><strong><?php echo esc_attr( $health ); ?> %</strong></span></dd>
	            <?php endif; ?>
	        </div><!-- /.property-valuation -->

        <?php endif; ?>

		<?php wp_link_pages( array(
			'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'realia' ) . '</span>',
			'after'       => '</div>',
			'link_before' => '<span>',
			'link_after'  => '</span>',
			'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'realia' ) . ' </span>%',
			'separator'   => '<span class="screen-reader-text">, </span>',
		) );
		?>

        <?php if ( comments_open() || get_comments_number() ): ?>
            <div class="box">
                <?php comments_template( '', true ); ?>
            </div>
        <?php endif; ?>
	</div><!-- .entry-content -->

	<?php
	// Author bio.
	if ( is_single() && get_the_author_meta( 'description' ) ) :
		get_template_part( 'author-bio' );
	endif;
	?>

	<footer class="entry-footer">
		<?php twentyfifteen_entry_meta(); ?>
		<?php edit_post_link( __( 'Edit', 'realia' ), '<span class="edit-link">', '</span>' ); ?>
	</footer><!-- .entry-footer -->

</article><!-- #post-## -->
