<?php
require_once("common.php");
require_once("/usr/lib/wangerin/get_cached_file.php");

$years = (isset($_GET["years"]) && is_numeric($_GET["years"]) ? $_GET["years"] : 1);

$event_colors = array(
			"Installfest"			=> array(	"text" => "If",		"color" => "greenyellow",	),
			"Linuxaften"			=> array(	"text" => "La",		"color" => "lime",		),
			"Generalforsamling"		=> array(	"text" => "GF",		"color" => "cyan",		),
			"AlsLUGs f&oslash;dselsdag"	=> array(	"text" => "fd",		"color" => "Peru",		),
			"Pakning til Kulturnat"		=> array(	"text" => "pa",		"color" => "SkyBlue",		),
			"Kulturnat"			=> array(	"text" => "KN",		"color" => "SkyBlue",		),
			"&Aring;bent hus i BAKC"	=> array(	"text" => "&aring;h",	"color" => "SkyBlue",		),
			"AktiVigDag"			=> array(	"text" => "AV",		"color" => "SkyBlue",		),
			"lugcamp"			=> array(	"text" => "LC",		"color" => "SkyBlue",		),
			"Linux Presentation Day"	=> array(	"text" => "LPD",	"color" => "violet",		),
			"LUG Flensburg - LEA"		=> array(	"text" => "FL",		"color" => "Yellow",		),
//			"Hellig- og m&aelig;rkedage"	=> array(				"color" => "red",		),
			);

//$holidays = str_replace(array_keys($sr),$sr,
//			get_cached_file("http://files.apple.com/calendars/Danish32Holidays.ics"));

foreach(query_array("			##########################################################
					SELECT	*,
						REPLACE(
							DATE_FORMAT(CONCAT(date,' ',
								CASE
									WHEN NOT(ISNULL(dates.start))		THEN dates.start
									WHEN NOT(ISNULL(activities.start))	THEN activities.start
									ELSE ''
								END),'$date_format %k:%i'),

							DATE_FORMAT(NOW(),'$date_format'),
							'I dag') AS start
					FROM	$dates_query
					LEFT JOIN activities USING(activity)
					ORDER BY date
					##########################################################") AS $event) {
	$months[substr($event->date,0,7)][substr($event->date,-2)*1] = $event;
}

for( $y = date("Y",time()-$years*365*86400) ; $y <= 2025 ; $y++) {
	$easter = easter_date($y);
	$months["$y-01"]["1"]->activity = "Nyt&aring;rsdag";
	foreach(array(
			-3 => "Sk&aelig;rtorsdag",
			-2 => "Langfredag",
			0 => "P&aring;skedag",
			1 => "2. P&aring;skedag",
			26 => "St. Bededag",
			39 => "Kr. Himmelfartsdag",
			49 => "Pinsedag",
			50 => "2. Pinsedag",
			) AS $offset => $dag) {
		$d = $easter+86400*$offset;
		$holidays[$_d_ = date("Y-m-d",$d)] = $dag;
		//echo "<div>$_d_ = $dag</div>";
		$event_colors[$dag] = array( "text" => "?",	"color" => "red",	);
		$months[date("Y-m",$d)][date("d",$d)]->activity = $dag;
	}

	$months["$y-12"]["24"]->activity = "Juleaften";
	$months["$y-12"]["25"]->activity = "Juledag";
	$months["$y-12"]["26"]->activity = "2. Juledag";
	$months["$y-12"]["31"]->activity = "Nyt&aring;rsaften";
	if ($y != 2017)	$sr[$y."0416"] = "xxxx";
	if ($y != 2017)	$sr[$y."0605"] = "xxxx";
	$sr[$y."0624"] = "xxxx";
	$sr[$y."1110"] = "xxxx";
}

foreach($event_colors AS $event => $details) echo "
		<div style='padding-left:0.5em; padding-right:0.5em; float:left; background:".$details["color"].";'>$event</div>";
echo "
				<table border=1 cellspacing=0>
					<tr>
						<td>&nbsp;</td>";
foreach(array(
		"S&oslash;","Ma","Ti","On","To","Fr","L&oslash;",
		"S&oslash;","Ma","Ti","On","To","Fr","L&oslash;",
		"S&oslash;","Ma","Ti","On","To","Fr","L&oslash;",
		"S&oslash;","Ma","Ti","On","To","Fr","L&oslash;",
		"S&oslash;","Ma","Ti","On","To","Fr","L&oslash;",
		"S&oslash;","Ma",//"Ti",//"On","To","Fr","L&oslash;",
		) AS $day) echo "
						<th>$day</th>";
echo "
						<td>&nbsp;</td>
					</tr>";
ksort($months);
foreach($months AS $month => $month_data) {
	if ($month < date("Y-m",time()-$years*365*86400)) continue;
	if (value_changed("year",$year = substr($month,0,4))) {
		echo "<tr>
						<th colspan=39>$year</th>
					</tr>";
	}
	echo "<tr>
						<th style='".
							(strtotime("$month-".date("t",strtotime("$month-01"))) < time() ? "color=silver; font-size:50%": "")
								."'
							colspan=".((date("N",strtotime("$month-01"))) % 7+1)." align='left'>$month</th>";
	for ($day = 1 ; $day <= date("t",strtotime("$month-01")) ; $day++) {
		$text = $day;
		$ugedag = date("N",$stamp = strtotime("$month-$day"));
		switch (true) {
			case $ugedag == 5 && $day >= 22 && $day <= 28 :	$color = "Yellow";	$text="FL";	break;
			case $ugedag == 6 :				$color = "MintCream";			break;
			case $ugedag == 7 :				$color = "MistyRose";			break;
			default :					$color = "white";
		}
		if (strpos($holidays,"DTSTART;VALUE=DATE:".date("Ymd",strtotime($month."-".substr(100+$day,-2))))) {
			$color="red";
		}
		if (isset($month_data[$day])) {
			$s = substr($month_data[$day]->activity,0,25);
			if (isset($event_colors[$s])) {
				$c = $event_colors[$s];
				$text = $c["text"];
				$color = $c["color"];
			} else {
				$text = "?";
				$color = "red";
			}
		}
		echo "
						<td style='".
							($stamp+86400 < time() ? "color=silver; font-size:50%": "")
								.";' bgcolor='$color' align='center'
									title='".$month_data[$day]->activity."'>$text</td>";
	}
	echo "
						<th style='".
							(strtotime("$month-".date("t",strtotime("$month-01"))) < time() ? "color=silver; font-size:50%": "")
								."'
							colspan=".(38-
									date("t",strtotime("$month-01"))-
									((7+date("N",strtotime("$month-01"))) % 7)-
									0).
									" align='right'>$month</th>";
}
echo "
				</table>";
echo "<pre>".print_r($months,true)."</pre>";

echo "<pre>".print_r($holidays,true)."</pre>";
