<?php if (!defined('PmWiki')) exit();

$RecipeInfo['SimpleCalendar']['Version'] = '2007-03-15';

SDV($HTMLStylesFmt['simplecalendar'], "
div#simplecalendar {
	font-size: smaller;
}

table.simplecalendar {
	border: 2px solid #abd5d6;
	border-collapse: collapse;
}
table.simplecalendar caption {
	text-align: center;
	font-weight: bold;
	padding-top: 3px;
	padding-bottom: 3px;
}
table.simplecalendar th {
	background-color: #eeeeee;
	border: 1px dotted #abd5d6;
	width: 2.2em;
	font-weight: normal;
}
table.simplecalendar td {
	border: 1px dotted #abd5d6;
	text-align: center;
	padding: 3px;
}

th.simplecalendar-daynames {
	text-align: center;
	color: green;
}
td.simplecalendar-blank {
	background-color: #ddbbaa;
}
td.simplecalendar-today {
	background-color: #ffff00;
	color: #000000;
}
td.simplecalendar-day {
	background-color: #ffeedd;
	color: slategrey;
}
");

SDV($simplecalendar_weekstart, 1); // Start on Monday;
SDVA($simplecalendar_dayname, array("S", "M", "T", "W", "T", "F", "S"));

Markup('calendar','directives',"/^\(:calendar:\)\s*$/", "simplecalendar");

$tyear = date("Y");
$tmonth = date("n");
$tday = date("d");

// If page variable not set us current date;.
if (isset($_REQUEST['year'])) {
	$year = $_REQUEST['year'];
} else {
	$year	= $tyear;
}
if (isset($_REQUEST['month'])) {
	$month = $_REQUEST['month'];
} else {
	$month	= $tmonth;
}

if ($month < 10) {
	$paddedmonthnum = "0".$month;
} else {
	$paddedmonthnum = $month;
}

$FmtPV['$SimpleCalendarMonth'] = '"' . $paddedmonthnum . '"';
$FmtPV['$SimpleCalendarYear'] = '"' . $year . '"';

function simplecalendar()
{

	global $pagename;
	global $simplecalendar_weekstart;
	global $simplecalendar_dayname;
	global $paddedmonthnum;
	global $year;
	global $month;
	global $tyear;
	global $tmonth;
	global $tday;

	$itime	= mktime(0,0,0,$month,$tday,$year); 
	$monthname = PSFT("%B %Y",$itime); // Full month name YYYY

	$pyear = $year;
	$pmonth = $month - 1;
	if ($pmonth == 0) {
		$pmonth = 12;
		$pyear -= 1;
	}
	$nyear = $year;
	$nmonth = $month + 1;
	if ($nmonth == 13) {
		$nmonth = 1;
		$nyear += 1;
	}

	$out = "";
	$out.= "<div id='simplecalendar'>";
	$out.= "<table class='simplecalendar'>";
	$out.= "<caption>[[$pagename?year=$pyear&month=$pmonth | &LT;&LT;]] $monthname [[$pagename?year=$nyear&month=$nmonth | &GT;&GT;]]</caption>";

	$totaldays = date("t",mktime(0,0,0,$month,1,$year));
	$startdayofweek = date('w',mktime(0,0,0,$month,1,$year));

	if	($simplecalendar_weekstart >= 1 && $startdayofweek == 0)
	{
		$startdayofweek = 7;
	}

	$out.="<tr>";
	for ($ind = 0; $ind < 7; $ind++)
	{
		$spacing = ' width="14%"';
		$d=($ind+$simplecalendar_weekstart) % 7;
		$out.="<th$spacing class='simplecalendar-daynames'><p>".$simplecalendar_dayname[$d]."</p></th>";
	}
	$out.="</tr>";

	for	(	$index=($simplecalendar_weekstart-$startdayofweek); $index < $totaldays; )
	{
		$out.="<tr>";

		for	(	$ind = 0; $ind < 7; $ind++, $index++)
		{

			$daynum = $index+1;
			if ($index < 0)
			{ // empty cell before cal starts
				$out.="<td class='simplecalendar-blank'><p>&nbsp;</p></td>";
			}
			else if ($index >= $totaldays)
			{ // empty cell after cal ends.
				$out.="<td class='simplecalendar-blank'><p>&nbsp;</p></td>";
			}
			else
			{ // Normal cal cell.

				if ($daynum < 10) {
					$paddeddaynum = "0".$daynum;
				} else {
					$paddeddaynum = $daynum;
				}

				$daynumtext = $daynum;

				if	(	$tyear == $year
					&&	$tmonth == $month
					&&	$tday == $daynum
					)
				{	// TODAY
					$out.="<td class='simplecalendar-today'><p>[[$tyear-$paddedmonthnum-$paddeddaynum | $daynumtext]]</p></td>";
				}
				else
				{
					$out.="<td class='simplecalendar-day'><p>[[$year-$paddedmonthnum-$paddeddaynum | $daynumtext]]</p></td>";
				}

			}

		}

		$out.="</tr>";
	}

	$out.= "</table>";
	$out .= "[[$pagename?year=$tyear&month=$tmonth | Today]]";
	$out .= "</div>";

	return $out;
}