<?php
$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;
$tv_day = date("Y-m-d 00:00:00", time());
$day_no = date("N", time());

$current_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$cpath = parse_url($current_url);
$current_url = $protocol . $cpath['host'] . $cpath['path'];

$canonical_link = $current_url;

$rev_menu = [
    strftime('%a', strtotime($tv_day)) => $day_no
];

$tv_days_order = [];
$tv_days_order [] = $day_no;

$menu = [
    $day_no => [
        'day_str' => strftime('%a', strtotime($tv_day)),
        'day' => '<span>' . strftime('%a', strtotime($tv_day)) . '</span><small>'.date("d/m").'</small>',
        'date' => date("Y-m-d 00:00:00", time()),
        'link' => $current_url
    ]
];
$ddate = strtotime(date("Y-m-d 00:00:00", time()));
$dindex = 0;
$dcounter = $day_no;
while($dindex < 6) {
    $next_day_no = $dcounter + 1;
    if($next_day_no > 7) {
        $next_day_no = 1;
        $dcounter = 0;
    }
    $tv_days_order [] = $next_day_no;
    $next_date = date("Y-m-d 00:00:00", $ddate + 24 * 60 * 60 * ($dindex + 1));

    $menu[$next_day_no] = [
        'day_str' => strftime('%a', strtotime($next_date)),
        'day' => '<span>' . strftime('%a', strtotime($next_date)) . '</span><small>'.date("d/m", strtotime($next_date)).'</small>',
        'date' => $next_date,
        'link' => $current_url . '?day=' . strtolower(strftime('%a', strtotime($next_date)))
    ];

    $rev_menu[strtolower(strftime('%a', strtotime($next_date)))] = $next_day_no;

    $dcounter ++;
    $dindex ++;
}

if(isset($_GET['day']) && strlen($_GET['day']) == 3 && array_key_exists($_GET['day'], $rev_menu)) {
    $day_name = strtolower($_GET['day']);
    $day_no = $rev_menu[$day_name];
    $tv_day = $menu[$day_no]['date'];
}

foreach($tv_days_order as $dk=>$dn) {
    if($dn == $day_no) {
        if($dk - 1 >= 0) {
            $rel_prev_page = $current_url . '?day=' . strtolower($menu[$tv_days_order[($dk-1)]]['day_str']);
        }
        if($dk + 1 <= 6) {
            $rel_next_page = $current_url . '?day=' . strtolower($menu[$tv_days_order[($dk+1)]]['day_str']);
        }
    }
}

//echo '$tv_day = '. $tv_day;

//echo '<pre>', var_export($tv_days_order, true), '</pre>';

$must_cache = false;
$module_dir = "tvmodule";
$admin_view = false;
if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
    $admin_view = true;
}

$include_www = "https://www.cypruswebsites.com";
//$admin_view = false;


if(file_exists(dirname(__FILE__) . "/tvmodule_init.php")) {
    include(dirname(__FILE__) . "/tvmodule_init.php");
}
else {
    echo "<br/><b>".dirname(__FILE__) . "/tvmodule_init.php does not exist</b>";
}




if(isset($module_params)) {
    $def_ads = isset($module_params['def_ads']) ? $module_params['def_ads'] : 'no';
    $def_ad_id = isset($module_params['ws_ad_unit_id']) ? $module_params['ws_ad_unit_id'] : '';
}
else {
    $default_module_params = [];
    $q = "SELECT l.* FROM left_content l, module m WHERE l.module_id=m.module_id AND m.url='tvmodule' AND l.catdom_id='$domain_id';";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        $row = mysql_fetch_assoc($r);
        $default_module_params = unserialize(base64_decode($row['module_params']));
    }

    $def_ads = isset($default_module_params['def_ads']) ? $default_module_params['def_ads'] : 'no';
    $def_ad_id = isset($default_module_params['ws_ad_unit_id']) ? $default_module_params['ws_ad_unit_id'] : '';

    $module_params = [
        'ws_ad_unit_id' => $def_ad_id,
        'def_ads' => $def_ads
    ];
}


$show_default = false;
if(isset($tv_day)) $show_default = true;
//echo $tv_day;
if($show_default && file_exists(dirname(__FILE__) . "/tvmodule_default.php")) {
    include(dirname(__FILE__) . "/tvmodule_default.php");
}