<?php get_header();
$sDateFormat = get_option( 'date_format' );
$sEventCatSlug = get_query_var( 'uni_event_cat', null );
$oCurrentTerm = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
        $iCheckoutAttachId = ( ot_get_option( 'uni_events_header_bg' ) ) ? ot_get_option( 'uni_events_header_bg' ) : '';
        if ( !empty($iCheckoutAttachId) ) {
            $aPageHeaderImage = wp_get_attachment_image_src( $iCheckoutAttachId, 'full' );
            $sPageHeaderImage = $aPageHeaderImage[0];
        } else {
            $sPageHeaderImage = get_template_directory_uri().'/images/placeholders/pageheader-events.jpg';
        }
        $sDateAndTimeFormat = get_option( 'date_format' ).' '.get_option( 'time_format' );
?>

	<section class="uni-container">

		<div class="pageHeader" style="background-image: url(<?php echo esc_url( $sPageHeaderImage ); ?>);">
			<h1><?php echo ( ot_get_option( 'uni_events_header_title' ) ) ? ot_get_option( 'uni_events_header_title' ) : __('follow our events', 'asana'); ?></h1>
		</div>

		<div class="contentWrap">
			<div class="pagePanel clear">
				<div class="pageTitle">
                    <?php single_term_title(); ?>
                    <?php
                    if ( isset($_GET['events_sort']) && $_GET['events_sort'] == 'newer' ) {
                        echo '<span class="pageTitle-sort-desc">(' . esc_html__('newer to older', 'asana') . ')</span>';
                    } else if ( isset($_GET['events_sort']) && $_GET['events_sort'] == 'older' ) {
                        echo '<span class="pageTitle-sort-desc">(' . esc_html__('older to newer', 'asana') . ')</span>';
                    } else {
                        echo '<span class="pageTitle-sort-desc">(' . esc_html__('by date created', 'asana') . ')</span>';
                    }
                    ?>
                </div>
                <?php
                $aEventTerms = get_terms( 'uni_event_cat' );
                if ( !empty($aEventTerms) && !is_wp_error($aEventTerms) ) {
                ?>
				<div class="categoryList">
					<span><?php esc_html_e('category', 'asana') ?> <i></i></span>
					<ul>
						<li><a href="<?php if ( ot_get_option( 'uni_events_page' ) ) echo get_permalink( ot_get_option( 'uni_events_page' ) ); ?>"><?php esc_html_e('All', 'asana') ?></a></li>
                    <?php foreach ( $aEventTerms as $oTerm ) { ?>
						<li<?php if ( $sEventCatSlug == $oTerm->slug ) echo ' class="current"'; ?>><a href="<?php echo get_term_link($oTerm) ?>"><?php echo esc_html( $oTerm->name ); ?></a></li>
					<?php } ?>
					</ul>
				</div>
                <?php } ?>
                <div class="sortingList">
                    <span><?php esc_html_e('sorting', 'asana') ?> <i></i></span>
                    <ul>
                        <li<?php if ( !isset($_GET['events_sort']) ) echo ' class="current"'; ?>>
                            <a href="<?php echo esc_url( get_term_link($oCurrentTerm) ) ?>"><?php esc_html_e('By date created', 'asana') ?></a>
                        </li>
                        <li<?php if ( isset($_GET['events_sort']) && $_GET['events_sort'] == 'newer' ) echo ' class="current"'; ?>>
                            <a href="<?php echo esc_url( add_query_arg( array( 'events_sort' => 'newer' ), get_term_link($oCurrentTerm) ) ) ?>"><?php esc_html_e('Newer first', 'asana') ?></a>
                        </li>
                        <li<?php if ( isset($_GET['events_sort']) && $_GET['events_sort'] == 'older' ) echo ' class="current"'; ?>>
                            <a href="<?php echo esc_url( add_query_arg( array( 'events_sort' => 'older' ), get_term_link($oCurrentTerm) ) ) ?>"><?php esc_html_e('Older first', 'asana') ?></a>
                        </li>
                    </ul>
                </div>
			</div>
			<div class="eventsWrap">
        <?php if (have_posts()) : while (have_posts()) : the_post();
            $aCustomData = get_post_custom( $post->ID );
        ?>
				<div id="post-<?php the_ID(); ?>" <?php post_class('eventItem clear') ?>>
					<a href="<?php echo the_permalink() ?>" class="eventItemImg">
                        <?php if ( has_post_thumbnail() ) { ?>
                            <?php the_post_thumbnail( 'unithumb-eventpost', array( 'alt' => the_title_attribute('echo=0') ) ); ?>
                        <?php } else { ?>
						    <img src="<?php echo get_template_directory_uri(); ?>/images/placeholders/unithumb-eventpost.jpg" alt="<?php the_title_attribute() ?>" width="502" height="342">
                        <?php } ?>
					</a>
					<div class="eventItemDesc">
						<time class="eventItemTime" datetime="<?php if ( !empty($aCustomData['uni_event_date'][0]) ) { $iEventTimestamp = strtotime($aCustomData['uni_event_date'][0]); echo date('Y-m-d', $iEventTimestamp); } ?>"><?php if ( !empty($aCustomData['uni_event_date'][0]) ) { echo date($sDateFormat, $iEventTimestamp); } else { esc_html_e('- not specified -'); } ?></time>
						<h3><a href="<?php echo the_permalink() ?>"><?php echo the_title() ?></a></h3>
						<?php uni_excerpt(30, '', true) ?>
						<a href="<?php echo the_permalink() ?>" class="eventLearnMore"><?php _e('learn more', 'asana') ?>
							<i>
								<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="38px" height="38px" viewBox="0 0 38 38" enable-background="new 0 0 38 38" xml:space="preserve">
									<path fill="#6BBFAC" d="M16.558 11.5l6.884 6.494l1.059 0.999v0.015L16.558 26.5L15.5 25.486l6.882-6.494L15.5 12.5L16.558 11.5z"/>
								</svg>
							</i>
						</a>
					</div>
				</div>
        <?php
        endwhile;
        endif;
        ?>

		    <div class="pagination clear"<?php if ( ot_get_option('uni_ajax_scroll_enable_events') == 'on' ) { echo ' style="display:none;"'; } ?>>
	    <?php
        $big = 999999999;
		echo paginate_links( array(
			'base'         => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
			'format'       => '?paged=%#%',
			'add_args'     => '',
			'current'      => max( 1, get_query_var( 'paged' ) ),
			'total'        => $wp_query->max_num_pages,
			'prev_text'    => '<i>
								<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="&#1057;&#1083;&#1086;&#1081;_1" x="0px" y="0px" width="7px" height="11px" viewBox="0 0 7 11" enable-background="new 0 0 7 11" xml:space="preserve">
									<path fill="#c3c3c3" class="paginationArrowIcon" d="M0.95 4.636L6.049 0L7 0.864L1.899 5.5L7 10.136L6.049 11L0 5.5L0.95 4.636z"/>
								</svg>
							</i>'.esc_html__('previous', 'asana'),
			'next_text'    => esc_html__('next', 'asana').'<i>
								<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="&#1057;&#1083;&#1086;&#1081;_1" x="0px" y="0px" width="7px" height="11px" viewBox="0 0 7 11" enable-background="new 0 0 7 11" xml:space="preserve">
									<path fill="#c3c3c3" class="paginationArrowIcon" d="M6.05 6.364L0.951 11L0 10.136L5.102 5.5L0 0.864L0.951 0L7 5.5L6.05 6.364z"/>
								</svg>
							</i>',
			'type'         => 'list',
			'end_size'     => 3,
			'mid_size'     => 3
		) );
	    ?>
		    </div>

			</div>

    <?php if ( ot_get_option('uni_mailchimp_events_enable') == 'on' ) { ?>
    <?php
        $iSubscribeAttachId = ( ot_get_option( 'uni_subscribe_header_bg' ) ) ? ot_get_option( 'uni_subscribe_header_bg' ) : '';
        if ( !empty($iSubscribeAttachId) ) {
            $aPageHeaderImage = wp_get_attachment_image_src( $iSubscribeAttachId, 'full' );
            $sPageHeaderImage = $aPageHeaderImage[0];
        } else {
            $sPageHeaderImage = get_template_directory_uri().'/images/placeholders/pageheader-events.jpg';
        }
    ?>
			<div class="subscribeBox" style="background-image: url(<?php echo esc_url( $sPageHeaderImage ); ?>);">
				<i class="iconEmail"></i>
				<h3><?php echo ( ot_get_option( 'uni_subscribe_header_title' ) ) ? esc_html( ot_get_option( 'uni_subscribe_header_title' ) ) : __('subscribe to our newsletter', 'asana'); ?></h3>
				<p><?php echo ( ot_get_option( 'uni_subscribe_header_subtitle' ) ) ? esc_html( ot_get_option( 'uni_subscribe_header_subtitle' ) ) : __('Subscribe and take all information about our latest events', 'asana'); ?></p>
		        <form action="<?php echo admin_url( 'admin-ajax.php' ); ?>" role="form" method="post" class="clear uni_form">
                    <input type="hidden" name="action" value="uni_mailchimp_subscribe_user" />
					<input type="text" name="uni_input_email" size="20" value="" placeholder="<?php _e('Your email', 'asana' ); ?>" data-parsley-required="true" data-parsley-trigger="change focusout submit" data-parsley-type="email">
					<input class="subscribeSubmit uni_input_submit" type="button" value="<?php _e('subscribe', 'asana' ); ?>">
				</form>
			</div>
    <?php } ?>

		</div>

	</section>

<?php get_footer(); ?>