Class Calendar
{
var $post_index;
var $current_date;
var $total_rows;
var $current_row;
function Calendar() {
}
function getNextDate() {
	$this->current_row++;
	$row = mysql_fetch_assoc($this->post_index);
	$this->current_date = $row["date_published"];
	if($this->current_date == "") {
		return 0;
	} else {
		return 1;
	}
}
function getPreviousDate() {
	$this->current_row--;
	if($this->current_row < 0) {
		return 0;
	} else {
		mysql_data_seek($this->post_index, $this->current_row);
		$row = mysql_fetch_assoc($this->post_index);
		$this->current_date = $row["date_published"];
		return 1;
	}
}
// This function pads a number to a fixed length by 
// adding leading zeroes.
// e.g. "3" padded to length "2" returns "03"
function pad($s, $n)
{
	$r = $s;
	while (strlen($r) < $n)
	{
		$r = "0".$r;
	}
	return $r;
}
function getCalendarLink($month, $year)
{
	$Year = $this->pad($year, 4);
	$Month = $this->pad($month, 2);
	$s = getenv('SCRIPT_NAME');
//	return "$s?Type=calendar&Year=$Year&Month=$Month";
	return "/archive/$Year/$Month";
}
function getMonthLinkData($month, $year) {
	$Year = $this->pad($year, 4);
	$Month = $this->pad($month, 2);
	$query = "SELECT DISTINCT REPLACE(left(date_published, 10), '-', '') 
		AS date_published FROM posts WHERE date_published BETWEEN
		\"$Year-$Month-01 00:00:00\" AND \"$Year-$Month-31 23:59:59\"
		AND published = 1 AND visible = 1 ORDER BY date_published ASC";
	$this->post_index = mysql_query_err($query);
	$row = mysql_fetch_assoc($this->post_index);
	$this->current_date = $row["date_published"];
}
function getDateLink($day, $month, $year)
{
	$Year = $this->pad($year, 4);
	$Month = $this->pad($month, 2);
	$Day = $this->pad($day, 2);
	$date = "$Year$Month$Day";
	while($this->current_date < $date && $this->current_date != NULL) {
		$this->getNextDate();
	}       
	if ($this->current_date == $date) {
		$s = getenv('SCRIPT_NAME');
//		return "$s?Type=calendar&Year=$Year&Month=$Month&Day=$Day";
		return "/archive/$Year/$Month/$Day";
	} else {
		return "";
	}
}
function printStylesheet($uc = 4) {
	if ($uc>0 && file_exists("esconfig.php")) {
		require("esconfig.php");
		$config = ereg_replace ("\?", "", $cfg[$uc]);
		$config = explode ("&",$config);
		foreach ($config as $values) {
			$c0 = explode ("=",$values);
			$$c0[0] = $c0[1];
		}
	}
	// FONT SIZES AND WEIGHT
	// These options can be set in the script or overridden by command-line options of
	// the same named variables. The sizes are in pixels.
	if (!$fsm) $fsm="18"; // FONT SIZE MONTH
	if (!$fsd) $fsd="11";  // FONT SIZE DAY NAMES
	if (!$fsn) $fsn="11"; // FONT SIZE NUMBERS
	if (!$fwm) $fwm="bold"; else $fwm="normal"; // FONT WEIGHT MONTH
	if (!$fwd) $fwd="normal"; else $fwd="bold"; // FONT WEIGHT DAY NAMES
	if (!$fwn) $fwn="normal"; else $fwn="bold"; // FONT WEIGHT NUMBERS
	// ONLY EDIT THE STYLE SHEET BELOW IF YOU WANT TO CHANGE THE FONT FACE AND/OR COLOR
	$s .= "\n";
	print $s;
}
function printMonth($yr = 0, $mo = 0, $uc = 0, $direction = 0) {
//////////////////////////////////////////
//////////BEGIN CALENDAR CODE ////////////
//////////////////////////////////////////
// QUICK REFERENCE
// :: uc = Use Configuration File
// :: ms = Mark Style
// :: ds = Day Start
// :: ny = Not Display Year
// :: nt = Not Mark Today
// :: es = Event Start
// :: ee = Event End
// :: ot = Offset Time
// :: th = Table Height
// :: tw = Table Width
// :: cs = Cell Spacing
// :: cp = Cell Padding
// :: al = Cell Alignment
// :: nbc = Normal Numbers Background Color
// :: tfc = Todays Font Color
// :: mbc = Marked Numbers Background Color
// :: dtc = Day Names Table Background Color
// :: ntc = Blank Numbers Background Color
// :: mtc = Month and Year Table Background Color
// :: fsm = Font Fize Month
// :: fsd = Font Size Dates
// :: fsn = Font Size Numbers
// :: fwm = Font Weight Month Name
// :: fwd = Font Weight Day Names
// :: fwn = Font Weight Numbers
// LOAD CONFIGURATION FILE
// $uc=x :: Use the configuration file set as 'x'
// This option can be set here or overridden by the command-line option 'uc='
if (!$uc) $uc = "0"; // SET THIS TO A NUMBER TO USE THE CONFIGURATION FILE
if ($uc>0 && file_exists("esconfig.php")) {
	require("esconfig.php");
	$config = ereg_replace ("\?", "", $cfg[$uc]);
	$config = explode ("&",$config);
	foreach ($config as $values) {
		$c0 = explode ("=",$values);
		$$c0[0] = $c0[1];
	}
}
// CALENDAR MARKING STYLE
// $ms=1 :: Mark calendar dates using table background colors [Default]
// $ms=2 :: Mark calendar dates using graphics for background colors
// NOTE: If using Style 2, all 'stat' graphics should be in the same directory as this script
// You can edit the 'stat' graphics to meet your needs
if (!$ms) $ms = "1";
// CALENDAR MARKING COLORS
// Edit these colors to suit your needs
// These options change be changed here or by the command-line options 'nbc', 'mbc', or 'tfc'
if (!$nbc) $nbc = "EEEEEE"; // NORMAL BACKGROUND COLOR
if (!$mbc) $mbc = "EEBBBB"; // MARKED BACKGROUND COLOR
if (!$tfc) $tfc = "CF0000"; // TODAY'S FONT COLOR
// DAY NAMES
// Edit the calendar day name column headers below
$day = array("S", "M", "T", "W", "T", "F", "S");
// MONTH NAMES
// Edit the calendar month names below
$mth = array("Author Uses Inconsistent Array Addressing", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
// ON WHAT DAY DOES THE WEEK START?
// 0=Sunday; 1=Monday; 2=Tuesday; 3=Wednesday; 4=Thursday; 5=Friday; 6=Saturday
// This option can be set here or overridden by the command-line option 'ds='
if (!$ds) $ds=0;
// PRINT YEAR?
// $ny=1 :: Don't display the year number after the month name
// This option can be set here or overridden by the command-line option 'ny='
if (!$ny) $ny=0;
// MARK TODAY?
// $nt=1 :: Don't mark today's date
// This option can be set here or overridden by the command-line option 'nt='
if (!$nt) $nt=0;
// HOURS TO OFFSET TIME +/-
// To add hours, enter a positive value. To substract hours, enter a "-" negative value
// This option can be set here or overridden by the command-line option 'ot='
if (!$ot) $ot = "-0";
// CALENDAR WIDTH AND HEIGHT
// Set $tw to the width of the calendar table
// Set $th to the height of the calendar table (does not include the month table which is additional)
// These options can be set here or overridden by the command-line options 'tw=' and 'ch='
if (!$tw) $tw="175"; // Table Width
if (!$th) $th="140"; // Table Height (with 6 rows of dates)
// NOTE: Table may be smaller than specified for months with only 5 rows of dates
// CALENDAR CELL SPACING AND PADDING
// Set $cs to the number of pixels of cell spacing (space between each cell)
// Set $cp to the number of pixels of cell padding (space around names and numbers inside cells)
// These options can be set here or overridden by the command-line options 'cs=' and 'cp='
if (!$cs) $cs=1; // IMPORTANT! To set cs to 0 from the command-line, you must use -0
if (!$cp) $cp=0;
// DATE NUMBER ALIGNMENT
// Set $algn to 0 [defult] to align the dates to the middle and center of table cells.
// Set $algn to 1 to align the dates to the upper-right corner of table cells.
// This option can be set here or overridden by the command-line option 'algn='
if (!$al) $al="0";
////////////////////////////////////////
///// DO NOT EDIT BELOW THIS POINT /////
////////////////////////////////////////
// DETERMINE AND SET DATE NUMBER CELL ALIGNMENT
if ($al==1) $al="align=\"right\" valign=\"top\"";
elseif ($al==2) $al="align=\"left\" valign=\"top\"";
elseif ($al!=1 && $al!=2) $al="align=\"center\" valign=\"middle\"";
// DETERMINE TODAYS DAY NUMBER
$ot = $ot*3600;
$tmo = date("m", time()+$ot);
$tda = date("j", time()+$ot);
$tyr = date("Y", time()+$ot);
$tnum = (intval((date ("U", mktime(20,0,0,$tmo,$tda,$tyr))/86400))); // TODAY'S DAY NUMBER
// CHECK FOR COMMAND LINE DATE VARIABLES
if (!$mo) $mo=$tmo;
if (!$yr) $yr=$tyr;
$daycount = (intval((date ("U", mktime(20,0,0,$mo,1,$yr))/86400)))-$ds; // FIRST OF MONTH DAY NUMBER
$daycount=$daycount+$ds; // ADJUST FOR DAY START VARIABLE
$mo=intval($mo);
$mn = $mth[$mo]; // SET MONTH NAME
if ($ny!=1) {$mn = $mn." ".$yr;} // ADD YEAR TO MONTH NAME?
// ON WHAT DAY DOES THE FIRST FALL
$sd = date ("w", mktime(0,0,0,$mo,1-$ds,$yr));
$cd = 1-$sd;
// NUMBER OF DAYS IN MONTH
$nd = mktime (0,0,0,$mo+1,0,$yr);
$nd = (strftime ("%d",$nd))+1;
////////////////////////////////////////
// PROCESS DAY MARKING /////////////////
////////////////////////////////////////
if ($es) {
	$es = explode ("x",$es);
	$smc = count ($es);
	$ee = explode ("x",$ee);
	$emc = count ($ee);
	if ($smc==1) {
		$es[1]="3000-01-01";
		$ee[1]="3000-01-01";
	}
}
$i=0;
while ($i < $smc) {
	$es[$i] = ereg_replace('-','/', $es[$i]);
	$ee[$i] = ereg_replace('-','/', $ee[$i]);
	$start = intval(strtotime ($es[$i])/86400)+1;
	$end = intval(strtotime ($ee[$i])/86400)+1;
	if (!$ee[$i]) $end=$start; // MARK SINGLE DAY WITH ONLY ES VARIABLE
	if (!$bgc[$start]) { 
		$bgc[$start]=1;
	} else {
		$bgc[$start]=4;
	}
	$bgc[$end]=3;
	for ($n = ($start+1); $n < $end; $n++) {
		$bgc[$n] = 2;
	}
	$i++;
}
////////////////////////////////////////////
// DISPLAY CALENDAR ////////////////////////
////////////////////////////////////////////
// Populate from database
$this->getMonthLinkData($mo, $yr);
// ADJUST TABLE HEIGHT FOR 5 ROW MONTHS
$checksize=$fsd+$cs+$cp;
$checkrows=$ds+$nd-$cd;
if ($checkrows<36) {
	$th=$th-intval(($th-$checksize)/6);
}
// Print the month name and optionally the year
$o .= "
\n";
$o .= " \n";
$o .= "  | $mn\n";
$o .= "\n";
$o .= " | 
\n";
$o .= "
\n";
// Print the body of the calendar
$o .= "\n";
$total_height = $fsd + $cs + $cp;
$o .= " 6) $dayprint=$dayprint-7;
	$o .= "  | $day[$dayprint]\n";
}
$o .= " | 
\n";
// PRINT CALENDAR USING TABLE BACKGROUND COLORS [CALENDAR STYLE 1]
if ($ms==1) { 
	for ($i = 1; $i<7; $i++) { 
		if ($cd>=$nd) break;
		$o .= " 0 && $cd<$nd) {
				$o .= "  | ";
				$link = $this->getDateLink($cd, $mo, $yr);
				$o .= (($link == "") ? "$cd" : "$cd");
				$o .= "\n";
				$daycount++;
				$cd++;
			} else {
				$o .= " | 0 && $cd<$nd) {
					$o .= " bgcolor=\"#";
					if ($bgc[$daycount]) {
						$o .= $mbc;
					} else {
						$o .= $nbc;
					}
					$o .= "\">";
					$link = $this->getDateLink($cd, $mo, $yr);
					$o .= (($link == "") ? $cd : "$cd");
					$daycount++;
				} else {
					$o .= ">";
				} 
				$cd++;
				$o .= "\n";
			}
		}
		$o .= " | 
\n";
	}
	$o .= "
\n";
}
print $o;
}
// This last bit commented out for now.
/**
}
} // END [CALENDAR STYLE 1]
// PRINT CALENDAR USING GRAPHICS BACKGROUNDS [CALENDAR STYLE 2]
if ($ms==2) { 
for ($i = 1; $i<7; $i++) { 
if ($cd>=$nd) break;
?>
  CLASS="dates"> 
for ($prow = 1; $prow<8; $prow++) { 
if ($daycount==$tnum && $nt!="1" && $cd>0 && $cd<$nd) {echo "  | $cd\n";$daycount++;$cd++;}
else { ?> | 0 && $cd<$nd) {echo " background=\"stat$bgc[$daycount].gif\">$cd";$daycount++;} else {echo ">";} $cd++;?>}} ?> | 
}
} // END [CALENDAR STYLE 2]
?>
}
**/
}
?>