<?php
/**
 * @package     SP Soccer
 * @author      JoomShaper http://www.joomshaper.com
 * @copyright   Copyright (C) 2010 - 2016 JoomShaper. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die('Restricted access');

//Load Helix
$helix3_path = JPATH_PLUGINS.'/system/helix3/core/helix3.php';
if (file_exists($helix3_path)) {
    require_once($helix3_path);
    $this->helix3 = Helix3::getInstance();
} else {
    die('Please install and activate helix plugin');
}

$this->helix3->addJS('jquery.countdown.min.js');

?>
<div id="spsoccer" class="spsoccer view-spsoccer-tournament">
<div class="spsoccer-match-top-content">
		<?php  if(!empty($this->sessions)) {
			$loaded_item = array();
			$now = JHtml::_('date', JFactory::getDate(), 'Y-m-d h:m:s');
		?>
		<?php
			foreach ($this->sessions as $session) {
				// has session matches
				if (!empty($session->session_matches)) {
				$item_no = 1;
				foreach ($session->session_matches as $key => $top_session_match) {
				$match_datetime = $top_session_match->date . ' ' . $top_session_match->time;
				if($match_datetime > $now) {
					$loaded_item [] = $top_session_match->spsoccer_match_id;
					unset($session->session_matches[$key]);
					$title_text = ($item_no == 1) ? JText::_('COM_SOSOCCER_TODAY_MATCH') : JText::_('COM_SOSOCCER_NEXT_MATCH');

					$countdown_time = JHtml::_('date', $match_datetime, 'Y/m/d h:m');
				?>
					<div class="spsoccer-col-sm-6">
						<div class="spsoccer-featured-match-wrap<?php echo ($item_no == 2) ? ' right': ''; ?>">
							<div class="spsoccer-row">
								<div class="spsoccer-col-sm-12">
									<div class="spsoccer-upcoming-match-countdown">
										<h4><?php echo $title_text; ?></h4>
										<div class="spsoccermatchtdown" data-countdown="<?php echo $countdown_time; ?>"></div>
									</div>
								</div>
								<div class="spsoccer-col-lg-4 spsoccer-col-md-4 spsoccer-col-sm-12 text-center">
									<a href="<?php echo $top_session_match->team_one_url; ?>" class="spsoccer-club-logo">
										<img class="spsoccer-img-thumbnail spsoccer-img-responsive soccer-team-thumb" src="<?php echo $top_session_match->team_one_info->logo; ?>" alt="<?php echo $top_session_match->team1_info->title; ?>">
										<span class="spsoccer-team-name"><?php echo $top_session_match->team_one_info->title; ?></span>
									</a>
								</div> <!-- /.spsoccer-col-lg -->

								<div class="spsoccer-col-lg-4 spsoccer-col-md-4 spsoccer-col-sm-12 text-center">
									<div class="spsoccer-match-time-location">
										<div class="spsoccer-match-date-time">
											<span><?php echo $top_session_match->match_date; ?>,</span>
											<span><?php echo $top_session_match->match_time; ?></span>
										</div>
										<p class="spsoccer-match-location">
											<a href="<?php echo $top_session_match->location_url ?>">
												<?php echo $top_session_match->match_location->title; ?>
											</a>
										</p>
									</div>
								</div><!-- /.spsoccer-col-lg -->

								<div class="spsoccer-col-lg-4 spsoccer-col-md-4 spsoccer-col-sm-12 text-center">
									<a href="<?php echo $top_session_match->team_two_url; ?>" class="spsoccer-club-logo">
										<img class="spsoccer-img-thumbnail spsoccer-img-responsive soccer-team-thumb" src="<?php echo $top_session_match->team_two_info->logo; ?>" alt="<?php echo $top_session_match->team_two_info->title; ?>">
										<span class="spsoccer-team-name"><?php echo $top_session_match->team_two_info->title; ?></span>
									</a>
								</div><!-- /.spsoccer-col-lg -->
							</div>
						</div>
					</div>
				
					<?php if($key==1) {
						break;
					} ?>
			<?php $item_no++; } // if upcoming match ?>
			<?php } // foreach session_matches ?>

		<?php } // has $session->session_matches ?>
		<?php } // foreach session ?>
	<?php } // has sessions ?>
	</div> <!-- spsoccer-match-top-content -->
	<div class="spsoccer-fixture-content-wrap">
		<div class="spsoccer-tournament">
			<div class="spsoccer-container">
				<div id="tournament-fixture">
					<!-- has Sessions -->
					<?php if(!empty($this->sessions)) {?>

						<?php
							foreach ($this->sessions as $session) {
							// has session matches
							if (!empty($session->session_matches)) {
						?>
						<div class="spsoccer-fixture-wrapper">
							<?php foreach ($session->session_matches as $session_match) {
								$session_match->team_one_info->logo = SpsoccerHelper::hasImg($session_match->team_one_info->logo);
								$session_match->team_two_info->logo = SpsoccerHelper::hasImg($session_match->team_two_info->logo);
							?>
								<!-- START:: fixture -->
								<div class="item-list-wrap">

									<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-left">
										<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_match->team_one_info->logo; ?>" alt="<?php echo $session_match->team_one_info->title; ?>">
										<a href="<?php echo $session_match->team_one_url; ?>">
											<span class="spsoccer-team-name"><?php echo $session_match->team_one_info->title; ?></span>
										</a>
									</div> <!-- /.spsoccer-col-lg -->

									<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-center">
										<div class="spsoccer-match-date-time">
											<span><?php echo $session_match->match_date; ?></span>,
											<span><?php echo $session_match->match_time; ?></span>
										</div>
										<p class="spsoccer-match-location">
											<a href="<?php echo $session_match->location_url ?>">
												<?php echo $session_match->match_location->title; ?>
											</a>
										</p>
									</div><!-- /.spsoccer-col-lg -->

									<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-right">
										<a href="<?php echo $session_match->team_two_url; ?>">
											<span class="spsoccer-team-name"><?php echo $session_match->team_two_info->title; ?></span>
										</a>
										<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_match->team_two_info->logo; ?>" alt="<?php echo $session_match->team_two_info->title; ?>">
									</div><!-- /.spsoccer-col-lg -->

								</div>
								<!-- /.spsoccer-row -->

								<?php //print_r($session_match); ?>

							<?php } // END:: forech $session_matches ?>
						</div> <!-- ./spsoccer-group-wrapper -->
						<?php } // END:: has session match  ?>
						<?php } // END:: forech $this->sessions  ?>
					<?php } // END:: !empty sessions ?>

				</div> <!-- /.tournament-fixture -->

			</div> <!-- /. spsoccer-container -->

		</div> <!-- /.spsoccer-tournament -->
	</div> <!-- /.spsoccer-fixture-content-wrap -->
</div> <!-- /#spsoccer .spsoccer -->

<script type="text/javascript">
jQuery(function($) {
	$('.spsoccermatchtdown[data-countdown]').each(function() {
        var $this = $(this), finalDate = $(this).data('countdown');
        $this.countdown(finalDate, function(event) {
            //$this.html(event.strftime('%D days %H:%M:%S'));
            $(this).html(event.strftime('<div class="days"><span class="number">%-D</span><span class="string">%!D:<?php echo JText::_("HELIX_DAY"); ?>,<?php echo JText::_("HELIX_DAYS"); ?>;</span></div><div class="hours"><span class="number">%H</span><span class="string">%!H:<?php echo JText::_("HELIX_HOUR"); ?>,<?php echo JText::_("HELIX_HOURS"); ?>;</span></div><div class="minutes"><span class="number">%M</span><span class="string">%!M:<?php echo JText::_("HELIX_MINUTE"); ?>,<?php echo JText::_("HELIX_MINUTES"); ?>;</span></div><div class="seconds"><span class="number">%S</span><span class="string">%!S:<?php echo JText::_("HELIX_SECOND"); ?>,<?php echo JText::_("HELIX_SECONDS"); ?>;</span></div>'));
            
        }).on('finish.countdown', function(event) {
        	$(this).html('<?php echo JText::_("COM_SPCAMPAIGN_CD_FINISHED"); ?>')
        	.parent().addClass('disabled');

        });
    });
});
</script>
