<?

class calendar {

	var $day_names    = array("Mo", "Tu", "We", "Th", "Fr", "St", "Su");
	var $month_names  = array(
	'01'=>"January",
   '02'=>"February",
   '03'=>"March",
   '04'=>"April",
   '05'=>"May",
   '06'=>"June",
   '07'=>"July",
   '08'=>"August",
   '09'=>"September",
   '10'=>"October",
   '11'=>"November",
   '12'=>"December");
    var $today        = null;
    var $month        = null;
    var $year         = null;
    var $_DAY_        = 87400;


    function setMonthNames($names) {
    $this->month_names = $names;
    return true;
    }

    function setDayNames($names) {
    $this->day_names = $names;
    return true;
    }


    function getDateLink($fd) {
		if ($this->customFunc) {
			return customFunction($fd);
		} else {
			return false;
		}
    }

    function getCalendarLink($mon, $year) {
	global $sys;
    $link = glueURL(array("file"=>"calendar","uid"=>$sys['uid'],"y"=>$year,"m"=>$mon));
    return $link;
    }

    function getCal($m, $y) {
        $tm    = time();
        $fd    = mktime(0,0,0,$m,1,$y);
        $ld    = mktime(0,0,0,($m+1),1,$y);
        $fdA   = getdate($fd);
        $today = getdate($tm);

        $prev = $this->adjustDate(($m-1), $y);
        $next = $this->adjustDate(($m+1), $y);
		
        ($ld<=$tm)?$nextMonth = $this->getCalendarLink($next[0], $next[1]):$nextMonth=false;
        ($fd<=$tm)?$prevMonth = $this->getCalendarLink($prev[0], $prev[1]):$prevMonth=false;


		  $s = "<table cellspacing=0 cellpadding=4 class=clCalTable>\n";



if ($nextMonth) {
	$sNextM = "\t<a href=\"".$nextMonth."\" class=clCalLink>&raquo;</a>";
}

if ($prevMonth) {
	$sPrevM = "<a href=\"".$prevMonth."\" class=clCalLink>&laquo;</a>";
}


		$s .= "<tr><td colspan=\"7\" class=clCalTitle align=center>

$sPrevM

";
		$s .= " ".$this->month_names[$m]."&nbsp;".$y." ";
		$s .= "
		
$sNextM
		</td></tr>";



		  $s .= "\n\n<tr>\n";
        foreach($this->day_names as $_dz) {
            $s.= "\t<td class=clDayNames ".(($_dz == "We")?"style=\"padding-left:1px;padding-right:0px\"":"").">".ucfirst($_dz)."</td>\n";
        }
		$s .= "</tr>\n";
		$s .= "\n\n<tr>\n";
		
        for ($c=1 ; $c<=42 ; $c++) {
            $dA   = getdate($fd);
            $link = $this->getDateLink($fd);
			
                if ( (($c-1)%7)==0) {
                    if ($dA['mon'] != $m){
						break;
					} else {
						$s.= "</tr>\n<tr>\n";
					}
                }
                if ($c<$fdA['wday'] || $dA['mon'] != $m || $fdA['wday']==0 && $c<7) {
                    //if($dA['mon'] != $m){break;}
					$s.= "\t<td class=clDay>&nbsp;</td>\n";
				} else {
					if ($today['mday'] == $dA['mday'] & $today['mon'] == $dA['mon'] && $today['year'] == $dA['year']) {
						if ($link) {
							$s.= strftime("\t<td class=clToday><a href=$link class=clTodayLink>%d</a></td>\n", $fd);
						} else {
							$s.= strftime("\t<td class=clToday>%d</td>\n", $fd);
						}
					} else {
						if ($link){
							$s.= strftime("\t<td class=clDay><a href=$link class=clDayLink>%d</a></td>\n", $fd);
						} else {
							$s.= strftime("\t<td class=clDay>%d</td>\n", $fd);
						}
					}
					$fd += $this->_DAY_;
                }
        }
        $s .= "</tr>\n";
        $s .= "</table>\n";
        return $s;
    }

    function adjustDate($m, $y) {
        $a[0]=$m;
        $a[1]=$y;
        $t = getdate(mktime(0,0,0,$m,1,$y));
        $a[1]=$t['year'];
        $a[0]=sprintf("%02d", $t['mon']);
        return $a;
    }
}
?>