* @link http://jpg.priv.no/scripts/astro/
 *
 * Made for Astro widget templates
 * Requires Astro widget script
 */
require_once dirname(__FILE__) . '/astro.php';
require_once dirname(__FILE__) . '/templates.php';
// Options and variables
date_default_timezone_set('Europe/Oslo');
setlocale(LC_TIME, 'nb_NO.UTF-8');
$location = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_ENCODED);
if (empty($location)) {
    $location = urlencode('Oslo');
}
$geocode_param['address'] = $location;
$geocode = astro_api_geocode($geocode_param);
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="no">
<head>
    <title>Sol og m&aring;ne for <?php 
echo $geocode['name'];
?>
</title>
    <meta charset="utf-8">
    <style>
        .astrowidget table {
            border-collapse: collapse;
        }
/**
 * Astro widget - small
 *
 * @param  string $address - location to request
 * @param  boolean $show_location - show or hide text
 * @param  int $lat - latitude if static location is preferred
 * @param  int $lon - longitude if static location is preferred
 * @return void
 */
function astro_widget_small($address, $show_location = true, $lat = null, $lon = null)
{
    $location = $address;
    // Geocode API parameters
    $geocode_param['address'] = $address;
    //$geocode_param['key'] = 'INSERT YOUR GOOGLE API_KEY HERE';
    // Sunrise API parameters
    $sunrise_param['date'] = date('Y-m-d', strtotime('now'));
    //$sunrise_param['from'] = date('Y-m-d', strtotime('now'));
    //$sunrise_param['to'] = date('Y-m-d', strtotime('now'));
    $sunrise_param['lat'] = $lat;
    $sunrise_param['lon'] = $lon;
    // Google Maps: Geocode API request
    if (isset($geocode_param['address']) && !isset($sunrise_param['lat']) && !isset($sunrise_param['lon'])) {
        $geocode = astro_api_geocode($geocode_param);
        $sunrise_param['lat'] = $geocode['lat'];
        $sunrise_param['lon'] = $geocode['lon'];
        $location = $geocode['name'];
    }
    // Meteorologisk institutt: Sunrise API request
    $sunrise = astro_api_sunrise($sunrise_param);
    // Set location text
    $location_text = isset($location) && $show_location ? $location . ' - ' : '';
    ?>
    <div class="astrowidget astrowidgetS">
        <!-- Astro widget by jpg: http://jpg.priv.no/scripts/astro/ -->
        <table>
            <tr>
                <th colspan="5">
                    <p style="font-size: 150%;">Sol og m&aring;ne</p>
                </th>
            </tr>
            <?php 
    foreach ($sunrise as $key => $value) {
        ?>
                <tr>
                    <th colspan="5">
                        <p style="font-size: 110%;  font-weight: normal; text-align: center;"><?php 
        echo $location_text . strftime('%A', strtotime($key));
        ?>
</p>
                    </th>
                </tr>
                <tr>
                    <th rowspan="2" style="text-align: center; vertical-align: middle;">
                        <img src="http://jpg.priv.no/pic/astro/sun50h.png"
                             style="max-height: 50px;"/>
                    </th>
                    <?php 
        if ($value['sun']['never_rise']) {
            ?>
                        <td rowspan="2" style="text-align: center;">
                            <p>nede hele dagen</p>
                        </td><?php 
        } elseif ($value['sun']['never_set'] && !isset($value['sun']['rise'])) {
            ?>
                        <td rowspan="2" style="text-align: center;">
                            <p>midnattssol</p>
                        </td><?php 
        } else {
            $insert_sun_p2 = true;
            ?>
                        <td style="text-align: center; vertical-align: middle;">
                            <p><?php 
            echo isset($value['sun']['rise']) ? $value['sun']['rise'] : '&nbsp;';
            ?>
</p>
                        </td>
                        <?php 
        }
        ?>
                    <td style="text-align: center; vertical-align: middle;">
                        <img src="http://jpg.priv.no/pic/astro/up8w.png" style="max-height: 8px; max-width: 8px;"/>
                    </td>
                    <?php 
        if ($value['moon']['never_rise']) {
            ?>
                        <td rowspan="2" style="text-align: center;">
                            <p>ingen m&aring;ne</p>
                        </td><?php 
        } elseif ($value['moon']['never_set'] && !isset($value['moon']['rise'])) {
            ?>
                        <td rowspan="2" style="text-align: center;">
                            <p>oppe hele tiden</p>
                        </td><?php 
        } else {
            $insert_moon_p2 = true;
            ?>
                        <td style="text-align: center; vertical-align: middle;">
                            <p><?php 
            echo isset($value['moon']['rise']) ? $value['moon']['rise'] : '&nbsp;';
            ?>
</p>
                        </td>
                        <?php 
        }
        ?>
                    <th rowspan="2" style="text-align: center; vertical-align: middle;">
                        <img src="http://jpg.priv.no/pic/astro/moon50h.png"
                             style="max-height: 50px;"/>
                    </th>
                </tr>
                <?php 
        if (isset($insert_sun_p2) || isset($insert_moon_p2)) {
            ?>
                    <tr>
                        <?php 
            if (isset($insert_sun_p2) && $insert_sun_p2) {
                ?>
                            <td style="text-align: center; vertical-align: middle;">
                                <p><?php 
                echo isset($value['sun']['set']) ? $value['sun']['set'] : '&nbsp;';
                ?>
</p>
                            </td>
                            <?php 
                unset($insert_sun_p2);
            }
            ?>
                        <td style="text-align: center; vertical-align: middle;">
                            <img src="http://jpg.priv.no/pic/astro/down8w.png"
                                 style="max-height: 8px; max-width: 8px;"/>
                        </td>
                        <?php 
            if (isset($insert_moon_p2) && $insert_moon_p2) {
                ?>
                            <td style="text-align: center; vertical-align: middle;">
                                <p><?php 
                echo isset($value['moon']['set']) ? $value['moon']['set'] : '&nbsp;';
                ?>
</p>
                            </td>
                            <?php 
                unset($insert_moon_p2);
            }
            ?>
                    </tr>
                    <?php 
        } else {
            ?>
                    <tr>
                    </tr>
                    <?php 
        }
    }
    ?>
            <tr>
                <th colspan="5">
                    <p style="color: gray; font-size: 75%; font-weight: normal;">
                        Astrodata levert av <a href="http://met.no/" style="color: gray; text-decoration: none;"
                                               target="_blank">met.no</a>
                    </p>
                </th>
            </tr>
        </table>
    </div>
    <?php 
}