/*	Calendar Script
	Copyright (c) 2006 Veeven.Org */

var Now = new Date();				// a date object containing today
var $thisYear = Now.getFullYear();	// YYYY
var $thisMonth = Now.getMonth();	// 0 to 11
var $today = Now.getDate();			// 1 to 31

var cells = new Array; // contains cells 1 to 35

var $activeCell = 1;
var $exActiveCell = 1;
var $firstVisibleCell = 1;
var $lastVisibleCell = 1;

var OnDisplay = new Date($thisYear, $thisMonth, 1, 12, 0, 0) // a date object, initially set to current month
function yearOD(){return OnDisplay.getFullYear();} // YYYY
function monthOD(){return OnDisplay.getMonth();} // 0 to 11
function firstDayOD(){return OnDisplay.getDay();} // 0 to 6
function daysOD(){return OnDisplay.getDaysInMonth();} // 28 to 31

function Today(){
	OnDisplay = new Date();
	OnDisplay.setDate(1);
	generateView();
}

function PreviousMonth(){ // go to previous month, Month On Display - 1
	if (monthOD() == JANUARY) {
		OnDisplay.setFullYear(yearOD() - 1, DECEMBER);
	}
	else {
		OnDisplay.setMonth(monthOD() - 1);
	}
	generateView();
	document.getElementById('prevMonth').blur();
}

function NextMonth(){ // go to next month, Month On Display + 1
	if (monthOD() == DECEMBER) {
		OnDisplay.setFullYear(yearOD() + 1, JANUARY);
	}
	else {
		OnDisplay.setMonth(monthOD() + 1);
	}
	generateView();
	document.getElementById('nextMonth').blur();
}

function PreviousYear(){ // go to previous year, but same month as Month On Display
	OnDisplay.setFullYear(yearOD() - 1);
	generateView();
	document.getElementById('prevYear').blur();
}

function NextYear(){ // go to next year, but same month as Month On Display
	OnDisplay.setFullYear(yearOD() + 1);
	generateView();
	document.getElementById('nextYear').blur();
}

function init(){

	for (var $i = 1; $i <= 35; $i++) {
		cells[$i] = document.getElementById("cell" + $i);
		cells[$i].addEventListener('click', activateMe, false);
	}

	fillTodaybox();
	Today();
//	generateMonthView($thisYear, $thisMonth);
}

function fillTodaybox(){

	// [CONTENT] get today date, day, full month, and full year

	document.getElementById("todayDate").firstChild.data = $today;
	document.getElementById("todayDay").firstChild.data = Now.format("%DDD");
	document.getElementById("todayMonth").firstChild.data = Now.format("%MMMM");
	document.getElementById("todayYear").firstChild.data = $thisYear;

	// [STYLE] if today is sunday, make it so
	if (Now.getDay() == SUNDAY) {
		document.getElementById("todayDate").className = 'todaySunday';
		document.getElementById("todayDay").className = 'todaySunday';
	}
}

function generateView(){

	// VARIABLES
	var $daysOD = daysOD();
	var $firstDayOD = firstDayOD();
	var $yearOD = yearOD();
	var $monthOD = monthOD();

	// CONTENT

	// fill month name and year
	document.getElementById("MMMM").firstChild.data = OnDisplay.format('%MMMM');
	document.getElementById('YYYY').firstChild.data = $yearOD;

	// clear all cells before filling
	for (var $i = 1; $i <= 35; $i++) {
		if (cells[$i].hasChildNodes()) cells[$i].removeChild(cells[$i].firstChild);
		cells[$i].removeAttribute('class');
	}

	// fill dates in cells
	for (var $i = 1; $i <= $daysOD; $i++) {
		$n = $i + $firstDayOD; // offset
		if ($n > 35) $n -= 35;
		cells[$n].appendChild(document.createTextNode($i));
	}

	$firstVisibleCell = 1 + $firstDayOD;
	$lastVisibleCell = $firstDayOD + $daysOD;

	// STYLE

	$exActiveCell = $activeCell;
	$activeCell = $firstVisibleCell;

	// show Sundays differently
	for (var $i = 1; $i <= 29; $i += 7) cells[$i].setAttribute('class', 'sunday');

	// show today differently
	document.getElementById('thismonth').style.display = "inline";
	if ($yearOD == $thisYear && $monthOD == $thisMonth) {
		$n = $today + $firstDayOD; // offset
		if ($n > 35) $n -= 35;
		$distinctClass = (Now.getDay() == SUNDAY) ? 'cellTodaySunday' : 'cellToday';
		cells[$n].setAttribute('class',$distinctClass);

		$activeCell = $n;

		//hide the link to today
		document.getElementById('thismonth').style.display = "none";
	}
	activateActiveCell();
}

function showCustomYearMonthForm(){
	customYearMonthForm = document.getElementById('customyearmonth');
	customYearMonthForm.reset();
	customYearMonthForm.style.display = 'block';
	txtYear = document.getElementById('customYYYY')
	txtYear.focus();
}

function closeCustomYearMonthForm(){
	document.getElementById('customyearmonth').style.display = 'none';
	document.getElementById('errorMsg').innerHTML = ' ';
}

function getCustomYYYYMM(){
	$customYear = document.getElementById('customYYYY').value;
	if (isNaN($customYear) || $customYear < 1000  || $customYear > 9999) {
		document.getElementById('errorMsg').innerHTML = 'Please enter a valid year (in YYYY format).';
		document.getElementById('customYYYY').focus();
		document.getElementById('customYYYY').select();
	}
	else {
		$customMonth = document.getElementById('customMM').value;
		OnDisplay = new Date ($customYear, $customMonth, 1, 12, 0, 0);
		generateView();
		closeCustomYearMonthForm();
	}
}

function activateActiveCell(){
	cells[$exActiveCell].style.borderColor = "#f6f6f6";
	cells[$activeCell].style.borderColor = "#099";
}

function activateMe(){
	$myID = this.id.substring(4)
	if ($myID >= $firstVisibleCell && $myID <= $lastVisibleCell) {
		$exActiveCell = $activeCell;
		$activeCell = $myID;
		activateActiveCell();
	}
}
/* vim: se ts=4 sw=4 : */
