<?php
	
// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
	echo 'Hi there!  I\'m just a plugin, not much I can do when called directly.';
	exit;
}
?>

<?php
	$day = date( 'D', strtotime( $from_date ) );		
	$datenum = date( 'd', strtotime( $from_date ) );		
	$month = date( 'M', strtotime( $from_date ) );
	$shortmonth = date( 'm', strtotime( $from_date ) );
	
	$date = new DateTime( $from_date );
	
	if ( $checkmonth != $shortmonth ) {
		?>
		<?php if ( $checkmonth != '' ) { ?>
		<div class="dheading">
			<div class="dcell"><?php echo adrotate_group(7); ?></div>
		</div>
		<?php } ?>
		<div class="dheading">
			<div class="dcell">
				<p><?php echo $month .' '. date( 'Y', strtotime( $from_date ) ); ?></p>
			</div>
		</div>
		<?php	
	}
	$checkmonth = $shortmonth;
	
	
	if ( $week != $date->format("W") ) {
		$week = $date->format("W");
		echo '<h6 style="margin:-10px 0 10px 15px;opacity:.6;">Week '. $week .'</h6>';
	}
?>

<?php  
	//print_r(get_post_meta( get_the_ID()));
	$trailvalues = get_post_meta( $tournamentsID, 'trail', true );
	$trail = '';
	//print_r($division);
	if( isset($trailvalues) && intval( $trailvalues ) > 0 ) {
		$trailpost = get_posts( array( 'p' => $trailvalues, 'post_type'=> 'trails', 'fields' => 'ids' ) );
	
		if ( !empty( $trailpost ) && intval( $trailpost[0] ) > 0 ) {

			$division= get_post_meta( $trailpost[0], 'division', true );

			$trail .=  ' <span class="mini_splitter">|</span> <a href="'.get_the_permalink( $trailpost[0] ).'" >'. titlechange( get_the_title( $trailpost[0] ) ) .'</a>';
			if ( !empty( $division ) ) {
				$trail .= " / <span style='font-weight:normal !important;'>".$division."</span>";
			}

			// /* Restore original Post Data */
			wp_reset_postdata();
		} else {
			// no posts found
		}
	}
?>
<?php  
	$lakesvalues= get_post_meta( $tournamentsID,'lake');
	$lake = '';
	if(isset($lakesvalues[0]) && is_numeric($lakesvalues[0]) && $lakesvalues[0] > 0) {
		$lakespost = new WP_Query(array( 'p' => $lakesvalues[0],'post_type'=> 'lakes' ));
		if ( $lakespost->have_posts() ) {
			while ( $lakespost->have_posts() ) {
				$lakespost->the_post();
				$lake .=  '<a href="'.get_permalink().'" >'.titlechange(get_the_title()).'</a>';
			}
			/* Restore original Post Data */
			wp_reset_postdata();
		}
	}
	
	$landingvalues= get_post_meta( $tournamentsID,'landing');
	if(isset($landingvalues[0]) && is_numeric($landingvalues[0]) && $landingvalues[0] > 0) {
		$landingpost = new WP_Query(array( 'p' => $landingvalues[0],'post_type'=> 'landings' ));
		if ( $landingpost->have_posts() ) {
			while ( $landingpost->have_posts() ) {
				$landingpost->the_post();
				$lake .=  ' <span class="mini_splitter">|</span> <a href="'.get_permalink().'" ><span style="text-transform:uppercase;" class="post_info_date">'.get_the_title().'</span></a>';
			}
			/* Restore original Post Data */
			wp_reset_postdata();
		}
	}
	
	$date_span = '';
	if ( $from_date != $to_date ) {
		$date_span = '<span class="datespan post_info_date">'. ( $lake != '' ? ' | ' : '' ) .'From '. date( 'j M', strtotime( $from_date ) ) .' to '. date( 'j M', strtotime( $to_date ) ).'</span>';
	}
	
	$featured = get_field( 'featured', $tournamentsID );
	$is_featured = (  ( isset( $featured[0] ) && $featured[0] == 'yes' ) ? 'is_featured' : '' );
?>

<div class="drow <?=$is_featured;?>" data-month="<?php echo $shortmonth; ?>" data-year="<?php echo date( 'Y', strtotime( $from_date ) ); ?>">
	<div class="dcell event_date_cell">
		<span class="event_date"><?php echo $day; ?></span>
		<span class="event_num"><?php echo $datenum; ?></span>
		<span class="event_month"><?php echo $month; ?></span>
	</div>
	<div class="dcell event_info_cell">
		<span class="tournament_line tournament_line1"><a href="<?php echo get_the_permalink( $tournamentsID );?>"><?php echo titlechange(get_the_title( $tournamentsID ));?></a> <?php echo $trail; ?></span>
		<span class="tournament_line tournament_line2"><?php echo $lake . $date_span; ?></span>
		
	</div>
</div>

