<?php
	if(!function_exists('fp_wp_simply_good_hero_slider_generate')) :
		function fp_wp_simply_good_hero_slider_generate($group_identifier, $posts, $is_mobile=false) {
?>
	<!-- Tiny Slider -->
	<div id="hero-slider-container-<?= $group_identifier ?>" class="hero-slider-container hidden <?= $is_mobile ? 'visible-xs' : 'hidden-xs' ?>">
		<ul id="hero-slider-<?= $group_identifier ?>" class="hero-slider">
		<?php 
			foreach ($posts as $post) :
				$url = types_render_field("hero-image-url", array("post_id"=>$post->ID, "raw"=>"true"));
		?>
			<li data-start-date="<?= types_render_field("start-date", array("post_id"=>$post->ID, "raw"=>"true")) ?>" data-end-date="<?= types_render_field("end-date", array("post_id"=>$post->ID, "raw"=>"true")) ?>">
				<?php if (!empty($url)) { ?><a href="<?= $url ?>" title="<?= $post->post_title ?>" target="<?= types_render_field("url-target-new-window", array("post_id"=>$post->ID, "raw"=>"true")) ?>"><?php } ?>
				<img
					src="<?= types_render_field($is_mobile ? 'hero-image-mobile' : 'hero-image', array("post_id"=>$post->ID, 'raw'=>'true')) ?>"
					alt="<?= types_render_field("hero-image-alt-text", array("post_id"=>$post->ID, "raw"=>"true")) ?>"
					class="full-width"
				/>
				<?php if (!empty($url)) { ?></a><?php } ?>
			</li>
		<?php endforeach; ?>
		</ul>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {
			$('#hero-slider-container-<?= $group_identifier ?>')
				.removeClass('hidden')
				.find('li')
				.each(function() {
					var $this = $(this),
						now = new Date(),
						startDate = $this.attr('data-start-date'),
						endDate = $this.attr('data-end-date');

					if(startDate && (new Date(startDate * 1000)) > now) {
						//remove slides that have a start date greater than current date
						$this.remove();
					}
					else if(endDate && (new Date(endDate * 1000)) <= now) {
						//remove slides that have a end date less than or equal to current date
						$this.remove();
					}
				});

			tns({
				container: '#hero-slider-<?= $group_identifier ?>',
				mode: 'gallery',
				navPosition: 'bottom',
				items: 1,
				slideBy: 'page',
				autoplayTimeout: 5000,
				speed: 800,
				animateIn: 'tns-animate-in',
				animateOut: 'no-animate',
				autoplay: true,
				autoplayButtonOutput: false,
				center: true
			});
		});
	</script>
	<?php } ?>
<?php endif; ?>
<?php
	$group_identifier = @$attrs['group_identifier'];
	if(!empty($group_identifier)) {
		$posts = get_posts(array(
			'post_type' => 'hero-slide',
			'meta_query' => array(array('key' => 'wpcf-hero-group-identifier', 'value' => $group_identifier)),
			'orderby'   => 'menu_order',
			'order'     => 'ASC',
			'numberposts' => -1
		));
			
		//generate mobile carousel if there are any mobile images
		$existing_timezone = date_default_timezone_get();
        date_default_timezone_set( 'UTC' );
		$time_now = current_time('timestamp');

		foreach ( $posts as $key => $post ) {
			$start_date = types_render_field("start-date", array("post_id"=>$post->ID, "raw"=>"true"));
			$end_date = types_render_field("end-date", array("post_id"=>$post->ID, "raw"=>"true"));

			//date('Y-m-d H:i:s', $start_date)
			
			//All date comparisons assume UTC dates
			//remove slides that have a start date greater than 24 hours from now
			if (!empty($start_date) && round(($start_date - $time_now) / (60 * 60)) > 24) {
				unset( $posts[$key] );
			}
			else if (!empty($end_date) && $end_date - $time_now <= 0)  {
				//remove slides that have an end date less than or equal to current date
				unset( $posts[$key] );
			}
		}

		date_default_timezone_set( $existing_timezone );
		
		if (count($posts)) {
			fp_wp_simply_good_hero_slider_generate($group_identifier, $posts);
			fp_wp_simply_good_hero_slider_generate("$group_identifier-mobile", $posts, true);
		}
	}
?>